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



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lambda_whenever/event_bridge_scheduler.rb', line 60

def clean_up_schedules(schedule_group)
  response = @scheduler_client.list_schedules({ group_name: schedule_group })
  response.schedules.each do |schedule|
    @scheduler_client.delete_schedule({
                                        name: schedule.name,
                                        group_name: schedule_group
                                      })
  end
rescue Aws::Scheduler::Errors::ResourceNotFoundException
  puts "Schedule group '#{schedule_group}' does not exist."
end

#create_schedule(target, option) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/lambda_whenever/event_bridge_scheduler.rb', line 30

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



22
23
24
25
26
27
# File 'lib/lambda_whenever/event_bridge_scheduler.rb', line 22

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

#list_schedules(group_name) ⇒ Object



13
14
15
16
17
18
19
20
# 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.each do |schedule|
    detail = @scheduler_client.get_schedule({ group_name: group_name, name: schedule.name })
    puts "#{schedule.state} #{schedule.name} #{detail.schedule_expression} #{detail.description}"
  end
end

#schedule_description(task) ⇒ Object



56
57
58
# File 'lib/lambda_whenever/event_bridge_scheduler.rb', line 56

def schedule_description(task)
  task.commands.to_s
end

#schedule_name(task, option) ⇒ Object



51
52
53
54
# File 'lib/lambda_whenever/event_bridge_scheduler.rb', line 51

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