Class: Jets::CLI::Schedule::Validate

Inherits:
Base
  • Object
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

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

#are_you_sure?Boolean

Returns:

  • (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

#clientObject



90
91
92
# File 'lib/jets/cli/schedule/validate.rb', line 90

def client
  Aws::CloudWatchEvents::Client.new
end

#performObject

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

#runObject



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

  # Create the rule with the provided cron expression
  client.put_rule(
    name: @@rule_name,
    schedule_expression: schedule_expression,
    state: "DISABLED" # Disable the rule to prevent it from being triggered
  )

  # log.info "Valid rule: #{key}"
  # log.info "  Schedule expression: #{schedule_expression}"
  true # If no error is raised, the cron expression is valid
rescue Aws::CloudWatchEvents::Errors::ValidationException => e
  log.error "Invalid rule: #{key}"
  log.error "Schedule expression: #{schedule_expression}"
  # log.error "Validation Error: #{e.message}" # commented out to reduce noise, not useful
  @@has_errors ||= true
  false # If ValidationException is raised, the cron expression is invalid
ensure
  # Delete the rule after validation (whether it's valid or not)
  begin
    client.delete_rule(name: @@rule_name)
  rescue
    nil
  end
end