Class: LambdaWhenever::EventBridgeScheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/lambda_whenever/event_bridge_scheduler.rb

Overview

The EventBridgeScheduler class is responsible for managing schedules in AWS EventBridge.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, timezone = "UTC") ⇒ EventBridgeScheduler

Returns a new instance of EventBridgeScheduler.



8
9
10
11
# File 'lib/lambda_whenever/event_bridge_scheduler.rb', line 8

def initialize(client, timezone = "UTC")
  @scheduler_client = client
  @timezone = timezone
end

Instance Attribute Details

#timezoneObject (readonly)

Returns the value of attribute timezone.



6
7
8
# File 'lib/lambda_whenever/event_bridge_scheduler.rb', line 6

def timezone
  @timezone
end

Instance Method Details

#clean_up_schedules(schedule_group) ⇒ Object



103
104
105
106
107
108
# File 'lib/lambda_whenever/event_bridge_scheduler.rb', line 103

def clean_up_schedules(schedule_group)
  response = @scheduler_client.list_schedules({ group_name: schedule_group })
  response.schedules.each do |schedule|
    delete_schedule(schedule.name, schedule_group)
  end
end

#create_schedule(target, option) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/lambda_whenever/event_bridge_scheduler.rb', line 73

def create_schedule(target, option)
  task = target.task
  @scheduler_client.create_schedule({
                                      name: schedule_name(task, option),
                                      schedule_expression: task.expression,
                                      schedule_expression_timezone: timezone,
                                      flexible_time_window: {
                                        maximum_window_in_minutes: 5,
                                        mode: "FLEXIBLE"
                                      },
                                      target: {
                                        arn: target.arn,
                                        role_arn: IamRole.new(option).arn,
                                        input: target.input
                                      },
                                      group_name: option.scheduler_group,
                                      state: option.rule_state,
                                      description: schedule_description(task)
                                    })
end

#create_schedule_group(group_name) ⇒ Object



65
66
67
68
69
70
# File 'lib/lambda_whenever/event_bridge_scheduler.rb', line 65

def create_schedule_group(group_name)
  @scheduler_client.create_schedule_group({ name: group_name })
  Logger.instance.message "Schedule group '#{group_name}' created."
rescue Aws::Scheduler::Errors::ConflictException
  Logger.instance.message "Schedule group '#{group_name}' already exists."
end

#list_schedules(group_name) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lambda_whenever/event_bridge_scheduler.rb', line 13

def list_schedules(group_name)
  Logger.instance.message("Schedules in group '#{group_name}':")
  response = @scheduler_client.list_schedules({ group_name: group_name })
  response.schedules.map do |schedule|
    detail = @scheduler_client.get_schedule({ group_name: group_name, name: schedule.name })
    Logger.instance.message "#{schedule.state} #{schedule.name} #{detail.schedule_expression} #{detail.description}"
    {
      name: schedule.name,
      state: schedule.state,
      expression: detail.schedule_expression,
      description: detail.description
    }
  end
end

#schedule_description(task) ⇒ Object



99
100
101
# File 'lib/lambda_whenever/event_bridge_scheduler.rb', line 99

def schedule_description(task)
  task.commands.to_s
end

#schedule_name(task, option) ⇒ Object



94
95
96
97
# File 'lib/lambda_whenever/event_bridge_scheduler.rb', line 94

def schedule_name(task, option)
  input = "#{task.name}-#{Digest::SHA1.hexdigest([option.key, task.expression, *task.commands].join("-"))}"
  sanitize_and_trim(input)
end

#sync_schedules(desired_schedules, current_schedules, option) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lambda_whenever/event_bridge_scheduler.rb', line 28

def sync_schedules(desired_schedules, current_schedules, option)
  desired_names = desired_schedules.map { |s| s[:name] }.to_set
  current_schedules_hash = current_schedules.to_h do |schedule|
    [schedule[:name], schedule]
  end
  current_names = current_schedules_hash.keys.to_set
  to_delete = current_names - desired_names

  to_add, to_update = desired_schedules.each_with_object([[], []]) do |desired, (add, update)|
    current = current_schedules_hash[desired[:name]]
    if current.nil?
      add << desired
    elsif schedules_differ?(current, desired, option)
      update << desired
    end
  end

  Logger.instance.message("Deleting #{to_delete.length} schedules...")
  to_delete.each do |name|
    Logger.instance.message "delete schedule: #{name}"
    delete_schedule(name, option.scheduler_group)
  end

  Logger.instance.message("Creating #{to_add.length} schedules...")
  to_add.each do |schedule|
    Logger.instance.message "create schedule: #{schedule[:name]}"
    create_schedule(schedule[:target], option)
  end

  Logger.instance.message("Updating #{to_update.length} schedules...")
  to_update.each do |desired|
    Logger.instance.message("Updating schedule: #{desired[:name]}")
    delete_schedule(desired[:name], option.scheduler_group)
    create_schedule(desired[:target], option)
  end
end