Class: Hoboken::AddOns::Sidekiq

Inherits:
Group
  • Object
show all
Defined in:
lib/hoboken/add_ons/sidekiq.rb

Overview

Background processing via the Sidekiq gem.

Instance Method Summary collapse

Methods inherited from Group

#classic?, #modular?, #rspec?, #rubocop?, #sequel?, source_root

Methods included from Hoboken::Actions

#gem, #indent

Instance Method Details

#add_gemsObject



8
9
10
# File 'lib/hoboken/add_ons/sidekiq.rb', line 8

def add_gems
  gem 'sidekiq', version: '6.2'
end

#add_worker_to_procfileObject

rubocop:enable Metrics/MethodLength



81
82
83
84
85
86
87
88
89
# File 'lib/hoboken/add_ons/sidekiq.rb', line 81

def add_worker_to_procfile
  append_file('Procfile') do
    "\nworker: bundle exec sidekiq " \
      '-r ./config/sidekiq.rb ' \
      '-e $RACK_ENV ' \
      '-c ${MAX_THREADS:-5} ' \
      "-v\n"
  end
end

#adjust_rackup_configObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/hoboken/add_ons/sidekiq.rb', line 12

def adjust_rackup_config
  gsub_file('config.ru', /run .*::App.*\n/) do |match|
    <<~CODE
      require 'sidekiq/web'

      if 'production' == ENV.fetch('RACK_ENV', 'production')
        Sidekiq::Web.use Rack::Auth::Basic do |username, password|
          [username, password] == [ENV['SIDEKIQ_USERNAME'], ENV['SIDEKIQ_PASSWORD']]
        end
      end

      Sidekiq::Web.use Rack::Session::Cookie, secret: ENV['SESSION_SECRET']
      run Rack::URLMap.new('/' => #{match[4...-1].chomp}, '/sidekiq' => Sidekiq::Web)
    CODE
  end
end

#example_workerObject



37
38
39
40
# File 'lib/hoboken/add_ons/sidekiq.rb', line 37

def example_worker
  empty_directory('workers')
  template('hoboken/templates/example_worker.rb.tt', 'workers/example_worker.rb')
end

#remindersObject

rubocop:enable Metrics/MethodLength



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/hoboken/add_ons/sidekiq.rb', line 120

def reminders
  text = <<~TEXT

    Gemfile updated... don't forget to 'bundle install'

    You can configure a Sidekiq error handing service in `config/sidekiq.rb`.

    Sidekiq UI is available at '/sidekiq'. In production environments
    the UI is protected with HTTP Basic Auth. Don't forget to set
    `SIDEKIQ_USERNAME` and `SIDEKIQ_PASSWORD` in your production
    environment or you won't be able to access the Sidekiq UI.
  TEXT

  say text, :green
end

#setup_configObject



29
30
31
32
33
34
35
# File 'lib/hoboken/add_ons/sidekiq.rb', line 29

def setup_config
  template('hoboken/templates/sidekiq.rb.tt', 'config/sidekiq.rb')
  location = "require_relative '../app'"
  insert_into_file('config/environment.rb', before: location) do
    "require_relative 'sidekiq'\n\n"
  end
end

#update_readmeObject

rubocop:disable Metrics/MethodLength



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/hoboken/add_ons/sidekiq.rb', line 92

def update_readme
  snippet = <<~CODE
    <tr>
        <td>REDIS_URL</td>
        <td>Yes</td>
        <td>localhost:6379</td>
        <td>Redis connection URL</td>
    </tr>
    <tr>
        <td>SIDEKIQ_PASSWORD</td>
        <td>Production Only</td>
        <td>None</td>
        <td>Password for SidekiqUI Basic Auth</td>
    </tr>
    <tr>
        <td>SIDEKIQ_USERNAME</td>
        <td>Production Only</td>
        <td>None</td>
        <td>Username for SidekiqUI Basic Auth</td>
    </tr>
  CODE

  insert_into_file('README.md', after: /<tbody>\n/) do
    indent(snippet, 8)
  end
end

#worker_spec_and_helperObject

rubocop:disable Metrics/MethodLength



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/hoboken/add_ons/sidekiq.rb', line 56

def worker_spec_and_helper
  return unless rspec?

  insert_into_file('spec/spec_helper.rb', after: "require 'rack/test'") do
    "\nrequire 'sidekiq/testing'"
  end

  snippet = <<~CODE
    config.before(:each, sidekiq: true) do
      Sidekiq::Worker.clear_all
    end
  CODE

  location = /RSpec\.configure do \|config\|\n/
  insert_into_file('spec/spec_helper.rb', after: location) do
    "#{indent(snippet, 2)}\n"
  end

  template(
    'hoboken/templates/spec/example_worker_spec.rb.tt',
    'spec/example_worker_spec.rb'
  )
end

#worker_test_and_helperObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hoboken/add_ons/sidekiq.rb', line 42

def worker_test_and_helper
  return if rspec?

  insert_into_file('test/test_helper.rb', after: "require 'rack/test'") do
    "\nrequire 'sidekiq/testing'"
  end

  template(
    'hoboken/templates/test/unit/example_worker_test.rb.tt',
    'test/unit/example_worker_test.rb'
  )
end