Class: Jets::CLI::Schedule::Validate
- Inherits:
-
Base
- Object
- Base
- Base
- Jets::CLI::Schedule::Validate
show all
- Defined in:
- lib/jets/cli/schedule/validate.rb
Constant Summary
collapse
- @@rule_name =
"validation_rule_#{Time.now.to_i}_#{rand(1000)}"
- @@has_errors =
false
Instance Attribute Summary
Attributes inherited from Base
#options
Instance Method Summary
collapse
#rate_expression
Methods inherited from Base
#initialize, #paginate, #paging_params, rescue_api_error
#log
#apigateway, #aws_options, #cfn, #codebuild, #dynamodb, #lambda_client, #logs, #s3, #s3_resource, #sns, #sqs, #ssm, #sts, #wafv2
#output_value, #stack_exists?
included, #reset_cache!
Methods included from Api
#api, #api_key
Instance Method Details
#are_you_sure? ⇒ Boolean
80
81
82
83
84
85
86
87
88
|
# File 'lib/jets/cli/schedule/validate.rb', line 80
def are_you_sure?
message = <<~EOL
Will validate: config/jets/schedule.yml
It does this by creating a live test event rule for each entry in schedule.yml
and then deleting it.
EOL
sure?(message)
end
|
#check_exist! ⇒ Object
35
36
37
38
39
|
# File 'lib/jets/cli/schedule/validate.rb', line 35
def check_exist!
unless File.exist?("config/jets/schedule.yml")
abort "config/jets/schedule.yml does not exist. Nothing to validate."
end
end
|
#client ⇒ Object
90
91
92
|
# File 'lib/jets/cli/schedule/validate.rb', line 90
def client
Aws::CloudWatchEvents::Client.new
end
|
interface method: used by deploy to translate on_deploy
26
27
28
29
30
31
32
33
|
# File 'lib/jets/cli/schedule/validate.rb', line 26
def perform
items = YAML.load_file("config/jets/schedule.yml")
items = ActiveSupport::HashWithIndifferentAccess.new(items)
items.each do |key, value|
validate_item(key, value)
end
!@@has_errors
end
|
#run ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/jets/cli/schedule/validate.rb', line 5
def run
are_you_sure?
check_exist!
valid = perform
if valid
log.info "Validation passed. config/jets/schedule.yml is valid"
if File.exist?("config/sidekiq.yml")
log.info <<~EOL
You can remove config/sidekiq.yml now. It is no longer needed.
rm config/sidekiq.yml
EOL
end
else
log.info "Validation failed. Please fix the errors and try again."
log.info "Docs: https://docs.rubyonjets.com/docs/jobs/schedule/"
exit 1
end
end
|
#validate_item(key, value) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/jets/cli/schedule/validate.rb', line 43
def validate_item(key, value)
log.debug "To validate, creating live event rule for #{key}"
log.debug "value: #{value.inspect}"
schedule_expression = if value[:cron]
"cron(#{value[:cron]})"
elsif value[:rate]
expr = rate_expression(value[:rate])
"rate(#{expr})"
else
raise "No schedule expression found for: #{key}"
end
client.put_rule(
name: @@rule_name,
schedule_expression: schedule_expression,
state: "DISABLED" )
true rescue Aws::CloudWatchEvents::Errors::ValidationException => e
log.error "Invalid rule: #{key}"
log.error "Schedule expression: #{schedule_expression}"
@@has_errors ||= true
false ensure
begin
client.delete_rule(name: @@rule_name)
rescue
nil
end
end
|