Class: Jets::CLI::Schedule::Translate

Inherits:
Base
  • Object
show all
Defined in:
lib/jets/cli/schedule/translate.rb

Constant Summary collapse

JETS_FIELDS =
%w[
  args
  class
  cron
  rate
  splat_args
]
SUPPORTED_FIELDS =
JETS_FIELDS + %w[
  keyword_arguments
  every
  interval
]
@@has_errors =
false

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods included from Event::Dsl::RateExpression

#rate_expression

Methods inherited from Base

#initialize, #paginate, #paging_params, rescue_api_error

Methods included from Util::Logging

#log

Methods included from AwsServices

#apigateway, #aws_options, #cfn, #codebuild, #dynamodb, #lambda_client, #logs, #s3, #s3_resource, #sns, #sqs, #ssm, #sts, #wafv2

Methods included from AwsServices::StackStatus

#output_value, #stack_exists?

Methods included from AwsServices::GlobalMemoist

included, #reset_cache!

Methods included from Api

#api, #api_key

Constructor Details

This class inherits a constructor from Jets::CLI::Base

Instance Method Details

#add_schedule_expression!(result, original) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/jets/cli/schedule/translate.rb', line 69

def add_schedule_expression!(result, original)
  if original[:cron]
    result[:cron] = cron(original[:cron])
  elsif original[:every]
    handle_every_expression!(result, original[:every])
  elsif original[:interval]
    handle_every_expression!(result, original[:interval])
  end
end

#are_you_sure?Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/jets/cli/schedule/translate.rb', line 132

def are_you_sure?
  message = <<~EOL
    This script will make changes to your project source code.

    It will try to translate the schedule items in

        config/sidekiq.yml => config/jets/schedule.yml

    Note: It's unfeasible to account for all cases perform miracle translations.
    It's a best-effort script, and the hope is that this script gets you pretty far
    and is helpful. 😄

    Please make sure you have backed up and committed your changes first.
  EOL
  sure?(message)
end

#check_exist!Object



126
127
128
129
130
# File 'lib/jets/cli/schedule/translate.rb', line 126

def check_exist!
  unless File.exist?("config/sidekiq.yml")
    abort "ERROR: config/sidekiq.yml does not exist. Unable to translate".color(:red)
  end
end

#cron(expr) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/jets/cli/schedule/translate.rb', line 80

def cron(expr)
  parts = expr.split(" ")
  # https://github.com/sidekiq-scheduler/sidekiq-scheduler
  # Sidekiq scheduler has 6 fields. The first is for seconds. Cron does not support seconds.
  # We'll remove the seconds first field.
  if parts.size == 6
    parts.shift
  end
  if parts.size != 5
    log.info "ERROR: Unsupported cron expression. Too many fields."
    log.info "There are #{parts.size} fields. Should have exactly 5 fields"
    log.info "Offending cron expr: #{expr}"
    @@has_errors = true
  end
  # AWS Cron expressions require ? for the day of the week field
  parts[-1] = "?" if parts[-1] == "*"
  if parts.size == 5
    # AWS Cron expressions require a 6th year field
    parts << "*"
  end
  parts.join(" ")
end

#finish_messageObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/jets/cli/schedule/translate.rb', line 149

def finish_message
  # Created message part of finish message because only want it to show as part of CLI run
  log.info "\n    Created: config/jets/schedule.yml\n".color(:green)
  log.info <<~EOL
    Translation complete! Please double check the schedule to make sure it looks correct.
    Remember, this is a best-effort tool. It does not cover all cases.

    You can validate the config/jets/schedule.yml

        jets schedule:validate

    If that looks good, try deploying the config/jets/schedule.yml
    It should create a *JetsScheduledEvent* child stack
    with the schedule event rules.

        jets deploy
  EOL
end

#handle_every_expression!(result, expr) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/jets/cli/schedule/translate.rb', line 103

def handle_every_expression!(result, expr)
  if simple_every?(expr)
    result[:rate] = rate_expression(expr)
  else
    # complex every expression translate to cron
    expr = Fugit::Nat.parse("every #{expr}").to_cron_s
    result[:cron] = cron(expr)
  end
end

#performObject

interface method: used by deploy to translate on_deploy



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/jets/cli/schedule/translate.rb', line 16

def perform
  # Currently only sidekiq is supported
  sidekiq = YAML.load_file("config/sidekiq.yml")
  sidekiq = ActiveSupport::HashWithIndifferentAccess.new(sidekiq)
  schedule = sidekiq.dig(:scheduler, :schedule) || sidekiq[:schedule]
  unless schedule
    log.error "config/sidekiq.yml does not have a scheduler.schedule key. Nothing to translate."
    return false
  end

  log.info "Translating config/sidekiq.yml => config/jets/schedule.yml"
  mapped = schedule.map do |k, v|
    translate_item(k, v)
  end
  schedule = mapped.inject({}) { |h, v| h.merge(v) }
  text = schedule.deep_stringify_keys!

  unless @@has_errors
    FileUtils.mkdir_p("config/jets")
    IO.write("config/jets/schedule.yml", YAML.dump(text))
  end
  !@@has_errors
end

#runObject



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/jets/cli/schedule/translate.rb', line 3

def run
  are_you_sure?
  check_exist!
  success = perform
  if success
    finish_message
  else
    log.error "Translation failed."
    exit 1
  end
end

#simple_every?(expr) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
# File 'lib/jets/cli/schedule/translate.rb', line 113

def simple_every?(expr)
  # IE: 1h, 1d, 1w, 1m, 45m, 45 minutes, 1 hour, 1 day, 1 week, 1 month
  expr =~ /\d+\s?\w+/
end

#translate_item(key, original) ⇒ Object

Example:

hard_worker:
  class: "HardWorker"
  args: ["bob", 5]
  description: "This is a description of the job"
  cron: "30 6 * *


59
60
61
62
63
64
65
66
67
# File 'lib/jets/cli/schedule/translate.rb', line 59

def translate_item(key, original)
  result = {}
  result[:args] = original[:args] if original[:args]
  result[:class] = original[:class] if original[:class]
  result[:splat_args] = true if original[:keyword_arguments]
  add_schedule_expression!(result, original)
  unsupported_fields!(result, original)
  {key => result}
end

#unsupported_fields!(result, original) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/jets/cli/schedule/translate.rb', line 118

def unsupported_fields!(result, original)
  unsupported = original.keys - SUPPORTED_FIELDS
  unsupported.each do |field|
    result[:"unsupported_#{field}"] = original[field]
  end
  result
end