Class: Recipes::BackgroundProcessor

Inherits:
Rails::AppBuilder
  • Object
show all
Defined in:
lib/potassium/recipes/background_processor.rb

Instance Method Summary collapse

Instance Method Details

#add_adapters(name) ⇒ Object



55
56
57
58
59
# File 'lib/potassium/recipes/background_processor.rb', line 55

def add_adapters(name)
  application("config.active_job.queue_adapter = :#{name}")
  application "config.active_job.queue_adapter = :async", env: "development"
  application "config.active_job.queue_adapter = :test", env: "test"
end

#add_sidekiqObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/potassium/recipes/background_processor.rb', line 33

def add_sidekiq
  recipe = self
  run_action(:install_sidekiq) do
    gather_gem("sidekiq")
    recipe.add_adapters("sidekiq")
    add_readme_section :internal_dependencies, :sidekiq
    recipe.edit_procfile("bundle exec sidekiq")
    append_to_file(".env.development", "DB_POOL=25\n")
    template("../assets/sidekiq.rb.erb", "config/initializers/sidekiq.rb", force: true)
    copy_file("../assets/sidekiq.yml", "config/sidekiq.yml", force: true)
    copy_file("../assets/redis.yml", "config/redis.yml", force: true)
    recipe.mount_sidekiq_routes
  end
end

#askObject



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/potassium/recipes/background_processor.rb', line 2

def ask
  response = if enabled_mailer?
               info "Note: Emails should be sent on background jobs. We'll install sidekiq"
               true
             else
               answer(:background_processor) do
                 Ask.confirm("Do you want to use Sidekiq for background job processing?")
               end
             end
  set(:background_processor, response)
end

#createObject



14
15
16
17
18
19
20
# File 'lib/potassium/recipes/background_processor.rb', line 14

def create
  if get(:background_processor)
    add_sidekiq
    add_docker_compose_redis_config
    set_redis_dot_env
  end
end

#edit_procfile(cmd) ⇒ Object



48
49
50
51
52
53
# File 'lib/potassium/recipes/background_processor.rb', line 48

def edit_procfile(cmd)
  heroku = load_recipe(:heroku)
  if selected?(:heroku) || heroku.installed?
    gsub_file('Procfile', /^.*$/m) { |match| "#{match}worker: #{cmd}" }
  end
end

#installObject



22
23
24
25
26
27
# File 'lib/potassium/recipes/background_processor.rb', line 22

def install
  ask
  heroku = load_recipe(:heroku)
  set(:heroku, heroku.installed?)
  create
end

#installed?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/potassium/recipes/background_processor.rb', line 29

def installed?
  gem_exists?(/sidekiq/)
end

#mount_sidekiq_routesObject



61
62
63
64
65
66
67
# File 'lib/potassium/recipes/background_processor.rb', line 61

def mount_sidekiq_routes
  insert_into_file "config/routes.rb", after: "Rails.application.routes.draw do\n" do
    <<-HERE.gsub(/^ {6}/, '')
      mount Sidekiq::Web => '/queue'
    HERE
  end
end