Class: ElasticWhenever::Task::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_whenever/task/rule.rb

Defined Under Namespace

Classes: UnsupportedOptionException

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#expressionObject (readonly)

Returns the value of attribute expression.



5
6
7
# File 'lib/elastic_whenever/task/rule.rb', line 5

def expression
  @expression
end

#nameObject (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) ⇒ 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.expression, task.commands),
    expression: task.expression
  )
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

Instance Method Details

#createObject



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

#deleteObject



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