Class: ElasticWhenever::Task::Rule
- Inherits:
-
Object
- Object
- ElasticWhenever::Task::Rule
- Defined in:
- lib/elastic_whenever/task/rule.rb
Defined Under Namespace
Classes: UnsupportedOptionException
Instance Attribute Summary collapse
-
#expression ⇒ Object
readonly
Returns the value of attribute expression.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .convert(option, task, chronic_options) ⇒ Object
- .fetch(option) ⇒ Object
- .rule_name(identifier, commands) ⇒ Object
- .schedule_expression(frequency, options, chronic_options) ⇒ Object
Instance Method Summary collapse
- #create ⇒ Object
- #delete ⇒ Object
-
#initialize(option, name:, expression:) ⇒ Rule
constructor
A new instance of Rule.
Constructor Details
#initialize(option, name:, expression:) ⇒ Rule
Returns a new instance of Rule.
28 29 30 31 32 |
# File 'lib/elastic_whenever/task/rule.rb', line 28 def initialize(option, name:, expression:) @name = name @expression = expression @client = Aws::CloudWatchEvents::Client.new(option.aws_config) end |
Instance Attribute Details
#expression ⇒ Object (readonly)
Returns the value of attribute expression.
5 6 7 |
# File 'lib/elastic_whenever/task/rule.rb', line 5 def expression @expression end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/elastic_whenever/task/rule.rb', line 4 def name @name end |
Class Method Details
.convert(option, task, chronic_options) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/elastic_whenever/task/rule.rb', line 20 def self.convert(option, task, ) self.new( option, name: rule_name(option.identifier, task.commands), expression: schedule_expression(task.frequency, task., ) ) end |
.fetch(option) ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/elastic_whenever/task/rule.rb', line 9 def self.fetch(option) client = Aws::CloudWatchEvents::Client.new(option.aws_config) client.list_rules(name_prefix: option.identifier).rules.map do |rule| self.new( option, name: rule.name, expression: rule.schedule_expression, ) end end |
.rule_name(identifier, commands) ⇒ Object
50 51 52 |
# File 'lib/elastic_whenever/task/rule.rb', line 50 def self.rule_name(identifier, commands) "#{identifier}_#{Digest::SHA1.hexdigest(commands.map { |command| command.join("-") }.join("-"))}" end |
.schedule_expression(frequency, options, chronic_options) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/elastic_whenever/task/rule.rb', line 54 def self.schedule_expression(frequency, , ) time = Chronic.parse([:at], ) || Time.new(2017, 12, 1, 0, 0, 0) case frequency when :hour "cron(#{time.min} * * * ? *)" when :day "cron(#{time.min} #{time.hour} * * ? *)" when :month "cron(#{time.min} #{time.hour} #{time.day} * ? *)" when :year "cron(#{time.min} #{time.hour} #{time.day} #{time.month} ? *)" when :sunday "cron(#{time.min} #{time.hour} ? * 1 *)" when :monday "cron(#{time.min} #{time.hour} ? * 2 *)" when :tuesday "cron(#{time.min} #{time.hour} ? * 3 *)" when :wednesday "cron(#{time.min} #{time.hour} ? * 4 *)" when :thursday "cron(#{time.min} #{time.hour} ? * 5 *)" when :friday "cron(#{time.min} #{time.hour} ? * 6 *)" when :saturday "cron(#{time.min} #{time.hour} ? * 7 *)" when :weekend "cron(#{time.min} #{time.hour} ? * 1,7 *)" when :weekday "cron(#{time.min} #{time.hour} ? * 2-6 *)" # cron syntax when /^((\*?[\d\/,\-]*)\s*){5}$/ min, hour, day, mon, week, year = frequency.split(" ") # You can't specify the Day-of-month and Day-of-week fields in the same Cron expression. # If you specify a value in one of the fields, you must use a ? (question mark) in the other. week.gsub!("*", "?") if day != "?" day.gsub!("*", "?") if week != "?" # cron syntax: sunday -> 0 # scheduled expression: sunday -> 1 week.gsub!(/(\d)/) { (Integer($1) + 1) % 7 } year = year || "*" "cron(#{min} #{hour} #{day} #{mon} #{week} #{year})" # schedule expression syntax when /^((\*?\??L?W?[\d\/,\-]*)\s*){6}$/ "cron(#{frequency})" else raise UnsupportedOptionException.new("`#{frequency}` is not supported option. Ignore this task.") end end |
Instance Method Details
#create ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/elastic_whenever/task/rule.rb', line 34 def create client.put_rule( name: name, schedule_expression: expression, state: "ENABLED", ) end |
#delete ⇒ Object
42 43 44 45 46 |
# File 'lib/elastic_whenever/task/rule.rb', line 42 def delete targets = client.list_targets_by_rule(rule: name).targets client.remove_targets(rule: name, ids: targets.map(&:id)) unless targets.empty? client.delete_rule(name: name) end |