class Xronor::AWS::CloudWatchEvents

Public Class Methods

new(client: Aws::CloudWatchEvents::Client.new) click to toggle source
# File lib/xronor/aws/cloud_watch_events.rb, line 4
def initialize(client: Aws::CloudWatchEvents::Client.new)
  @client = client
end

Public Instance Methods

deregister_job(job_name) click to toggle source
# File lib/xronor/aws/cloud_watch_events.rb, line 8
def deregister_job(job_name)
  targets = @client.list_targets_by_rule(rule: job_name).targets
  @client.remove_targets(rule: job_name, ids: targets.map(&:id))
  @client.delete_rule(name: job_name)
end
list_jobs(prefix = "") click to toggle source
# File lib/xronor/aws/cloud_watch_events.rb, line 14
def list_jobs(prefix = "")
  @client.list_rules.rules.select { |rule| rule.name.start_with?(prefix) }.map(&:name)
end
register_job(job, prefix, cluster, task_definition, container, target_function_arn) click to toggle source
# File lib/xronor/aws/cloud_watch_events.rb, line 18
def register_job(job, prefix, cluster, task_definition, container, target_function_arn)
  rule_name = job.cloud_watch_rule_name(prefix)
  rule_arn = put_rule(rule_name, job.description, job.cloud_watch_schedule)
  put_target(rule_name, cluster, task_definition, container, job.command, target_function_arn)
  rule_arn
end

Private Instance Methods

generate_id() click to toggle source
# File lib/xronor/aws/cloud_watch_events.rb, line 65
def generate_id
  SecureRandom.uuid
end
generate_input_template(cluster, task_definition, container, command) click to toggle source
# File lib/xronor/aws/cloud_watch_events.rb, line 54
def generate_input_template(cluster, task_definition, container, command)
  JSON.generate({
    "resources" => '###$.resources###',
    "time" => '###$.time###',
    "cluster" => cluster,
    "task_definition" => task_definition,
    "container" => container,
    "command" => Shellwords.split(command),
  }).sub('"###$.resources###"', "<resources>").sub('"###$.time###"', "<time>")
end
put_rule(name, description, schedule) click to toggle source
# File lib/xronor/aws/cloud_watch_events.rb, line 27
def put_rule(name, description, schedule)
  @client.put_rule({
    name: name,
    description: description,
    schedule_expression: schedule,
  }).rule_arn
end
put_target(name, cluster, task_definition, container, command, target_function_arn) click to toggle source
# File lib/xronor/aws/cloud_watch_events.rb, line 35
def put_target(name, cluster, task_definition, container, command, target_function_arn)
  @client.put_targets({
    rule: name,
    targets: [
      {
        id: generate_id,
        arn: target_function_arn,
        input_transformer: {
          input_paths_map: {
            "resources" => "$.resources",
            "time" => "$.time",
          },
          input_template: generate_input_template(cluster, task_definition, container, command),
        },
      },
    ],
  })
end