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_entrypointObject



93
94
95
96
# File 'lib/generators/modulorails/sidekiq/sidekiq_generator.rb', line 93

def add_entrypoint
  template 'entrypoints/sidekiq-entrypoint.sh', 'bin/sidekiq-entrypoint'
  chmod 'bin/sidekiq-entrypoint', 0o755
end

#add_health_checkObject



61
62
63
64
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
# File 'lib/generators/modulorails/sidekiq/sidekiq_generator.rb', line 61

def add_health_check
  file_path = Rails.root.join('config/initializers/health_check.rb')

  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?

  enqueued_jobs_count = queues.each.map { |queue| queue.count }.sum

  # Less than 200 enqueued jobs, ok!
  enqueued_jobs_count < 200 ? '' : "\#{enqueued_jobs_count} are currently enqueued."
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



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

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

#add_routesObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/generators/modulorails/sidekiq/sidekiq_generator.rb', line 47

def add_routes
  routes_path = Rails.root.join('config/routes.rb')

  return if File.read(routes_path).match?(%r{require ['"]sidekiq/web["']})

  inject_into_file routes_path, after: "Rails.application.routes.draw do\n" do
    <<-RUBY
require 'sidekiq/web'
mount Sidekiq::Web => '/sidekiq'

    RUBY
  end
end

#add_to_configObject



37
38
39
40
41
# File 'lib/generators/modulorails/sidekiq/sidekiq_generator.rb', line 37

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

#add_to_deploy_filesObject



19
20
21
22
23
# File 'lib/generators/modulorails/sidekiq/sidekiq_generator.rb', line 19

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



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

def add_to_docker_compose
  if Rails.root.join('compose.yml').exist?
    add_to_docker_compose_yml_file(Rails.root.join('compose.yml'))
  elsif Rails.root.join('docker-compose.yml').exist?
    add_to_docker_compose_yml_file(Rails.root.join('docker-compose.yml'))
  end
end

#add_to_gemfileObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/generators/modulorails/sidekiq/sidekiq_generator.rb', line 25

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

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

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

  append_to_file(gemfile_path, "gem 'sidekiq'\n")
end