crono 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c146a6ca9be59f261b40b2716337e6db97962c60
4
- data.tar.gz: f0889fab37a17460d0c676dead916f0c36e315ac
3
+ metadata.gz: d44592e0e575b43cbfbc08ae70a3ba2b48fe4c56
4
+ data.tar.gz: f7155bff1058414b3772e008a497e5932e1117f1
5
5
  SHA512:
6
- metadata.gz: 5498ec9e3ef28a21fa38ffb372d6807f7e34bd4088346a6381151dabf925a26b68857b6c5d5ba74af092a2470a6afc47844875d5c700775d4843d75f0678e614
7
- data.tar.gz: f4b21a9fe445df51770cff88da34a888fa3e03666c1371b74efe45267c2c0f4d44cba76ebb3571e6e644be366da78c7b41f89114cfe3c51dae91f7c10e91dd84
6
+ metadata.gz: cc7a404b38391f25500e7ac662e06426d7bd9129c8911bc909a82262f4d231a824852b33a30f4a52ae58503c7e6ec7eacddd27a3379d1292de0bc61ab5cb5dd9
7
+ data.tar.gz: 6c101c91fc475b626baacd442725cdd7bd55b86b6e77c5d3cf3bbfa312062fa0931215ba683cb593c7ec9371972657d0968144f37e9a355f89c95f4a4adbbe40
data/Changes.md CHANGED
@@ -7,3 +7,8 @@
7
7
  ———————————
8
8
 
9
9
  - Added -e/--environment ENV option to set the daemon rails environment.
10
+
11
+ 0.5.2
12
+ ———————————
13
+
14
+ - Fix: Scheduled time now related to the last performing time.
data/README.md CHANGED
@@ -26,7 +26,7 @@ Add the following line to your application's Gemfile:
26
26
 
27
27
  gem 'crono'
28
28
 
29
- Run the bundle command to install it.
29
+ Run the `bundle` command to install it.
30
30
  After you install Crono, you can run the generator:
31
31
 
32
32
  rails generate crono:install
@@ -42,8 +42,8 @@ Now you are ready to move forward to create a job and schedule it.
42
42
  Crono can use Active Job jobs from `app/jobs/`. The only requirements is that the `perform` method should take no arguments.
43
43
 
44
44
  Here's an example of a test job:
45
- app/jobs/test_job.rb
46
45
 
46
+ # app/jobs/test_job.rb
47
47
  class TestJob < ActiveJob::Base
48
48
  def perform
49
49
  # put you scheduled code here
@@ -65,6 +65,7 @@ The ActiveJob jobs is convenient because you can use one job in both periodic an
65
65
 
66
66
  The schedule described in the configuration file `config/cronotab.rb`, that created using `crono:install` or manually. The semantic is pretty straightforward:
67
67
 
68
+ # config/cronotab.rb
68
69
  Crono.perform(TestJob).every 2.days, at: "15:30"
69
70
 
70
71
  You can schedule one job a few times, if you want a job to be performed a few times a day:
@@ -94,10 +95,15 @@ Usage: crono [options]
94
95
  ```
95
96
 
96
97
  ## Capistrano
98
+
97
99
  Use the `capistrano-crono` gem ([github](https://github.com/plashchynski/capistrano-crono/)).
98
100
 
101
+
102
+ ## Support
103
+
104
+ Feel free to create [issues](https://github.com/plashchynski/crono/issues)
105
+
106
+
99
107
  ## License
100
108
 
101
- Copyright 2015 Dzmitry Plashchynski <plashchynski@gmail.com>
102
- Licensed under the Apache License, Version 2.0
103
109
  Please see [LICENSE](https://github.com/plashchynski/crono/blob/master/LICENSE) for licensing details.
@@ -0,0 +1,15 @@
1
+ # cronotab.rb — Crono configuration example file
2
+ #
3
+ # Here you can specify periodic jobs and their schedule.
4
+ # You can specify a periodic job as a ActiveJob class in `app/jobs/`
5
+ # Actually you can use any class. The only requirement is that
6
+ # the class should implement a method `perform` without arguments.
7
+ #
8
+
9
+ class TestJob
10
+ def perform
11
+ puts "Test!"
12
+ end
13
+ end
14
+
15
+ Crono.perform(TestJob).every 5.second
data/lib/crono.rb CHANGED
@@ -4,6 +4,7 @@ end
4
4
  require "active_support/all"
5
5
  require "crono/version.rb"
6
6
  require "crono/period.rb"
7
+ require "crono/job.rb"
7
8
  require "crono/schedule.rb"
8
9
  require "crono/config.rb"
9
10
  require "crono/performer_proxy.rb"
data/lib/crono/cli.rb CHANGED
@@ -3,17 +3,15 @@ require 'optparse'
3
3
 
4
4
  module Crono
5
5
  mattr_accessor :schedule
6
+ mattr_accessor :logger
6
7
 
7
8
  class CLI
8
9
  include Singleton
9
10
  attr_accessor :config
10
- attr_accessor :schedule
11
- attr_accessor :logger
12
11
 
13
12
  def initialize
14
13
  self.config = Config.new
15
- self.schedule = Schedule.new
16
- Crono.schedule = schedule
14
+ Crono.schedule = Schedule.new
17
15
  end
18
16
 
19
17
  def run
@@ -44,12 +42,12 @@ module Crono
44
42
 
45
43
  def init_logger
46
44
  logfile = config.daemonize ? config.logfile : STDOUT
47
- self.logger = Logger.new(logfile)
45
+ Crono.logger = Logger.new(logfile)
48
46
  end
49
47
 
50
48
  def print_banner
51
- logger.info "Loading Crono #{Crono::VERSION}"
52
- logger.info "Running in #{RUBY_DESCRIPTION}"
49
+ Crono.logger.info "Loading Crono #{Crono::VERSION}"
50
+ Crono.logger.info "Running in #{RUBY_DESCRIPTION}"
53
51
  end
54
52
 
55
53
  def load_rails
@@ -60,16 +58,11 @@ module Crono
60
58
  require File.expand_path(config.cronotab)
61
59
  end
62
60
 
63
- def run_job(klass)
64
- logger.info "Perform #{klass}"
65
- Thread.new { klass.new.perform }
66
- end
67
-
68
61
  def start_working_loop
69
62
  loop do
70
- klass, time = schedule.next
71
- sleep(time - Time.now)
72
- run_job(klass)
63
+ job = Crono.schedule.next
64
+ sleep(job.next - Time.now)
65
+ job.perform
73
66
  end
74
67
  end
75
68
 
data/lib/crono/job.rb ADDED
@@ -0,0 +1,21 @@
1
+ module Crono
2
+ class Job
3
+ attr_accessor :performer
4
+ attr_accessor :period
5
+ attr_accessor :last_performed_at
6
+
7
+ def initialize(performer, period)
8
+ self.performer, self.period = performer, period
9
+ end
10
+
11
+ def next
12
+ period.next(since: last_performed_at)
13
+ end
14
+
15
+ def perform
16
+ Crono.logger.info "Perform #{performer}"
17
+ self.last_performed_at = Time.now
18
+ Thread.new { performer.new.perform }
19
+ end
20
+ end
21
+ end
@@ -6,7 +6,8 @@ module Crono
6
6
  end
7
7
 
8
8
  def every(period, *args)
9
- @schedule.add(@performer, Period.new(period, *args))
9
+ job = Job.new(@performer, Period.new(period, *args))
10
+ @schedule.add(job)
10
11
  end
11
12
  end
12
13
 
data/lib/crono/period.rb CHANGED
@@ -5,8 +5,9 @@ module Crono
5
5
  @at_hour, @at_min = parse_at(at) if at
6
6
  end
7
7
 
8
- def next
9
- @period.from_now.change({hour: @at_hour, min: @at_min}.compact)
8
+ def next(since: nil)
9
+ since ||= Time.now
10
+ @period.since(since).change({hour: @at_hour, min: @at_min}.compact)
10
11
  end
11
12
 
12
13
  def parse_at(at)
@@ -1,20 +1,22 @@
1
1
  module Crono
2
2
  class Schedule
3
+ attr_accessor :schedule
4
+
3
5
  def initialize
4
- @schedule = []
6
+ self.schedule = []
5
7
  end
6
8
 
7
- def add(peformer, period)
8
- @schedule << [peformer, period]
9
+ def add(job)
10
+ schedule << job
9
11
  end
10
12
 
11
13
  def next
12
- [queue.first[0], queue.first[1].next]
14
+ queue.first
13
15
  end
14
16
 
15
- private
17
+ private
16
18
  def queue
17
- @schedule.sort { |a,b| a[1].next <=> b[1].next }
19
+ schedule.sort { |a,b| a.next <=> b.next }
18
20
  end
19
21
  end
20
22
  end
data/lib/crono/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Crono
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.2"
3
3
  end
data/spec/cli_spec.rb CHANGED
@@ -18,13 +18,6 @@ describe Crono::CLI do
18
18
  end
19
19
  end
20
20
 
21
- describe "#run_job" do
22
- it "should run job in separate thread" do
23
- thread = cli.send(:run_job, TestJob).join
24
- expect(thread).to be_stop
25
- end
26
- end
27
-
28
21
  describe "#start_working_loop" do
29
22
  it "should start working loop"
30
23
  end
data/spec/job_spec.rb ADDED
@@ -0,0 +1,22 @@
1
+ require "spec_helper"
2
+
3
+ class TestJob
4
+ def perform;end
5
+ end
6
+
7
+ describe Crono::Job do
8
+ let(:period) { Crono::Period.new(2.day) }
9
+ let(:job) { Crono::Job.new(TestJob, period) }
10
+
11
+ it "should contain performer and period" do
12
+ expect(job.performer).to be TestJob
13
+ expect(job.period).to be period
14
+ end
15
+
16
+ describe "#perform" do
17
+ it "should run performer in separate thread" do
18
+ thread = job.perform.join
19
+ expect(thread).to be_stop
20
+ end
21
+ end
22
+ end
@@ -5,8 +5,8 @@ class TestJob
5
5
  end
6
6
 
7
7
  describe Crono::PerformerProxy do
8
- it "should add job and period to schedule" do
9
- expect(Crono.schedule).to receive(:add).with(TestJob, kind_of(Crono::Period))
8
+ it "should add job to schedule" do
9
+ expect(Crono.schedule).to receive(:add).with(kind_of(Crono::Job))
10
10
  Crono.perform(TestJob).every(2.days, at: "15:30")
11
11
  end
12
12
  end
data/spec/period_spec.rb CHANGED
@@ -30,6 +30,11 @@ describe Crono::Period do
30
30
  @period = Crono::Period.new(2.day, at: 1)
31
31
  }.to raise_error("Unknown 'at' format")
32
32
  end
33
+
34
+ it "should return time in relation to last time" do
35
+ @period = Crono::Period.new(2.day)
36
+ expect(@period.next(since: 1.day.ago)).to be_eql(1.day.from_now)
37
+ end
33
38
  end
34
39
  end
35
40
  end
@@ -5,16 +5,23 @@ class TestJob
5
5
  end
6
6
 
7
7
  describe Crono::Schedule do
8
+ before(:each) do
9
+ @schedule = Crono::Schedule.new
10
+ @jobs = [
11
+ Crono::Period.new(3.day, at: "18:55"),
12
+ Crono::Period.new(1.day, at: "15:30"),
13
+ Crono::Period.new(7.day, at: "06:05")
14
+ ].map { |period| Crono::Job.new(TestJob, period) }
15
+ @schedule.schedule = @jobs
16
+ end
17
+
8
18
  describe "#next" do
9
19
  it "should return next job in schedule" do
10
- @schedule = Crono::Schedule.new
11
- [
12
- Crono::Period.new(3.day, at: "18:55"),
13
- Crono::Period.new(1.day, at: "15:30"),
14
- Crono::Period.new(7.day, at: "06:05")
15
- ].each { |period| @schedule.add(TestJob, period) }
20
+ expect(@schedule.next).to be @jobs[1]
21
+ end
16
22
 
17
- expect(@schedule.next).to be_eql([TestJob, 1.day.from_now.change(hour: 15, min: 30)])
23
+ it "should return next based on last" do
24
+ expect(@schedule.next)
18
25
  end
19
26
  end
20
27
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crono
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dzmitry Plashchynski
@@ -114,10 +114,12 @@ files:
114
114
  - Rakefile
115
115
  - bin/crono
116
116
  - crono.gemspec
117
+ - examples/cronotab.rb
117
118
  - examples/monitrc.conf
118
119
  - lib/crono.rb
119
120
  - lib/crono/cli.rb
120
121
  - lib/crono/config.rb
122
+ - lib/crono/job.rb
121
123
  - lib/crono/performer_proxy.rb
122
124
  - lib/crono/period.rb
123
125
  - lib/crono/schedule.rb
@@ -126,6 +128,7 @@ files:
126
128
  - lib/generators/crono/install/templates/cronotab.rb.erb
127
129
  - spec/cli_spec.rb
128
130
  - spec/config_spec.rb
131
+ - spec/job_spec.rb
129
132
  - spec/performer_proxy_spec.rb
130
133
  - spec/period_spec.rb
131
134
  - spec/schedule_spec.rb