cronline 0.2.0
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 +7 -0
- data/.gitignore +38 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +45 -0
- data/Rakefile +1 -0
- data/bin/console +15 -0
- data/bin/setup +7 -0
- data/cronline.gemspec +34 -0
- data/lib/cronline.rb +17 -0
- data/lib/cronline/cron.rb +44 -0
- data/lib/cronline/cron_days_of_month.rb +38 -0
- data/lib/cronline/cron_days_of_week.rb +56 -0
- data/lib/cronline/cron_field.rb +58 -0
- data/lib/cronline/cron_hours.rb +12 -0
- data/lib/cronline/cron_minutes.rb +14 -0
- data/lib/cronline/cron_months.rb +19 -0
- data/lib/cronline/cron_seconds.rb +12 -0
- data/lib/cronline/cron_years.rb +25 -0
- data/lib/cronline/simulator.rb +138 -0
- data/lib/cronline/version.rb +3 -0
- metadata +110 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a31ad50c7f38818689b8cc1408e9a37f763a1def
|
4
|
+
data.tar.gz: 19173412d249957bc9f7abbd46cde64832237e48
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a543d9a8a331c25ed01d340f0957590defa6cba13812a78d3732da05e0ce79fcc3acdc2f4f2581de2e03e27290d0b0dcaa9a564ab58172fe67374dda44fa6fa4
|
7
|
+
data.tar.gz: 03e18289c3d98ba4ca18d6a3bb9554dff21eda4c20d945898d4c207814632bb7c47840719d74c71d3a36804398248f43a893ce275746031bbbc87c8188d3ea55
|
data/.gitignore
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
## Specific to RubyMotion:
|
13
|
+
.dat*
|
14
|
+
.repl_history
|
15
|
+
build/
|
16
|
+
|
17
|
+
## Documentation cache and generated files:
|
18
|
+
/.yardoc/
|
19
|
+
/_yardoc/
|
20
|
+
/doc/
|
21
|
+
/rdoc/
|
22
|
+
|
23
|
+
## Environment normalisation:
|
24
|
+
/.bundle/
|
25
|
+
/vendor/bundle
|
26
|
+
/lib/bundler/man/
|
27
|
+
|
28
|
+
# for a library or gem, you might want to ignore these files since the code is
|
29
|
+
# intended to run in multiple environments; otherwise, check them in:
|
30
|
+
Gemfile.lock
|
31
|
+
.ruby-version
|
32
|
+
.ruby-gemset
|
33
|
+
|
34
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
35
|
+
.rvmrc
|
36
|
+
|
37
|
+
.idea
|
38
|
+
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 anton-vattay
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# CronLine
|
2
|
+
|
3
|
+
Code goes in `lib/cronline`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
Converts a cron expression into a series of instants in time.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'cronline'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install cronline
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Create a simulator. You don't have to provide any parameters but you can optionally override them using the builder. Default timezone is the local time for the system.
|
26
|
+
|
27
|
+
sim = Cronline::Simulator.builder
|
28
|
+
.set_duration(2.weeks)
|
29
|
+
.set_timezone('Eastern Time (US & Canada)')
|
30
|
+
.build
|
31
|
+
times = sim.simulate_cron('0 30 7 ? * MON,WED *')
|
32
|
+
|
33
|
+
## Development
|
34
|
+
|
35
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
36
|
+
|
37
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
1. Fork it ( https://github.com/[my-github-username]/cronline/fork )
|
42
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
43
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
44
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
45
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require 'active_support/all'
|
5
|
+
require "cronline"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start
|
data/bin/setup
ADDED
data/cronline.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cronline/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "cronline"
|
8
|
+
spec.version = Cronline::VERSION
|
9
|
+
spec.authors = ["anton-vattay"]
|
10
|
+
spec.email = ["anton.vattay@opower.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Converts a cron expression to a sequence of instants in time}
|
13
|
+
spec.description = %q{Simulates a cron expression over a range of time and produces the times which the cron is e
|
14
|
+
expected to fire.}
|
15
|
+
spec.homepage = "http://opower.com"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
19
|
+
# delete this section to allow pushing this gem to any host.
|
20
|
+
# if spec.respond_to?(:metadata)
|
21
|
+
# spec.metadata['allowed_push_host'] = "http://gemserver.va.opower.it/"
|
22
|
+
# else
|
23
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
24
|
+
# end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
27
|
+
spec.bindir = "exe"
|
28
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
spec.add_development_dependency 'bundler', '~> 1.9'
|
32
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
33
|
+
spec.add_runtime_dependency 'activesupport', '~> 4.2'
|
34
|
+
end
|
data/lib/cronline.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "cronline/version"
|
2
|
+
require "time"
|
3
|
+
require "cronline/simulator"
|
4
|
+
require "cronline/cron"
|
5
|
+
require "cronline/cron_field"
|
6
|
+
require "cronline/cron_seconds"
|
7
|
+
require "cronline/cron_minutes"
|
8
|
+
require "cronline/cron_hours"
|
9
|
+
require "cronline/cron_days_of_month"
|
10
|
+
require "cronline/cron_days_of_week"
|
11
|
+
require "cronline/cron_months"
|
12
|
+
require "cronline/cron_years"
|
13
|
+
require "active_support/all"
|
14
|
+
|
15
|
+
module Cronline
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Cronline
|
2
|
+
class Cron
|
3
|
+
attr_reader :seconds, :minutes, :hours, :days, :months, :years
|
4
|
+
|
5
|
+
@seconds
|
6
|
+
@minutes
|
7
|
+
@hours
|
8
|
+
@days
|
9
|
+
@months
|
10
|
+
@years
|
11
|
+
|
12
|
+
def initialize(cron_expression)
|
13
|
+
@seconds = CronSeconds.new(cron_expression)
|
14
|
+
@minutes = CronMinutes.new(cron_expression)
|
15
|
+
@hours = CronHours.new(cron_expression)
|
16
|
+
|
17
|
+
@months = CronMonths.new(cron_expression)
|
18
|
+
@years = CronYears.new(cron_expression)
|
19
|
+
|
20
|
+
cron_days_of_month = CronDaysOfMonth.new(cron_expression)
|
21
|
+
cron_days_of_week = CronDaysOfWeek.new(cron_expression)
|
22
|
+
if (cron_days_of_month).active? && (cron_days_of_week).active?
|
23
|
+
fail 'Only one days field is allowed'
|
24
|
+
elsif cron_days_of_week.active?
|
25
|
+
@days = cron_days_of_week
|
26
|
+
elsif cron_days_of_month.active?
|
27
|
+
@days = cron_days_of_month
|
28
|
+
else
|
29
|
+
fail 'One day field is required'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def test?(time)
|
34
|
+
@years.test?(time) && @months.test?(time) && @days.test?(time) &&
|
35
|
+
@hours.test?(time) && @minutes.test?(time) && @seconds.test?(time)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_up_to_hours?(time)
|
39
|
+
@years.test?(time) && @months.test?(time) && @days.test?(time) &&
|
40
|
+
@hours.test?(time)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Cronline
|
2
|
+
class CronDaysOfMonth < CronField
|
3
|
+
@last_day_of_month = false
|
4
|
+
|
5
|
+
def initialize(cron_expression)
|
6
|
+
@field_expression = cron_expression.split(' ')[3]
|
7
|
+
if @field_expression == 'L'
|
8
|
+
@last_day_of_month = true
|
9
|
+
@field_expression.sub!('L', '')
|
10
|
+
else
|
11
|
+
super(@field_expression, 0, 31)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def active?
|
16
|
+
@field_expression != '?'
|
17
|
+
end
|
18
|
+
|
19
|
+
def time_field(time)
|
20
|
+
time.day
|
21
|
+
end
|
22
|
+
|
23
|
+
def range(time)
|
24
|
+
if @last_day_of_month
|
25
|
+
puts @last_day_of_month
|
26
|
+
[last_day_of_month(time)]
|
27
|
+
else
|
28
|
+
super(time)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def last_day_of_month(time)
|
35
|
+
Date.new(time.year, time.month, -1).day
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Cronline
|
2
|
+
class CronDaysOfWeek < CronField
|
3
|
+
@field_expression
|
4
|
+
@last_weekday_of_month = false
|
5
|
+
|
6
|
+
def initialize(cron_expression)
|
7
|
+
@field_expression = cron_expression.split(' ')[5]
|
8
|
+
Date::ABBR_DAYNAMES.each do |abbreviation|
|
9
|
+
unless abbreviation.nil?
|
10
|
+
@field_expression.gsub!(/(#{abbreviation})/i, Date::ABBR_DAYNAMES.index(abbreviation).to_s)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
if @field_expression[-1,1] == 'L'
|
14
|
+
@last_weekday_of_month = true
|
15
|
+
@field_expression.sub!('L', '')
|
16
|
+
end
|
17
|
+
super(@field_expression, 1, 7)
|
18
|
+
end
|
19
|
+
|
20
|
+
def active?
|
21
|
+
@field_expression != '?'
|
22
|
+
end
|
23
|
+
|
24
|
+
def range(time)
|
25
|
+
if @last_weekday_of_month
|
26
|
+
[last_weekday_day_of_month(super(time)[0], time)]
|
27
|
+
else
|
28
|
+
super(time)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def test?(time)
|
33
|
+
if @last_weekday_of_month
|
34
|
+
test_last_weekday?(@range[0], time)
|
35
|
+
else
|
36
|
+
@range.include?(time.wday)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Thank you Mohit Sindhwani https://www.ruby-forum.com/topic/125970
|
41
|
+
def last_weekday_day_of_month(weekday_number, time)
|
42
|
+
last_day_of_month = Date.new(time.year, time.month, -1)
|
43
|
+
last_day_wday = ((last_day_of_month.wday + 1) - weekday_number).abs
|
44
|
+
last_day = Date.new(time.year, time.month, (last_day_of_month.day - last_day_wday))
|
45
|
+
last_day.day
|
46
|
+
end
|
47
|
+
|
48
|
+
# Thank you Mohit Sindhwani https://www.ruby-forum.com/topic/125970
|
49
|
+
def test_last_weekday?(weekday_number, time)
|
50
|
+
last_day_of_month = Date.new(time.year, time.month, -1)
|
51
|
+
last_day_wday = ((last_day_of_month.wday + 1) - weekday_number).abs
|
52
|
+
last_day = Date.new(time.year, time.month, (last_day_of_month.day - last_day_wday))
|
53
|
+
last_day.year == time.year && last_day.month == time.month && last_day.day == time.day
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Cronline
|
2
|
+
class CronField
|
3
|
+
|
4
|
+
@range
|
5
|
+
@wild = false
|
6
|
+
|
7
|
+
def self.wildcard
|
8
|
+
'*'
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(field_expression, absolute_min, absolute_max)
|
12
|
+
@absolute_min = absolute_min
|
13
|
+
@absolute_max = absolute_max
|
14
|
+
if field_expression == CronField.wildcard
|
15
|
+
#Wild
|
16
|
+
@wild = true
|
17
|
+
@range = Integer(absolute_min)..Integer(absolute_max)
|
18
|
+
elsif field_expression.include?('-')
|
19
|
+
#Range
|
20
|
+
min = Integer(field_expression.split('-')[0])
|
21
|
+
max = Integer(field_expression.split('-')[1])
|
22
|
+
@range = min..max
|
23
|
+
elsif field_expression.include?(',')
|
24
|
+
#Value list
|
25
|
+
@range = field_expression.split(',').map do |number_expression|
|
26
|
+
Integer(number_expression)
|
27
|
+
end
|
28
|
+
elsif field_expression.include?('/')
|
29
|
+
#Increment
|
30
|
+
increment_start = field_expression.split('/')[0]
|
31
|
+
increment_step = field_expression.split('/')[1]
|
32
|
+
@range = (Integer(increment_start)..absolute_max).step(Integer(increment_step))
|
33
|
+
elsif field_expression == '?'
|
34
|
+
#Disabled
|
35
|
+
@range = []
|
36
|
+
elsif field_expression[-1, 1] == 'L'
|
37
|
+
#Last
|
38
|
+
@range = [Integer(field_expression.sub!('L', ''))]
|
39
|
+
else
|
40
|
+
#Single
|
41
|
+
@range = [Integer(field_expression)]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def range(time = nil)
|
46
|
+
@range
|
47
|
+
end
|
48
|
+
|
49
|
+
def test?(time)
|
50
|
+
range(time).include?(time_field(time))
|
51
|
+
end
|
52
|
+
|
53
|
+
def time_field(time)
|
54
|
+
fail 'time_field must be implemented'
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Cronline
|
2
|
+
class CronMonths < CronField
|
3
|
+
def initialize(cron_expression)
|
4
|
+
expression = cron_expression.split(' ')[4]
|
5
|
+
Date::ABBR_MONTHNAMES.each do |abbreviation|
|
6
|
+
unless abbreviation.nil?
|
7
|
+
expression.gsub!(/(#{abbreviation})/i, Date::ABBR_MONTHNAMES.index(abbreviation).to_s)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
super(expression, 1, 12)
|
11
|
+
end
|
12
|
+
|
13
|
+
def time_field(time)
|
14
|
+
time.month
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Cronline
|
2
|
+
class CronYears < CronField
|
3
|
+
|
4
|
+
def self.absolute_min_year
|
5
|
+
1970
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.absolute_max_year
|
9
|
+
2099
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(cron_expression)
|
13
|
+
expression = cron_expression.split(' ')[6]
|
14
|
+
if expression.nil?
|
15
|
+
super(CronField.wildcard, CronYears.absolute_min_year , CronYears.absolute_max_year)
|
16
|
+
else
|
17
|
+
super(expression, CronYears.absolute_min_year , CronYears.absolute_max_year)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def time_field(time)
|
22
|
+
time.year
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
module Cronline
|
2
|
+
class Simulator
|
3
|
+
|
4
|
+
def self.default_max_ticks
|
5
|
+
1e+5
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.default_max_time_output
|
9
|
+
365
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.default_timeframe_seconds
|
13
|
+
12.month.to_i
|
14
|
+
end
|
15
|
+
|
16
|
+
class Builder
|
17
|
+
def initialize
|
18
|
+
@start_time = Time.new
|
19
|
+
@end_time = @start_time + Simulator.default_timeframe_seconds
|
20
|
+
@max_ticks = Simulator.default_max_ticks
|
21
|
+
@max_time_output = Simulator.default_max_time_output
|
22
|
+
end
|
23
|
+
|
24
|
+
def set_timezone(timezone_string)
|
25
|
+
@timezone = timezone_string
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
def set_start_time(start_time)
|
30
|
+
@start_time = start_time
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
def set_end_time(end_time)
|
35
|
+
@end_time = end_time
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
def set_duration(duration)
|
40
|
+
@end_time = @start_time + duration
|
41
|
+
self
|
42
|
+
end
|
43
|
+
|
44
|
+
def set_max_ticks(max_ticks)
|
45
|
+
@max_ticks = max_ticks
|
46
|
+
self
|
47
|
+
end
|
48
|
+
|
49
|
+
def set_max_time_output(max_time_output)
|
50
|
+
@max_time_output = max_time_output
|
51
|
+
self
|
52
|
+
end
|
53
|
+
|
54
|
+
def build
|
55
|
+
@start_time = @start_time || Time.new
|
56
|
+
Simulator.new(
|
57
|
+
@timezone || nil,
|
58
|
+
@start_time,
|
59
|
+
@end_time,
|
60
|
+
@max_ticks,
|
61
|
+
@max_time_output)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.builder
|
66
|
+
Builder.new
|
67
|
+
end
|
68
|
+
|
69
|
+
def initialize(
|
70
|
+
timezone = nil,
|
71
|
+
start_time = Time.new,
|
72
|
+
end_time = start_time + Simulator.default_timeframe_seconds,
|
73
|
+
max_ticks = Simulator.default_max_ticks,
|
74
|
+
max_time_output = Simulator.default_max_time_output)
|
75
|
+
@start_time = start_time
|
76
|
+
@end_time = end_time
|
77
|
+
@max_ticks = max_ticks
|
78
|
+
@max_time_output = max_time_output
|
79
|
+
@timezone = timezone
|
80
|
+
end
|
81
|
+
|
82
|
+
def simulate_cron(cron_expression, timezone = nil)
|
83
|
+
merged_timezone = timezone.nil? ? @timezone : timezone
|
84
|
+
@time_accumulator = []
|
85
|
+
cron = Cron.new(cron_expression)
|
86
|
+
time_increment = time_increment_size
|
87
|
+
@start_unix_time = aligned_start_time(@start_time)
|
88
|
+
@end_unix_time = @end_time.to_time.to_i
|
89
|
+
@tick = 0
|
90
|
+
while @start_unix_time <= @end_unix_time do
|
91
|
+
if stop_simulation
|
92
|
+
break
|
93
|
+
end
|
94
|
+
if merged_timezone.nil?
|
95
|
+
time = Time.at(@start_unix_time)
|
96
|
+
else
|
97
|
+
time = Time.at(@start_unix_time).in_time_zone(merged_timezone)
|
98
|
+
end
|
99
|
+
if cron.test_up_to_hours?(time)
|
100
|
+
@time_accumulator += generate_hours_and_seconds(cron, time)
|
101
|
+
end
|
102
|
+
@start_unix_time += time_increment
|
103
|
+
end
|
104
|
+
@time_accumulator
|
105
|
+
end
|
106
|
+
|
107
|
+
def generate_hours_and_seconds(cron, time)
|
108
|
+
accumulator = []
|
109
|
+
cron.minutes.range.each do |minute|
|
110
|
+
cron.seconds.range.each do |second|
|
111
|
+
@tick += 1
|
112
|
+
accumulator.push(Time.new(time.year, time.month, time.day, time.hour, minute, second))
|
113
|
+
end
|
114
|
+
end
|
115
|
+
accumulator
|
116
|
+
end
|
117
|
+
|
118
|
+
def stop_simulation
|
119
|
+
if @tick >= @max_ticks
|
120
|
+
return true
|
121
|
+
end
|
122
|
+
if @time_accumulator.size >= @max_time_output
|
123
|
+
return true
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def aligned_start_time(start_time)
|
128
|
+
#Align to day since minutes / seconds are relatively deterministic across timezones and DST...
|
129
|
+
Time.new(start_time.year, start_time.month, start_time.day, start_time.hour).to_i
|
130
|
+
end
|
131
|
+
|
132
|
+
def time_increment_size
|
133
|
+
#Skip seconds and minutes since we will generate these
|
134
|
+
1.hours.to_i
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
138
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cronline
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- anton-vattay
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activesupport
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4.2'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '4.2'
|
55
|
+
description: |-
|
56
|
+
Simulates a cron expression over a range of time and produces the times which the cron is e
|
57
|
+
expected to fire.
|
58
|
+
email:
|
59
|
+
- anton.vattay@opower.com
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- ".rspec"
|
66
|
+
- ".travis.yml"
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- cronline.gemspec
|
74
|
+
- lib/cronline.rb
|
75
|
+
- lib/cronline/cron.rb
|
76
|
+
- lib/cronline/cron_days_of_month.rb
|
77
|
+
- lib/cronline/cron_days_of_week.rb
|
78
|
+
- lib/cronline/cron_field.rb
|
79
|
+
- lib/cronline/cron_hours.rb
|
80
|
+
- lib/cronline/cron_minutes.rb
|
81
|
+
- lib/cronline/cron_months.rb
|
82
|
+
- lib/cronline/cron_seconds.rb
|
83
|
+
- lib/cronline/cron_years.rb
|
84
|
+
- lib/cronline/simulator.rb
|
85
|
+
- lib/cronline/version.rb
|
86
|
+
homepage: http://opower.com
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.4.6
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: Converts a cron expression to a sequence of instants in time
|
110
|
+
test_files: []
|