Skip to content

Commit

Permalink
Merge pull request #63 from crgstar/main
Browse files Browse the repository at this point in the history
cron のバリデーションを強化、cron の設定改善
  • Loading branch information
crgstar authored Jan 17, 2024
2 parents 53bd750 + 1c65f81 commit b33f14a
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 13 deletions.
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.3.7
* Bizside::CronValidator
** バリデーションを強化
* Bizside::JobUtils
** add_cron_to と add_cron で cron を設定する際 blocking: true オプションを自動で付与

## 2.3.6
* Bizside::LogAnalyzer を削除

Expand Down
16 changes: 10 additions & 6 deletions bizside_test_app/test/lib/bizside/cron_validator_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'

class Bizside::GengouTest < ActiveSupport::TestCase
class Bizside::CronValidatorTest < ActiveSupport::TestCase

def test_good
[
Expand All @@ -10,12 +10,13 @@ def test_good
'59 23 31 12 7',
'0,15 * * * *',
'0-30 * * * *',
'0/120 * * * *',
'0/59 * * * *',
'0,10-20 * * * *',
'0,10-20/2 * * * *'
'0,10-20/2 * * * *',
'* 1,3,5,7,9,11,13,15,17,19,21,23 * * *',
].each do |line|
validator = Bizside::CronValidator.new(line)
assert validator.valid?
assert validator.valid?, line
end
end

Expand All @@ -25,17 +26,20 @@ def test_bad
'60 * * * *',
'* -1 * * *',
'* 24 * * *',
'* */24 * * *',
'* */0 * * *',
'* * 0 * *',
'* * 32 * *',
'* * * 0 *',
'* * * 13 *',
'* * * * -1',
'* * * * 8',
'a * * * *',
''
'',
'* 1,3,5,7,9,11,13,15,17,19,21,23,25 * * *',
].each do |line|
validator = Bizside::CronValidator.new(line)
assert ! validator.valid?
assert ! validator.valid?, line
end
end

Expand Down
11 changes: 11 additions & 0 deletions bizside_test_app/test/lib/bizside/job_utils_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,15 @@ def test_delayed_except
end
end
end

def test_set_cron_options
cronline = "*/59 * * * *"
expected = [cronline, { "blocking" => true }]
assert_equal expected, JobUtils.set_cron_options(cronline)
assert_equal expected, JobUtils.set_cron_options([cronline])
assert_equal expected, JobUtils.set_cron_options([cronline, { blocking: true }])
assert_equal expected, JobUtils.set_cron_options([cronline, { "blocking" => true }])
assert_equal expected, JobUtils.set_cron_options([cronline, { "blocking" => false }])
end

end
28 changes: 23 additions & 5 deletions lib/bizside/cron_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def valid?
def valid_#{attr}?
valid_format?(@#{attr}) &&
valid_range?(MIN_#{attr.upcase}, MAX_#{attr.upcase}, @#{attr})
valid_range_and_step?(MIN_#{attr.upcase}, MAX_#{attr.upcase}, @#{attr})
end
EOS
end
Expand All @@ -41,22 +41,40 @@ def valid_format?(value)
value =~ /\A(\*(\/\d+)?|\d+(,\d+)*(-\d+)*(\/\d+)*)\Z/
end

def valid_range_and_step?(min, max, value)
valid_range?(min, max, value) && valid_step?(min, max, value)
end

def valid_range?(min, max, value)
validate_values = remove_to_ignore_value(value).split(",")
validate_values.reject do |v|
range_values = get_range_value(value).split(",")
range_values.reject do |v|
if v.include?("-")
range_v = v.split("-")
(min..max).include?(range_v[0].to_i) &&
(min..max).include?(range_v[1].to_i) &&
range_v[0].to_i < range_v[1].to_i
else
(min..max).include? v.to_i
(min..max).include?(v.to_i)
end
end.empty?
end

def remove_to_ignore_value(value)
def valid_step?(min, max, value)
v = get_step_value(value)
return true if v.empty?

v != '*' &&
v.to_i != 0 &&
(min..max).include?(v.to_i)
end

def get_range_value(value)
value.gsub(/(\/\d*|\*)/, "")
end

def get_step_value(value)
return "" unless value.include?('/')
value.gsub(/.*\/(.*)/) { $1 }
end
end
end
18 changes: 17 additions & 1 deletion lib/bizside/job_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,11 @@ def self.add_cron_to(queue, name, job_type, cron, *args)
if cronline.to_s.strip.empty?
return
elsif CronValidator.new(cronline).valid?
new_cron = set_cron_options(cron)

config = {
:class => job_type,
:cron => cron,
:cron => new_cron,
:args => args,
:persist => true
}
Expand Down Expand Up @@ -404,5 +406,19 @@ def self.do_perform_and_hooks_instantly(klass, log_message, *args)
end
private_class_method :do_perform_and_hooks_instantly

def self.set_cron_options(cron)
ret = Array(cron)
if ret.size > 1
opts = ret.second
opts ||= {}
opts = opts.with_indifferent_access
opts = opts.merge(blocking: true)
else
opts = { blocking: true }.with_indifferent_access
end
ret[1] = opts
ret
end

end
end
2 changes: 1 addition & 1 deletion lib/bizside/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Bizside
VERSION = '2.3.6'
VERSION = '2.3.7'
end

0 comments on commit b33f14a

Please sign in to comment.