crono 0.8.7.pre → 0.8.8.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2aba8e6d1336334c7705b1849e0627afb048f3aa
4
- data.tar.gz: 0bbb66243e43eebb3bff785759c9b644f94e6883
3
+ metadata.gz: 4ada92b83365f8a5dc386a0206bc78c885406109
4
+ data.tar.gz: 4d8a3131f5fb9b8e80b2fd5ef289ff00e673b7fc
5
5
  SHA512:
6
- metadata.gz: 7327585dd13c2f2b996d7caaa232d36154b0fde0944655d7b19e31234753f12f88beea3c7a5eb6292bd69c95bd95aff439cf090a4da37bc051841ea57483e591
7
- data.tar.gz: 9dc8f836867c3b5dbfce6b06cdfb5abf9d40d57e89465bc22a59f4d3f7ed427aad05bbde875d942c0d20779bd20879d269d1d0de75f88440733622ff751927ea
6
+ metadata.gz: f23b4482087ae0345cf642507e72afc0695fcfddd604eaadfb78bae95114dbe0ae902d39512e41cfaaeccf6f3c936e858eeac71b11bc45d728e64df92d49a6ef
7
+ data.tar.gz: d33fac75dba413e402171fb7a1fbdb94ee0ae78401cb4cf9ebf67d4f7027065a2dc66d0a3066e0a738338620f2764091b929a4958b4714bb8552090ea4c10bc9
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- crono (0.8.7.pre)
4
+ crono (0.8.8.pre)
5
5
  activejob (~> 4.0)
6
6
  activerecord (~> 4.0)
7
7
  activesupport (~> 4.0)
data/lib/crono.rb CHANGED
@@ -10,6 +10,7 @@ require 'crono/job'
10
10
  require 'crono/scheduler'
11
11
  require 'crono/config'
12
12
  require 'crono/performer_proxy'
13
+ require 'crono/cronotab'
13
14
  require 'crono/orm/active_record/crono_job'
14
15
  require 'crono/railtie' if defined?(Rails)
15
16
 
data/lib/crono/cli.rb CHANGED
@@ -21,6 +21,7 @@ module Crono
21
21
 
22
22
  write_pid
23
23
  load_rails
24
+ Cronotab.process(File.expand_path(config.cronotab))
24
25
  print_banner
25
26
 
26
27
  check_jobs
@@ -71,7 +72,6 @@ module Crono
71
72
  require 'rails'
72
73
  require File.expand_path('config/environment.rb')
73
74
  ::Rails.application.eager_load!
74
- require File.expand_path(config.cronotab)
75
75
  end
76
76
 
77
77
  def check_jobs
@@ -0,0 +1,10 @@
1
+ module Crono
2
+ class Cronotab
3
+ def self.process(cronotab_path = nil)
4
+ cronotab_path ||= ENV['CRONOTAB'] || (defined?(Rails) &&
5
+ File.join(Rails.root, Config::CRONOTAB))
6
+ fail 'No cronotab defined' unless cronotab_path
7
+ require cronotab_path
8
+ end
9
+ end
10
+ end
data/lib/crono/job.rb CHANGED
@@ -6,18 +6,20 @@ module Crono
6
6
  class Job
7
7
  include Logging
8
8
 
9
- attr_accessor :performer, :period, :last_performed_at, :job_log,
10
- :job_logger, :healthy
9
+ attr_accessor :performer, :period, :last_performed_at,
10
+ :next_performed_at, :job_log, :job_logger, :healthy
11
11
 
12
12
  def initialize(performer, period)
13
13
  self.performer, self.period = performer, period
14
14
  self.job_log = StringIO.new
15
15
  self.job_logger = Logger.new(job_log)
16
+ self.next_performed_at = period.next
16
17
  @semaphore = Mutex.new
17
18
  end
18
19
 
19
20
  def next
20
- period.next(since: last_performed_at)
21
+ return next_performed_at if next_performed_at.future?
22
+ Time.now
21
23
  end
22
24
 
23
25
  def description
@@ -31,6 +33,7 @@ module Crono
31
33
  def perform
32
34
  log "Perform #{performer}"
33
35
  self.last_performed_at = Time.now
36
+ self.next_performed_at = period.next(since: last_performed_at)
34
37
 
35
38
  Thread.new { perform_job }
36
39
  end
@@ -44,6 +47,7 @@ module Crono
44
47
 
45
48
  def load
46
49
  self.last_performed_at = model.last_performed_at
50
+ self.next_performed_at = period.next(since: last_performed_at)
47
51
  end
48
52
 
49
53
  private
@@ -61,23 +65,24 @@ module Crono
61
65
 
62
66
  def perform_job
63
67
  performer.new.perform
64
- finished_time_sec = format('%.2f', Time.now - last_performed_at)
65
68
  rescue StandardError => e
66
- handle_job_fail(e, finished_time_sec)
69
+ handle_job_fail(e)
67
70
  else
68
- handle_job_success(finished_time_sec)
71
+ handle_job_success
69
72
  ensure
70
73
  save
71
74
  end
72
75
 
73
- def handle_job_fail(exception, finished_time_sec)
76
+ def handle_job_fail(exception)
77
+ finished_time_sec = format('%.2f', Time.now - last_performed_at)
74
78
  self.healthy = false
75
79
  log_error "Finished #{performer} in #{finished_time_sec} seconds"\
76
- "with error: #{exception.message}"
80
+ " with error: #{exception.message}"
77
81
  log_error exception.backtrace.join("\n")
78
82
  end
79
83
 
80
- def handle_job_success(finished_time_sec)
84
+ def handle_job_success
85
+ finished_time_sec = format('%.2f', Time.now - last_performed_at)
81
86
  self.healthy = true
82
87
  log "Finished #{performer} in #{finished_time_sec} seconds"
83
88
  end
data/lib/crono/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Crono
2
- VERSION = '0.8.7.pre'
2
+ VERSION = '0.8.8.pre'
3
3
  end
@@ -1,18 +1,8 @@
1
- module Crono
2
- def self.load_cronotab
3
- cronotab_path = ENV['CRONOTAB'] || (defined?(Rails) &&
4
- File.join(Rails.root, cronotab_path))
5
- fail 'No cronotab defined' unless cronotab_path
6
- puts "Load cronotab #{cronotab_path}"
7
- require cronotab_path
8
- end
9
- end
10
-
11
1
  namespace :crono do
12
2
  desc 'Clean unused job stats from DB'
13
3
  task clean: :environment do
14
4
  Crono.scheduler = Crono::Scheduler.new
15
- Crono.load_cronotab
5
+ Crono::Cronotab.process
16
6
  current_job_ids = Crono.scheduler.jobs.map(&:job_id)
17
7
  Crono::CronoJob.where.not(job_id: current_job_ids).destroy_all
18
8
  end
@@ -20,7 +10,7 @@ namespace :crono do
20
10
  desc 'Check cronotab.rb syntax'
21
11
  task check: :environment do
22
12
  Crono.scheduler = Crono::Scheduler.new
23
- Crono.load_cronotab
13
+ Crono::Cronotab.process
24
14
  puts 'Syntax ok'
25
15
  end
26
16
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crono
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.7.pre
4
+ version: 0.8.8.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dzmitry Plashchynski
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-04-12 00:00:00.000000000 Z
11
+ date: 2015-04-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activejob
@@ -206,6 +206,7 @@ files:
206
206
  - lib/crono.rb
207
207
  - lib/crono/cli.rb
208
208
  - lib/crono/config.rb
209
+ - lib/crono/cronotab.rb
209
210
  - lib/crono/job.rb
210
211
  - lib/crono/logging.rb
211
212
  - lib/crono/orm/active_record/crono_job.rb