Class: CloudwatchScheduler::Task

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, every: nil, cron: nil, &code) ⇒ Task

Returns a new instance of Task.



9
10
11
12
13
14
15
# File 'lib/cloudwatch_scheduler/task.rb', line 9

def initialize(name, every: nil, cron: nil, &code)
  @name = name
  @every, @cron = every, cron
  fail "You must specify one of every: or cron:" unless [@every, @cron].any?

  @code = code
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



7
8
9
# File 'lib/cloudwatch_scheduler/task.rb', line 7

def code
  @code
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/cloudwatch_scheduler/task.rb', line 7

def name
  @name
end

Instance Method Details

#cron_expObject



68
69
70
# File 'lib/cloudwatch_scheduler/task.rb', line 68

def cron_exp
  "cron(#{@cron})"
end

#event_dataObject

"queue_name":"scalar-production-poller","priority":null,"arguments":[433],
"locale":"en"


28
29
30
31
32
33
34
35
36
37
# File 'lib/cloudwatch_scheduler/task.rb', line 28

def event_data
  {
    job_class:  CloudwatchScheduler::Job.name,
    job_id:     job_id,
    queue_name: CloudwatchScheduler::Job.queue_name,
    arguments:  [name],
    locale:     "en",
    priority:   nil
  }
end

#invokeObject



17
18
19
# File 'lib/cloudwatch_scheduler/task.rb', line 17

def invoke
  code.call
end

#job_idObject



21
22
23
# File 'lib/cloudwatch_scheduler/task.rb', line 21

def job_id
  Digest::UUID.uuid_v5(Digest::UUID::DNS_NAMESPACE, name)
end

#rate_expObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cloudwatch_scheduler/task.rb', line 52

def rate_exp
  units = if @every % 1.day == 0
            "day"
          elsif @every % 1.hour == 0
            "hour"
          elsif @every % 1.minute == 0
            "minute"
          else
            fail "Intervals less than 1 minute are not allowed by Cloudwatch Events."
          end

  qty = @every.to_i / 1.send(units)

  "rate(#{qty.to_i} #{units.pluralize(qty)})"
end

#rule_nameObject



39
40
41
42
# File 'lib/cloudwatch_scheduler/task.rb', line 39

def rule_name
  limit = 64 - CloudwatchScheduler::Job.queue_name.length
  [name[0, limit - 1], CloudwatchScheduler::Job.queue_name].join("-")
end

#rule_schedule_expressionObject



44
45
46
47
48
49
50
# File 'lib/cloudwatch_scheduler/task.rb', line 44

def rule_schedule_expression
  if @every
    rate_exp
  else
    cron_exp
  end
end