chrono 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +7 -0
- data/CHANGELOG.md +5 -0
- data/README.md +20 -7
- data/Rakefile +3 -0
- data/chrono.gemspec +3 -1
- data/lib/chrono/fields/base.rb +0 -4
- data/lib/chrono/next_time.rb +0 -8
- data/lib/chrono/trigger.rb +37 -0
- data/lib/chrono/version.rb +1 -1
- data/lib/chrono.rb +1 -0
- data/spec/chrono/iterator_spec.rb +2 -0
- data/spec/chrono/trigger_spec.rb +40 -0
- data/spec/spec_helper.rb +9 -0
- metadata +39 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 31bceadc7e5050d3837628b9388d98b748367baa
|
4
|
+
data.tar.gz: c026bae471b3daacadd4ae5d328238f5ccdf51b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b161f9fcd7187abea95639062772b8979d343b714fce640a828029839951290f5e70e7ab3e035294f6ad8d9ecd654a7e17a1dcad128a6065a854cd3f4a89a362
|
7
|
+
data.tar.gz: a9797e3245d9a7700be75855526e963e723245d002a1954dc93c1e6a2e473b110b5dcb07c4949c6f8b12eca92c907cda767da264caa8da4de2d19d59ef3dff28
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -1,20 +1,33 @@
|
|
1
|
-
# Chrono
|
1
|
+
# Chrono [![Build Status](https://travis-ci.org/r7kamura/chrono.png)](https://travis-ci.org/r7kamura/chrono) [![Code Climate](https://codeclimate.com/github/r7kamura/chrono.png)](https://codeclimate.com/github/r7kamura/chrono) [![Coverage Status](https://coveralls.io/repos/r7kamura/chrono/badge.png?branch=master&cache-version=1)](https://coveralls.io/r/r7kamura/chrono?branch=master)
|
2
|
+
|
2
3
|
Provides a chain of logics about chronology.
|
3
4
|
|
4
|
-
##
|
5
|
-
|
5
|
+
## Trigger
|
6
|
+
Waits till scheduled time and then triggers a given job. `#run` is a periodic version of `#once`.
|
6
7
|
|
7
8
|
```ruby
|
8
|
-
|
9
|
+
# Trigger
|
10
|
+
trigger = Chrono::Trigger.new("30 * * * *") { Time.now }
|
11
|
+
trigger.once #=> 2000-01-01 00:30:00
|
12
|
+
trigger.run #=> 2000-01-01 01:30:00
|
13
|
+
#=> 2000-01-01 02:30:00
|
14
|
+
#=> 2000-01-01 03:30:00
|
15
|
+
#=> ...
|
16
|
+
```
|
9
17
|
|
18
|
+
## Iterator
|
19
|
+
Parses cron syntax and determines next scheduled run.
|
20
|
+
|
21
|
+
```ruby
|
10
22
|
iterator = Chrono::Iterator.new("30 * * * *")
|
11
23
|
iterator.next #=> 2000-01-01 00:30:00
|
12
|
-
iterator.next #=> 2000-01-
|
13
|
-
iterator.next #=> 2000-01-
|
24
|
+
iterator.next #=> 2000-01-01 01:30:00
|
25
|
+
iterator.next #=> 2000-01-01 02:30:00
|
14
26
|
```
|
15
27
|
|
28
|
+
## Syntax
|
16
29
|
The following syntax is supported.
|
17
|
-
See [examples](https://github.com/r7kamura/chrono/blob/master/spec/chrono/iterator_spec.rb)
|
30
|
+
See the [examples](https://github.com/r7kamura/chrono/blob/master/spec/chrono/iterator_spec.rb)
|
18
31
|
for more details.
|
19
32
|
|
20
33
|
* (*) Asterisk
|
data/Rakefile
CHANGED
data/chrono.gemspec
CHANGED
@@ -17,7 +17,9 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.require_paths = ["lib"]
|
18
18
|
|
19
19
|
spec.add_dependency "activesupport"
|
20
|
-
spec.add_development_dependency "bundler"
|
20
|
+
spec.add_development_dependency "bundler"
|
21
|
+
spec.add_development_dependency "coveralls"
|
21
22
|
spec.add_development_dependency "rake"
|
22
23
|
spec.add_development_dependency "rspec", "2.14.1"
|
24
|
+
spec.add_development_dependency "simplecov"
|
23
25
|
end
|
data/lib/chrono/fields/base.rb
CHANGED
data/lib/chrono/next_time.rb
CHANGED
@@ -38,10 +38,6 @@ module Chrono
|
|
38
38
|
@time ||= now + 1.minute
|
39
39
|
end
|
40
40
|
|
41
|
-
def fields
|
42
|
-
@fields ||= source.split(" ")
|
43
|
-
end
|
44
|
-
|
45
41
|
def schedule
|
46
42
|
@schedule ||= Schedule.new(source)
|
47
43
|
end
|
@@ -74,10 +70,6 @@ module Chrono
|
|
74
70
|
self.time = time.tomorrow.at_beginning_of_day
|
75
71
|
end
|
76
72
|
|
77
|
-
def carry_wday
|
78
|
-
self.time = time.next_week.at_beginning_of_week
|
79
|
-
end
|
80
|
-
|
81
73
|
def carry_hour
|
82
74
|
self.time = (time + 1.hour).at_beginning_of_hour
|
83
75
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Chrono
|
2
|
+
class Trigger
|
3
|
+
attr_reader :block, :source
|
4
|
+
|
5
|
+
def initialize(source, &block)
|
6
|
+
@source = source
|
7
|
+
@block = block || -> {}
|
8
|
+
end
|
9
|
+
|
10
|
+
def once
|
11
|
+
wait
|
12
|
+
call
|
13
|
+
end
|
14
|
+
|
15
|
+
def run
|
16
|
+
loop { once }
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def call
|
22
|
+
block.call
|
23
|
+
end
|
24
|
+
|
25
|
+
def iterator
|
26
|
+
@iterator ||= Iterator.new(source)
|
27
|
+
end
|
28
|
+
|
29
|
+
def wait
|
30
|
+
sleep(period)
|
31
|
+
end
|
32
|
+
|
33
|
+
def period
|
34
|
+
iterator.next - Time.now
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/chrono/version.rb
CHANGED
data/lib/chrono.rb
CHANGED
@@ -8,6 +8,8 @@ describe Chrono::Iterator do
|
|
8
8
|
"2000-01-01 23:59:00", "2000-01-02 00:00:00", "* * * * *",
|
9
9
|
"2000-01-31 23:59:00", "2000-02-01 00:00:00", "* * * * *",
|
10
10
|
"2000-12-31 23:59:00", "2001-01-01 00:00:00", "* * * * *",
|
11
|
+
"2000-01-01 00:00:00", "2000-01-02 00:00:00", "* * 2 * *",
|
12
|
+
"2000-01-01 00:00:00", "2000-02-01 00:00:00", "* * * 2 *",
|
11
13
|
"2000-01-01 00:00:00", "2000-01-01 00:15:00", "*/15 * * * *",
|
12
14
|
"2000-01-01 00:01:00", "2000-01-01 00:15:00", "*/15 * * * *",
|
13
15
|
"2000-01-01 00:00:00", "2000-01-01 00:31:00", "*/31 * * * *",
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Chrono::Trigger do
|
4
|
+
let(:trigger) do
|
5
|
+
described_class.new(source, &block)
|
6
|
+
end
|
7
|
+
|
8
|
+
let(:source) do
|
9
|
+
"* * * * *"
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:block) do
|
13
|
+
-> {}
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#once" do
|
17
|
+
it "waits till scheduled time and then triggers a given job only once" do
|
18
|
+
block.should_receive(:call)
|
19
|
+
trigger.should_receive(:sleep)
|
20
|
+
trigger.once
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Stub Trigger#loop behavior to avoid blocking main process.
|
25
|
+
describe "#run" do
|
26
|
+
before do
|
27
|
+
trigger.stub(:loop) do |&block|
|
28
|
+
2.times do
|
29
|
+
block.call
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it "waits till scheduled time and then triggers a given job periodically" do
|
35
|
+
block.should_receive(:call).twice
|
36
|
+
trigger.should_receive(:sleep).twice
|
37
|
+
trigger.run
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
require "simplecov"
|
2
|
+
require "coveralls"
|
3
|
+
|
4
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
5
|
+
SimpleCov::Formatter::HTMLFormatter,
|
6
|
+
Coveralls::SimpleCov::Formatter
|
7
|
+
]
|
8
|
+
SimpleCov.start
|
9
|
+
|
1
10
|
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
|
2
11
|
require "chrono"
|
3
12
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chrono
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryo Nakamura
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -28,16 +28,30 @@ dependencies:
|
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
32
39
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: coveralls
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
34
48
|
type: :development
|
35
49
|
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
38
|
-
- - "
|
52
|
+
- - ">="
|
39
53
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: rake
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,6 +80,20 @@ dependencies:
|
|
66
80
|
- - '='
|
67
81
|
- !ruby/object:Gem::Version
|
68
82
|
version: 2.14.1
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
69
97
|
description:
|
70
98
|
email:
|
71
99
|
- r7kamura@gmail.com
|
@@ -74,6 +102,8 @@ extensions: []
|
|
74
102
|
extra_rdoc_files: []
|
75
103
|
files:
|
76
104
|
- ".gitignore"
|
105
|
+
- ".travis.yml"
|
106
|
+
- CHANGELOG.md
|
77
107
|
- Gemfile
|
78
108
|
- LICENSE.txt
|
79
109
|
- README.md
|
@@ -89,8 +119,10 @@ files:
|
|
89
119
|
- lib/chrono/iterator.rb
|
90
120
|
- lib/chrono/next_time.rb
|
91
121
|
- lib/chrono/schedule.rb
|
122
|
+
- lib/chrono/trigger.rb
|
92
123
|
- lib/chrono/version.rb
|
93
124
|
- spec/chrono/iterator_spec.rb
|
125
|
+
- spec/chrono/trigger_spec.rb
|
94
126
|
- spec/spec_helper.rb
|
95
127
|
homepage: https://github.com/r7kamura/chrono
|
96
128
|
licenses:
|
@@ -118,4 +150,5 @@ specification_version: 4
|
|
118
150
|
summary: Provides a chain of logics about chronology.
|
119
151
|
test_files:
|
120
152
|
- spec/chrono/iterator_spec.rb
|
153
|
+
- spec/chrono/trigger_spec.rb
|
121
154
|
- spec/spec_helper.rb
|