cron_config_parser 0.1.2 → 0.1.3

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
  SHA256:
3
- metadata.gz: 895da1f251f953d6815a2bb37ade3974189abbef9e6215c80de705b7164e6874
4
- data.tar.gz: 1d1bcd7003feaf1453b86d87d96ff89126c2214d8c2218cb492e383e3b87d5cd
3
+ metadata.gz: dbc1edeff71f699025ba9d6821319ccf0fb7070b6162d08359f6f80996672dca
4
+ data.tar.gz: 86ddd6f709afa0a42a935ad1ac2fe7aa46643ee75b0c47adb1fe3b7f394d73e6
5
5
  SHA512:
6
- metadata.gz: cbd0de9b030fe9134482ea5edc26fe82a87204572bf6fcdd54d9071476c1b8ee1388512236ed20c8ab4372dddcfeedf0102865b90a7357c2d5346b31822e2726
7
- data.tar.gz: 9fafca4d6b2d9d03f716adf58e729fff7113009f0d1fc7ddd302d9ee3ad5dd040ddaa667e53c544a894821c4c452c867f3340a22bf7d91476df650699ae2c09d
6
+ metadata.gz: c047af12dbc736dec3ea0df5df321da98eef3d3ce77c48072a779193fa6354c61abd47b04580417773ae2a892ed46893d0b2d120224674af606cc7acc3ae02c1
7
+ data.tar.gz: fa4b863a04e751653d8237e407d789645203ca441219baf32632d64b416936db3e5beadb5075c2a6ab09fda296af8b9d6564bc421f7a8526960c07a350984330
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cron_config_parser (0.1.2)
4
+ cron_config_parser (0.1.3)
5
5
  activesupport (~> 5.2)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -19,10 +19,10 @@ Or install it yourself as:
19
19
 
20
20
  ## Usage
21
21
 
22
- You can parse the cron configuration for readability with `CronConfigParser::Parser.call`.
22
+ You can parse the cron configuration for readability with `CronConfigParser.call`.
23
23
 
24
24
  ``` ruby
25
- CronConfigParser::Parser.call('00 5 * * * Asia/Tokyo')
25
+ CronConfigParser.call('00 5 * * * Asia/Tokyo')
26
26
  => #<CronConfigParser::CronConfig:0x00007fa4f492e820
27
27
  @days=["*"],
28
28
  @hours=["5"],
@@ -36,7 +36,7 @@ enable check configured properties.
36
36
 
37
37
  ``` ruby
38
38
  # return false if configured nil or '*'
39
- config = CronConfigParser::Parser.call('00 5 * * * Asia/Tokyo')
39
+ config = CronConfigParser.call('00 5 * * * Asia/Tokyo')
40
40
  config.minutes_configured?
41
41
  => true
42
42
  config.days_configured?
@@ -46,9 +46,17 @@ config.days_configured?
46
46
  enable check next execute time.
47
47
 
48
48
  ``` ruby
49
- config = CronConfigParser::Parser.call('00 5 * * * Asia/Tokyo')
49
+ config = CronConfigParser.call('00 5 * * * Asia/Tokyo')
50
50
  config.next_execute_at
51
51
  => 2019-05-23 05:00:00 +0900
52
+ config.execute_schedule(execute_conut: 5, annotation: 'DailyJob')
53
+ =>
54
+ [{:annotation=>"DailyJob", :execute_at=>2019-05-23 05:00:00 +0900},
55
+ {:annotation=>"DailyJob", :execute_at=>2019-05-24 05:00:00 +0900},
56
+ {:annotation=>"DailyJob", :execute_at=>2019-05-25 05:00:00 +0900},
57
+ {:annotation=>"DailyJob", :execute_at=>2019-05-26 05:00:00 +0900},
58
+ {:annotation=>"DailyJob", :execute_at=>2019-05-27 05:00:00 +0900}]
59
+ =>
52
60
  ```
53
61
 
54
62
  This gem check simple validation when CronConfigParser::CronConfig object initialize.
@@ -56,15 +64,15 @@ If the config is invalid, Config::SyntaxError or Config::RequiredError is raised
56
64
 
57
65
  ``` ruby
58
66
  # not configured require property.
59
- CronConfigParser::Parser.call('00 5,13 * * ')
67
+ CronConfigParser.call('00 5,13 * * ')
60
68
  => CronConfigParser::ConfigRequiredError
61
69
 
62
70
  # configured invalid property.
63
- CronConfigParser::Parser.call('00 5,a * * * Asia/Tokyo')
71
+ CronConfigParser.call('00 5,a * * * Asia/Tokyo')
64
72
  => CronConfigParser::ConfigSyntaxError
65
73
 
66
74
  # this check is Invalidationable.
67
- CronConfigParser::Parser.call('00 5,a * * * Asia/Tokyo', validation: false)
75
+ CronConfigParser.call('00 5,a * * * Asia/Tokyo', validation: false)
68
76
  => #<CronConfigParser::CronConfig:0x00007fcedf09cdf0
69
77
  @days=["*"],
70
78
  @hours=["5", "a"],
@@ -1,3 +1,3 @@
1
1
  module CronConfigParser
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -2,6 +2,12 @@ require "cron_config_parser/version"
2
2
  require 'active_support/all'
3
3
 
4
4
  module CronConfigParser
5
+ def self.call(cron_config, validation: true)
6
+ Parser.call(cron_config, validation: true)
7
+ end
8
+
9
+ private
10
+
5
11
  class Parser
6
12
  def self.call(cron_config, validation: true)
7
13
  new(cron_config, validation: validation).call
@@ -33,6 +39,17 @@ module CronConfigParser
33
39
  attr_accessor :minutes, :hours, :days, :months, :wdays, :timezone
34
40
 
35
41
  def next_execute_at(basis_datetime: Time.current)
42
+ calc_next_execute_at(basis_datetime)
43
+ end
44
+
45
+ def execute_schedule(basis_datetime: Time.current, execute_count: 1, annotation: '')
46
+ (1..execute_count).map do
47
+ basis_datetime = calc_next_execute_at(basis_datetime)
48
+ { annotation: annotation, execute_at: basis_datetime }
49
+ end
50
+ end
51
+
52
+ def calc_next_execute_at(basis_datetime)
36
53
  ExecuteAtCalculator.new(cron_config: self, basis_datetime: basis_datetime).call
37
54
  end
38
55
 
@@ -107,7 +124,7 @@ module CronConfigParser
107
124
  WDAYS = %i[sunday monday tuesday wednesday thursday friday saturday].freeze
108
125
 
109
126
  def initialize(cron_config:, basis_datetime:)
110
- @cron_config = cron_config
127
+ @cron_config = cron_config.dup
111
128
  @basis_datetime = basis_datetime
112
129
  @execute_at = basis_datetime
113
130
  prepare_cron_config
@@ -164,35 +181,62 @@ module CronConfigParser
164
181
 
165
182
  def next_hour
166
183
  return unless cron_config.hours_configured?
184
+ # reset hour if configured cron hour
185
+ @execute_at = @execute_at.change(hour: basis_datetime.hour) if @execute_at.hour >= basis_datetime.hour
167
186
  next_hour = cron_config.hours.select { |config_hour| config_hour > execute_at.hour }.first
168
- @execute_at = change_to_property_and_move_up(property: next_hour, property_sym: :hours)
169
- # reset minute when execute in freture and not configured minute
170
- @execute_at = @execute_at.change(min: 0) unless cron_config.minutes_configured?
187
+ check_hash = { hour: next_hour.presence || cron_config.hours[0], min: execute_at.min }
188
+ # Do not change the time if it is a future time or other than the specified value
189
+ if cron_config.hours.exclude?(execute_at.hour) || changed_basis_datetime_future?(check_hash)
190
+ @execute_at = change_to_property_and_move_up(property: next_hour, property_sym: :hours)
191
+ # reset minute when execute in freture and not configured minute
192
+ @execute_at = @execute_at.change(min: 0) unless cron_config.minutes_configured?
193
+ end
171
194
  end
172
195
 
173
196
  def next_day
174
197
  return unless cron_config.days_configured?
175
- next_day = cron_config.days.select { |config_day| config_day > execute_at.day }.first
176
- @execute_at = change_to_property_and_move_up(property: next_day, property_sym: :days)
177
- # reset minute when execute in freture and not configured minute
178
- @execute_at = @execute_at.change(min: 0) unless cron_config.minutes_configured?
198
+ # reset hour if configured cron hour
199
+ @execute_at = @execute_at.change(hour: basis_datetime.hour) if @execute_at.hour >= basis_datetime.hour
200
+ next_day = cron_config.days.select { |config_day| config_day >= execute_at.day }.first
201
+ check_hash = { day: next_day.presence || cron_config.days[0], hour: execute_at.hour, min: execute_at.min }
202
+ # Do not change the time if it is a future time or other than the specified value
203
+ if cron_config.days.exclude?(execute_at.day) || changed_basis_datetime_future?(check_hash)
204
+ @execute_at = change_to_property_and_move_up(property: next_day, property_sym: :days)
205
+ # reset minute when execute in freture and not configured minute, hour
206
+ @execute_at = @execute_at.change(min: 0) unless cron_config.minutes_configured?
207
+ @execute_at = @execute_at.change(hour: 0) unless cron_config.minutes_configured?
208
+ end
179
209
  end
180
210
 
181
211
  def next_wday
182
212
  return unless cron_config.wdays_configured?
213
+ @execute_at = @execute_at.change(hour: basis_datetime.hour) if @execute_at.hour >= basis_datetime.hour
183
214
  next_wday = cron_config.wdays.select { |config_wday| config_wday > execute_at.wday }.first
184
215
  next_wday_sym = next_wday ? WDAYS[next_wday] : WDAYS[cron_config.wdays.first]
185
216
  @execute_at = execute_at.next_occurring(next_wday_sym)
186
- # reset minute when execute in freture and not configured minute
217
+ # reset minute when execute in freture and not configured minute, hour
187
218
  @execute_at = @execute_at.change(min: 0) unless cron_config.minutes_configured?
219
+ @execute_at = @execute_at.change(hour: 0) unless cron_config.minutes_configured?
188
220
  end
189
221
 
190
222
  def next_month
191
223
  return unless cron_config.months_configured?
192
- next_month = cron_config.months.select { |config_month| config_month > execute_at.month }.first
193
- @execute_at = change_to_property_and_move_up(property: next_month, property_sym: :months)
194
- # reset minute when execute in freture and not configured minute
195
- @execute_at = @execute_at.change(min: 0) unless cron_config.minutes_configured?
224
+ @execute_at = @execute_at.change(hour: basis_datetime.hour) if @execute_at.hour >= basis_datetime.hour
225
+ next_month = cron_config.months.select { |config_month| config_month >= execute_at.month }.first
226
+ check_next_month = next_month.presence || cron_config.months[0]
227
+ check_hash = { month: check_next_month, day: execute_at.day, hour: execute_at.hour, min: execute_at.min }
228
+ # Do not change the time if it is a future time or other than the specified value
229
+ if cron_config.months.exclude?(execute_at.month) || changed_basis_datetime_future?(check_hash)
230
+ @execute_at = change_to_property_and_move_up(property: next_month, property_sym: :months)
231
+ # reset minute when execute in freture and not configured minute, hour, day
232
+ @execute_at = @execute_at.change(min: 0) unless cron_config.minutes_configured?
233
+ @execute_at = @execute_at.change(hour: 0) unless cron_config.hours_configured?
234
+ @execute_at = @execute_at.change(day: 1) unless cron_config.days_configured?
235
+ end
236
+ end
237
+
238
+ def changed_basis_datetime_future?(check_hash)
239
+ execute_at <= basis_datetime.change(check_hash)
196
240
  end
197
241
 
198
242
  def change_to_property_and_move_up(property:, property_sym:)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cron_config_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Madogiwa
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-21 00:00:00.000000000 Z
11
+ date: 2019-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport