Class: Modulorails::SidekiqGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/modulorails/sidekiq/sidekiq_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_health_checkObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/generators/modulorails/sidekiq/sidekiq_generator.rb', line 65

def add_health_check
  file_path = Rails.root.join('config/initializers/health_check.rb')
  return unless File.exist?(file_path)

  return if File.read(file_path).match?(/add_custom_check\s*\(?\s*['"]sidekiq-queues['"]\s*\)?/)

  inject_into_file file_path, after: /^HealthCheck.setup do \|config\|\n$/ do
    <<-RUBY

# Add one or more custom checks that return a blank string if ok, or an error message if there is an error
config.add_custom_check('sidekiq-queues') do
  queues = Sidekiq::Queue.all

  # No queues, means no jobs, ok!
  next '' if queues.empty?

  global_latency = queues.each.map { |queue| queue.latency }.sum

  # Global latency is less than 5 minutes, ok!
  global_latency < 5.minutes ? '' : "Global latency (\#{global_latency}) is too high."
end

# Add one or more custom checks that return a blank string if ok, or an error message if there is an error
config.add_custom_check('sidekiq-retries') do
  retry_jobs_count = Sidekiq::RetrySet.new.count

  # Less than 200 jobs to retry, ok!
  retry_jobs_count < 200 ? '' : "\#{retry_jobs_count} are waiting for retry."
end
    RUBY
  end
end

#add_initializerObject



49
50
51
# File 'lib/generators/modulorails/sidekiq/sidekiq_generator.rb', line 49

def add_initializer
  template 'config/initializers/sidekiq.rb'
end

#add_routesObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/generators/modulorails/sidekiq/sidekiq_generator.rb', line 53

def add_routes
  routes_path = Rails.root.join('config/routes.rb')
  return if File.read(routes_path).match?(%r{require ['"]sidekiq/web["']})

  authentication_type = Modulorails.data.authentication_type
  if respond_to?("add_#{authentication_type}_authenticated_route")
    send("add_#{authentication_type}_authenticated_route", routes_path)
  else
    add_unauthenticated_route(routes_path)
  end
end

#add_to_configObject



43
44
45
46
47
# File 'lib/generators/modulorails/sidekiq/sidekiq_generator.rb', line 43

def add_to_config
  Rails.root.glob('config/environments/*.rb') do |file|
    add_to_config_file(file)
  end
end

#add_to_deploy_filesObject



22
23
24
25
26
# File 'lib/generators/modulorails/sidekiq/sidekiq_generator.rb', line 22

def add_to_deploy_files
  add_to_deploy_file(Rails.root.join('config/deploy/production.yaml'))
  add_to_deploy_file(Rails.root.join('config/deploy/staging.yaml'))
  add_to_deploy_file(Rails.root.join('config/deploy/review.yaml'))
end

#add_to_docker_composeObject



18
19
20
# File 'lib/generators/modulorails/sidekiq/sidekiq_generator.rb', line 18

def add_to_docker_compose
  add_to_docker_compose_yml_file(Rails.root.join('.devcontainer/compose.yml'))
end

#add_to_gemfileObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/generators/modulorails/sidekiq/sidekiq_generator.rb', line 28

def add_to_gemfile
  gemfile_path = Rails.root.join('Gemfile')

  # Add gem redis unless already present
  append_to_file(gemfile_path, "\ngem 'redis'\n") unless File.read(gemfile_path).match?(/^\s*gem ['"]redis['"]/)

  # Add gem sidekiq unless already present
  append_to_file(gemfile_path, "\ngem 'sidekiq'\n") unless File.read(gemfile_path).match?(/^\s*gem ['"]sidekiq['"]/)

  # Add gem sidekiq-datadog-error-tracking unless already present
  return if File.read(gemfile_path).match?(/^\s*gem ['"]sidekiq-datadog-error-tracking['"]/)

  append_to_file(gemfile_path, "\ngem 'sidekiq-datadog-error-tracking'\n")
end

#deprecation_warningObject



11
12
13
14
15
16
# File 'lib/generators/modulorails/sidekiq/sidekiq_generator.rb', line 11

def deprecation_warning
  Modulorails.deprecator.warn(<<~MESSAGE)
    Modulorails::SidekiqGenerator is deprecated and will be removed in version 2.0.
    Use Moduloproject 3.0 (available later) to initialize new projects with Sidekiq configuration.
  MESSAGE
end

#remove_entrypointObject



98
99
100
101
# File 'lib/generators/modulorails/sidekiq/sidekiq_generator.rb', line 98

def remove_entrypoint
  remove_file 'entrypoints/sidekiq-entrypoint.sh'
  remove_file 'bin/sidekiq-entrypoint'
end