capistrano-sidekiq 1.0.3 → 2.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2eadf4da0195248e5ce25a260423b32f4f71e7f54b6c0d396b7f5e1c40604aa3
4
- data.tar.gz: e682b32cc48da4c50aebfaed702c7dd98a27b04f23747da96289807a32fa105a
3
+ metadata.gz: 115e140182e27990ccdc85a27c67a2788c4d74724edff94e4786318a0fe5d05c
4
+ data.tar.gz: d0adf318dc8d25845d00d0601b383b54be2327da37cd1e791ac767492b2b55d9
5
5
  SHA512:
6
- metadata.gz: a2016c2552e53d2eb04e8615b094b7d52b868907db40e196b0b5fa2b13de0121b81f9921e7947d9758fd68a12377a8355de5da4556629e5d4d8b9aa3a3ff3cfd
7
- data.tar.gz: eeccd0a9dea2b6a81ed1de9e4a72cf909b1a12e67f9aa06043f0c57e7f13fef8b536b41442eb190a35048a211ed8e8e53162307c5cef2ead88ab84f85f04c5fa
6
+ metadata.gz: 56b1fcd34fa970a62c517016551fa7e68193c35f9c3187e8abbfbffbd7adf3ad2e2766b98ff5b5dbff67d982de8adb687aec4657f70d1a7583f3f89feff2e5fd
7
+ data.tar.gz: d6e43d4fce3e9812b62449bc029820b856742ae8f8aed458bbf461c88984725b02987161964ec432935a5d1f9a58c15bc67ded351f5aea242890f5b34597ce35
@@ -0,0 +1 @@
1
+ ruby 2.6.5
data/README.md CHANGED
@@ -6,12 +6,6 @@ Sidekiq integration for Capistrano
6
6
 
7
7
  ## Installation
8
8
 
9
- Add this line to your application's Gemfile:
10
-
11
- gem 'capistrano-sidekiq', github: 'seuros/capistrano-sidekiq'
12
-
13
- or:
14
-
15
9
  gem 'capistrano-sidekiq', group: :development
16
10
 
17
11
  And then execute:
@@ -21,29 +15,28 @@ And then execute:
21
15
 
22
16
  ## Usage
23
17
  ```ruby
24
- # Capfile
25
- require 'capistrano/sidekiq'
26
- require 'capistrano/sidekiq/monit' #to require monit tasks # Only for capistrano3
18
+ # Capfile
19
+
20
+ require 'capistrano/sidekiq'
21
+ install_plugin Capistrano::Sidekiq # Default sidekiq tasks
22
+ # Then select your service manager
23
+ install_plugin Capistrano::Sidekiq::Systemd
24
+ # or
25
+ install_plugin Capistrano::Sidekiq::Upstart # tests needed
26
+ # or
27
+ install_plugin Capistrano::Sidekiq::Monit # tests needed
27
28
  ```
28
29
 
29
30
 
30
31
  Configurable options, shown here with defaults:
31
32
 
32
33
  ```ruby
34
+ :sidekiq_roles => :app
33
35
  :sidekiq_default_hooks => true
34
36
  :sidekiq_pid => File.join(shared_path, 'tmp', 'pids', 'sidekiq.pid') # ensure this path exists in production before deploying.
35
37
  :sidekiq_env => fetch(:rack_env, fetch(:rails_env, fetch(:stage)))
36
38
  :sidekiq_log => File.join(shared_path, 'log', 'sidekiq.log')
37
- :sidekiq_options => nil
38
- :sidekiq_require => nil
39
- :sidekiq_tag => nil
40
- :sidekiq_config => nil # if you have a config/sidekiq.yml, do not forget to set this.
41
- :sidekiq_queue => nil
42
- :sidekiq_timeout => 10
43
- :sidekiq_roles => :app
44
- :sidekiq_processes => 1
45
- :sidekiq_options_per_process => nil
46
- :sidekiq_concurrency => nil
39
+
47
40
  # sidekiq monit
48
41
  :sidekiq_monit_templates_path => 'config/deploy/templates'
49
42
  :sidekiq_monit_conf_dir => '/etc/monit/conf.d'
@@ -51,10 +44,8 @@ Configurable options, shown here with defaults:
51
44
  :monit_bin => '/usr/bin/monit'
52
45
  :sidekiq_monit_default_hooks => true
53
46
  :sidekiq_monit_group => nil
54
- :sidekiq_service_name => "sidekiq_#{fetch(:application)}_#{fetch(:sidekiq_env)}" + (index ? "_#{index}" : '')
47
+ :sidekiq_service_name => "sidekiq_#{fetch(:application)}"
55
48
 
56
- :sidekiq_cmd => "#{fetch(:bundle_cmd, "bundle")} exec sidekiq" # Only for capistrano2.5
57
- :sidekiqctl_cmd => "#{fetch(:bundle_cmd, "bundle")} exec sidekiqctl" # Only for capistrano2.5
58
49
  :sidekiq_user => nil #user to run sidekiq as
59
50
  ```
60
51
 
@@ -74,70 +65,6 @@ SSHKit.config.command_map[:sidekiq] = "bundle exec sidekiq"
74
65
  SSHKit.config.command_map[:sidekiqctl] = "bundle exec sidekiqctl"
75
66
  ```
76
67
 
77
- ## Multiple processes
78
-
79
- You can configure sidekiq to start with multiple processes. Just set the proper amount in `sidekiq_processes`.
80
-
81
- You can also customize the configuration for every process. If you want to do that, just set
82
- `sidekiq_options_per_process` with an array of the configuration options that you want in string format.
83
- This example should boot the first process with the queue `high` and the second one with the queues `default`
84
- and `low`:
85
-
86
- ```ruby
87
- set :sidekiq_processes, 2
88
- set :sidekiq_options_per_process, ["--queue high", "--queue default --queue low"]
89
- ```
90
-
91
- ## Different number of processes per role
92
-
93
- You can configure how many processes you want to run on each host next way:
94
-
95
- ```ruby
96
- set :sidekiq_roles, [:sidekiq_small, :sidekiq_big]
97
- set :sidekiq_small_processes, 1
98
- set :sidekiq_big_processes, 4
99
- server 'example-small.com', roles: [:sidekiq_small]
100
- server 'example-big.com', roles: [:sidekiq_big]
101
- ```
102
-
103
- ## Integration with systemd
104
-
105
- Set init system to systemd in the cap deploy config:
106
-
107
- ```ruby
108
- set :init_system, :systemd
109
- ```
110
-
111
- Enable lingering for systemd user account
112
-
113
- ```
114
- loginctl enable-linger USERACCOUNT
115
- ```
116
-
117
- Install systemd.service template file and enable the service with:
118
-
119
- ```
120
- bundle exec cap sidekiq:install
121
- ```
122
-
123
- Default name for the service file is `sidekiq-stage.service`. This can be changed as needed, for example:
124
-
125
- ```ruby
126
- set :service_unit_name, "sidekiq-#{fetch(:application)}-#{fetch(:stage)}.service"
127
- ```
128
-
129
- ## Integration with upstart
130
-
131
- Set init system to upstart in the cap deploy config:
132
-
133
- ```ruby
134
- set :init_system, :upstart
135
- ```
136
-
137
- Set upstart service name:
138
- ```ruby
139
- set :upstart_service_name, 'sidekiq'
140
- ```
141
68
 
142
69
  ## Customizing the monit sidekiq templates
143
70
 
@@ -18,5 +18,6 @@ Gem::Specification.new do |spec|
18
18
  spec.require_paths = ['lib']
19
19
 
20
20
  spec.add_dependency 'capistrano', '>= 3.9.0'
21
- spec.add_dependency 'sidekiq', '>= 3.4', "< 6.0"
21
+ spec.add_dependency 'capistrano-bundler'
22
+ spec.add_dependency 'sidekiq', '>= 6.0'
22
23
  end
@@ -1 +1,29 @@
1
- load File.expand_path('../tasks/sidekiq.rake', __FILE__)
1
+ require 'capistrano/bundler'
2
+ require "capistrano/plugin"
3
+
4
+ module Capistrano
5
+ class Sidekiq < Capistrano::Plugin
6
+ def define_tasks
7
+ eval_rakefile File.expand_path('../tasks/sidekiq.rake', __FILE__)
8
+ end
9
+
10
+ def set_defaults
11
+ set_if_empty :sidekiq_default_hooks, true
12
+
13
+ set_if_empty :sidekiq_env, -> { fetch(:rack_env, fetch(:rails_env, fetch(:rake_env, fetch(:stage)))) }
14
+ set_if_empty :sidekiq_roles, fetch(:sidekiq_role, :app)
15
+ set_if_empty :sidekiq_log, -> { File.join(shared_path, 'log', 'sidekiq.log') }
16
+ # Rbenv, Chruby, and RVM integration
17
+ append :rbenv_map_bins, 'sidekiq', 'sidekiqctl'
18
+ append :rvm_map_bins, 'sidekiq', 'sidekiqctl'
19
+ append :chruby_map_bins, 'sidekiq', 'sidekiqctl'
20
+ # Bundler integration
21
+ append :bundle_bins, 'sidekiq', 'sidekiqctl'
22
+ end
23
+
24
+ end
25
+ end
26
+
27
+ require_relative 'sidekiq/systemd'
28
+ require_relative 'sidekiq/upstart'
29
+ require_relative 'sidekiq/monit'
@@ -1 +1,17 @@
1
- load File.expand_path('../../tasks/monit.rake', __FILE__)
1
+ module Capistrano
2
+ class Sidekiq::Monit < Capistrano::Plugin
3
+ def set_defaults
4
+ set_if_empty :monit_bin, '/usr/bin/monit'
5
+ set_if_empty :sidekiq_monit_conf_dir, '/etc/monit/conf.d'
6
+ set_if_empty :sidekiq_monit_conf_file, -> { "#{sidekiq_service_name}.conf" }
7
+ set_if_empty :sidekiq_monit_use_sudo, true
8
+ set_if_empty :sidekiq_monit_default_hooks, true
9
+ set_if_empty :sidekiq_monit_templates_path, 'config/deploy/templates'
10
+ set_if_empty :sidekiq_monit_group, nil
11
+ end
12
+
13
+ def define_tasks
14
+ eval_rakefile File.expand_path('../../tasks/monit.rake', __FILE__)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ module Capistrano
2
+ class Sidekiq::Systemd < Capistrano::Plugin
3
+ def set_defaults
4
+ set_if_empty :sidekiq_service_unit_name, 'sidekiq'
5
+ set_if_empty :sidekiq_service_unit_user, :user # :system
6
+ end
7
+
8
+ def define_tasks
9
+ eval_rakefile File.expand_path('../../tasks/systemd.rake', __FILE__)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module Capistrano
2
+ class Sidekiq::Upstart < Capistrano::Plugin
3
+ def set_defaults
4
+ set_if_empty :sidekiq_service_unit_name, 'sidekiq'
5
+ end
6
+
7
+ def define_tasks
8
+ eval_rakefile File.expand_path('../../tasks/upstart.rake', __FILE__)
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module Capistrano
2
- SidekiqVERSION = '1.0.3'
2
+ SidekiqVERSION = '2.0.0.beta1'
3
3
  end
@@ -1,29 +1,3 @@
1
- namespace :load do
2
- task :defaults do
3
- set :sidekiq_default_hooks, true
4
-
5
- set :sidekiq_pid, -> { File.join(shared_path, 'tmp', 'pids', 'sidekiq.pid') }
6
- set :sidekiq_env, -> { fetch(:rack_env, fetch(:rails_env, fetch(:stage))) }
7
- set :sidekiq_log, -> { File.join(shared_path, 'log', 'sidekiq.log') }
8
- set :sidekiq_timeout, 10
9
- set :sidekiq_roles, fetch(:sidekiq_role, :app)
10
- set :sidekiq_processes, 1
11
- set :sidekiq_options_per_process, nil
12
- set :sidekiq_user, nil
13
- # Rbenv, Chruby, and RVM integration
14
- set :rbenv_map_bins, fetch(:rbenv_map_bins).to_a.concat(%w[sidekiq sidekiqctl])
15
- set :rvm_map_bins, fetch(:rvm_map_bins).to_a.concat(%w[sidekiq sidekiqctl])
16
- set :chruby_map_bins, fetch(:chruby_map_bins).to_a.concat(%w[sidekiq sidekiqctl])
17
- # Bundler integration
18
- set :bundle_bins, fetch(:bundle_bins).to_a.concat(%w[sidekiq sidekiqctl])
19
- # Init system integration
20
- set :init_system, -> { nil }
21
- # systemd integration
22
- set :service_unit_name, "sidekiq-#{fetch(:stage)}.service"
23
- set :upstart_service_name, "sidekiq"
24
- end
25
- end
26
-
27
1
  namespace :deploy do
28
2
  before :starting, :check_sidekiq_hooks do
29
3
  invoke 'sidekiq:add_default_hooks' if fetch(:sidekiq_default_hooks)
@@ -32,255 +6,15 @@ end
32
6
 
33
7
  namespace :sidekiq do
34
8
  task :add_default_hooks do
35
- after 'deploy:starting', 'sidekiq:quiet'
36
- after 'deploy:updated', 'sidekiq:stop'
9
+ after 'deploy:starting', 'sidekiq:quiet'
10
+ after 'deploy:updated', 'sidekiq:stop'
37
11
  after 'deploy:published', 'sidekiq:start'
38
12
  after 'deploy:failed', 'sidekiq:restart'
39
13
  end
40
14
 
41
- desc 'Quiet sidekiq (stop fetching new tasks from Redis)'
42
- task :quiet do
43
- on roles fetch(:sidekiq_roles) do |role|
44
- switch_user(role) do
45
- case fetch(:init_system)
46
- when :systemd
47
- execute :systemctl, "--user", "reload", fetch(:service_unit_name), raise_on_non_zero_exit: false
48
- when :upstart
49
- sudo :service, fetch(:upstart_service_name), :reload
50
- else
51
- if test("[ -d #{release_path} ]")
52
- each_process_with_index(reverse: true) do |pid_file, _idx|
53
- if pid_file_exists?(pid_file) && process_exists?(pid_file)
54
- quiet_sidekiq(pid_file)
55
- end
56
- end
57
- end
58
- end
59
- end
60
- end
61
- end
62
-
63
- desc 'Stop sidekiq (graceful shutdown within timeout, put unfinished tasks back to Redis)'
64
- task :stop do
65
- on roles fetch(:sidekiq_roles) do |role|
66
- switch_user(role) do
67
- case fetch(:init_system)
68
- when :systemd
69
- execute :systemctl, "--user", "stop", fetch(:service_unit_name)
70
- when :upstart
71
- sudo :service, fetch(:upstart_service_name), :stop
72
- else
73
- if test("[ -d #{release_path} ]")
74
- each_process_with_index(reverse: true) do |pid_file, _idx|
75
- if pid_file_exists?(pid_file) && process_exists?(pid_file)
76
- stop_sidekiq(pid_file)
77
- end
78
- end
79
- end
80
- end
81
- end
82
- end
83
- end
84
-
85
- desc 'Start sidekiq'
86
- task :start do
87
- on roles fetch(:sidekiq_roles) do |role|
88
- switch_user(role) do
89
- case fetch(:init_system)
90
- when :systemd
91
- execute :systemctl, '--user', 'start', fetch(:service_unit_name)
92
- when :upstart
93
- sudo :service, fetch(:upstart_service_name), :start
94
- else
95
- each_process_with_index do |pid_file, idx|
96
- unless pid_file_exists?(pid_file) && process_exists?(pid_file)
97
- start_sidekiq(pid_file, idx)
98
- end
99
- end
100
- end
101
- end
102
- end
103
- end
104
-
105
15
  desc 'Restart sidekiq'
106
16
  task :restart do
107
17
  invoke! 'sidekiq:stop'
108
18
  invoke! 'sidekiq:start'
109
19
  end
110
-
111
- desc 'Rolling-restart sidekiq'
112
- task :rolling_restart do
113
- on roles fetch(:sidekiq_roles) do |role|
114
- switch_user(role) do
115
- each_process_with_index(reverse: true) do |pid_file, idx|
116
- if pid_file_exists?(pid_file) && process_exists?(pid_file)
117
- stop_sidekiq(pid_file)
118
- end
119
- start_sidekiq(pid_file, idx)
120
- end
121
- end
122
- end
123
- end
124
-
125
- desc 'Delete any pid file not in use'
126
- task :cleanup do
127
- on roles fetch(:sidekiq_roles) do |role|
128
- switch_user(role) do
129
- each_process_with_index do |pid_file, _idx|
130
- unless process_exists?(pid_file)
131
- next unless pid_file_exists?(pid_file)
132
- execute "rm #{pid_file}"
133
- end
134
- end
135
- end
136
- end
137
- end
138
-
139
- # TODO: Don't start if all processes are off, raise warning.
140
- desc 'Respawn missing sidekiq processes'
141
- task :respawn do
142
- invoke 'sidekiq:cleanup'
143
- on roles fetch(:sidekiq_roles) do |role|
144
- switch_user(role) do
145
- each_process_with_index do |pid_file, idx|
146
- start_sidekiq(pid_file, idx) unless pid_file_exists?(pid_file)
147
- end
148
- end
149
- end
150
- end
151
-
152
- task :install do
153
- on roles fetch(:sidekiq_roles) do |role|
154
- switch_user(role) do
155
- case fetch(:init_system)
156
- when :systemd
157
- create_systemd_template
158
- execute :systemctl, "--user", "enable", fetch(:service_unit_name)
159
- end
160
- end
161
- end
162
- end
163
-
164
- task :uninstall do
165
- on roles fetch(:sidekiq_roles) do |role|
166
- switch_user(role) do
167
- case fetch(:init_system)
168
- when :systemd
169
- execute :systemctl, "--user", "disable", fetch(:service_unit_name)
170
- execute :rm, File.join(fetch(:service_unit_path, fetch_systemd_unit_path),fetch(:service_unit_name))
171
- end
172
- end
173
- end
174
- end
175
-
176
- def each_process_with_index(reverse: false)
177
- pid_file_list = pid_files
178
- pid_file_list.reverse! if reverse
179
- pid_file_list.each_with_index do |pid_file, idx|
180
- within release_path do
181
- yield(pid_file, idx)
182
- end
183
- end
184
- end
185
-
186
- def fetch_systemd_unit_path
187
- home_dir = capture :pwd
188
- File.join(home_dir, ".config", "systemd", "user")
189
- end
190
-
191
- def create_systemd_template
192
- search_paths = [
193
- File.expand_path(
194
- File.join(*%w[.. .. .. generators capistrano sidekiq systemd templates sidekiq.service.capistrano.erb]),
195
- __FILE__
196
- ),
197
- ]
198
- template_path = search_paths.detect {|path| File.file?(path)}
199
- template = File.read(template_path)
200
- systemd_path = fetch(:service_unit_path, fetch_systemd_unit_path)
201
- execute :mkdir, "-p", systemd_path
202
- upload!(
203
- StringIO.new(ERB.new(template).result(binding)),
204
- "#{systemd_path}/#{fetch :service_unit_name}"
205
- )
206
- execute :systemctl, "--user", "daemon-reload"
207
- end
208
-
209
- def pid_files
210
- sidekiq_roles = Array(fetch(:sidekiq_roles)).dup
211
- sidekiq_roles.select! { |role| host.roles.include?(role) }
212
- sidekiq_roles.flat_map do |role|
213
- processes = fetch(:"#{ role }_processes") || fetch(:sidekiq_processes)
214
- Array.new(processes) { |idx| fetch(:sidekiq_pid).gsub(/\.pid$/, "-#{idx}.pid") }
215
- end
216
- end
217
-
218
- def pid_file_exists?(pid_file)
219
- test(*("[ -f #{pid_file} ]").split(' '))
220
- end
221
-
222
- def process_exists?(pid_file)
223
- test(*("kill -0 $( cat #{pid_file} )").split(' '))
224
- end
225
-
226
- def quiet_sidekiq(pid_file)
227
- begin
228
- execute :sidekiqctl, 'quiet', pid_file.to_s
229
- rescue SSHKit::Command::Failed
230
- # If gems are not installed (first deploy) and sidekiq_default_hooks is active
231
- warn 'sidekiqctl not found (ignore if this is the first deploy)'
232
- end
233
- end
234
-
235
- def stop_sidekiq(pid_file)
236
- execute :sidekiqctl, 'stop', pid_file.to_s, fetch(:sidekiq_timeout)
237
- end
238
-
239
- def start_sidekiq(pid_file, idx = 0)
240
- args = []
241
- args.push "--index #{idx}"
242
- args.push "--pidfile #{pid_file}"
243
- args.push "--environment #{fetch(:sidekiq_env)}"
244
- args.push "--logfile #{fetch(:sidekiq_log)}" if fetch(:sidekiq_log)
245
- args.push "--require #{fetch(:sidekiq_require)}" if fetch(:sidekiq_require)
246
- args.push "--tag #{fetch(:sidekiq_tag)}" if fetch(:sidekiq_tag)
247
- Array(fetch(:sidekiq_queue)).each do |queue|
248
- args.push "--queue #{queue}"
249
- end
250
- args.push "--config #{fetch(:sidekiq_config)}" if fetch(:sidekiq_config)
251
- args.push "--concurrency #{fetch(:sidekiq_concurrency)}" if fetch(:sidekiq_concurrency)
252
- if (process_options = fetch(:sidekiq_options_per_process))
253
- args.push process_options[idx]
254
- end
255
- # use sidekiq_options for special options
256
- args.push fetch(:sidekiq_options) if fetch(:sidekiq_options)
257
-
258
- if defined?(JRUBY_VERSION)
259
- args.push '>/dev/null 2>&1 &'
260
- warn 'Since JRuby doesn\'t support Process.daemon, Sidekiq will not be running as a daemon.'
261
- else
262
- args.push '--daemon'
263
- end
264
-
265
- execute :sidekiq, args.compact.join(' ')
266
- end
267
-
268
- def switch_user(role)
269
- su_user = sidekiq_user(role)
270
- if su_user == role.user
271
- yield
272
- else
273
- as su_user do
274
- yield
275
- end
276
- end
277
- end
278
-
279
- def sidekiq_user(role)
280
- properties = role.properties
281
- properties.fetch(:sidekiq_user) || # local property for sidekiq only
282
- fetch(:sidekiq_user) ||
283
- properties.fetch(:run_as) || # global property across multiple capistrano gems
284
- role.user
285
- end
286
20
  end
@@ -0,0 +1,134 @@
1
+ git_plugin = self
2
+
3
+ namespace :sidekiq do
4
+ desc 'Quiet sidekiq (stop fetching new tasks from Redis)'
5
+ task :quiet do
6
+ on roles fetch(:sidekiq_roles) do |role|
7
+ git_plugin.switch_user(role) do
8
+ if fetch(:sidekiq_service_unit_user) == :system
9
+ execute :sudo, :systemctl, "reload", fetch(:sidekiq_service_unit_name), raise_on_non_zero_exit: false
10
+ else
11
+ execute :systemctl, "--user", "reload", fetch(:sidekiq_service_unit_name), raise_on_non_zero_exit: false
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ desc 'Stop sidekiq (graceful shutdown within timeout, put unfinished tasks back to Redis)'
18
+ task :stop do
19
+ on roles fetch(:sidekiq_roles) do |role|
20
+ git_plugin.switch_user(role) do
21
+ if fetch(:sidekiq_service_unit_user) == :system
22
+ execute :sudo, :systemctl, "stop", fetch(:sidekiq_service_unit_name)
23
+ else
24
+ execute :systemctl, "--user", "stop", fetch(:sidekiq_service_unit_name)
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ desc 'Start sidekiq'
31
+ task :start do
32
+ on roles fetch(:sidekiq_roles) do |role|
33
+ git_plugin.switch_user(role) do
34
+ if fetch(:sidekiq_service_unit_user) == :system
35
+ execute :sudo, :systemctl, 'start', fetch(:sidekiq_service_unit_name)
36
+ else
37
+ execute :systemctl, '--user', 'start', fetch(:sidekiq_service_unit_name)
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ desc 'Install systemd sidekiq service'
44
+ task :install do
45
+ on roles fetch(:sidekiq_roles) do |role|
46
+ git_plugin.switch_user(role) do
47
+ git_plugin.create_systemd_template
48
+ if fetch(:sidekiq_service_unit_user) == :system
49
+ execute :sudo, :systemctl, "enable", fetch(:sidekiq_service_unit_name)
50
+ else
51
+ execute :systemctl, "--user", "enable", fetch(:sidekiq_service_unit_name)
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ desc 'UnInstall systemd sidekiq service'
58
+ task :uninstall do
59
+ on roles fetch(:sidekiq_roles) do |role|
60
+ git_plugin.switch_user(role) do
61
+ if fetch(:sidekiq_service_unit_user) == :system
62
+ execute :sudo, :systemctl, "disable", fetch(:sidekiq_service_unit_name)
63
+ else
64
+ execute :systemctl, "--user", "disable", fetch(:sidekiq_service_unit_name)
65
+ end
66
+ execute :rm, '-f', File.join(fetch(:service_unit_path, fetch_systemd_unit_path), fetch(:sidekiq_service_unit_name))
67
+ end
68
+ end
69
+ end
70
+
71
+ desc 'Generate service_locally'
72
+ task :generate_service_locally do
73
+ run_locally do
74
+ File.write('sidekiq', git_plugin.compiled_template)
75
+ end
76
+ end
77
+
78
+ def fetch_systemd_unit_path
79
+ if fetch(:sidekiq_service_unit_user) == :system
80
+ # if the path is not standard `set :service_unit_path`
81
+ "/etc/systemd/system/"
82
+ else
83
+ home_dir = backend.capture :pwd
84
+ File.join(home_dir, ".config", "systemd", "user")
85
+ end
86
+ end
87
+
88
+ def compiled_template
89
+ search_paths = [
90
+ File.expand_path(
91
+ File.join(*%w[.. .. .. generators capistrano sidekiq systemd templates sidekiq.service.capistrano.erb]),
92
+ __FILE__
93
+ ),
94
+ ]
95
+ template_path = search_paths.detect { |path| File.file?(path) }
96
+ template = File.read(template_path)
97
+ ERB.new(template).result(binding)
98
+ end
99
+
100
+ def create_systemd_template
101
+ ctemplate = compiled_template
102
+ systemd_path = fetch(:service_unit_path, fetch_systemd_unit_path)
103
+
104
+ if fetch(:sidekiq_service_unit_user) == :user
105
+ backend.execute :mkdir, "-p", systemd_path
106
+ end
107
+ backend.upload!(
108
+ StringIO.new(ctemplate),
109
+ "/tmp/#{fetch :sidekiq_service_unit_name}.service"
110
+ )
111
+ if fetch(:sidekiq_service_unit_user) == :system
112
+ backend.execute :sudo, :mv, "/tmp/#{fetch :sidekiq_service_unit_name}.service", "#{systemd_path}/#{fetch :sidekiq_service_unit_name}.service"
113
+ backend.execute :sudo, :systemctl, "daemon-reload"
114
+ else
115
+ backend.execute :sudo, :mv, "/tmp/#{fetch :sidekiq_service_unit_name}.service", "#{systemd_path}/#{fetch :sidekiq_service_unit_name}.service"
116
+ backend.execute :systemctl, "--user", "daemon-reload"
117
+ end
118
+ end
119
+
120
+ def switch_user(role)
121
+ su_user = sidekiq_user
122
+ if su_user != role.user
123
+ yield
124
+ else
125
+ as su_user do
126
+ yield
127
+ end
128
+ end
129
+ end
130
+
131
+ def sidekiq_user
132
+ fetch(:sidekiq_user, fetch(:run_as))
133
+ end
134
+ end
@@ -0,0 +1,47 @@
1
+ namespace :sidekiq do
2
+ desc 'Quiet sidekiq (stop fetching new tasks from Redis)'
3
+ task :quiet do
4
+ on roles fetch(:sidekiq_roles) do |role|
5
+ git_plugin.switch_user(role) do
6
+ sudo :service, fetch(:sidekiq_service_unit_name), :reload
7
+ end
8
+ end
9
+ end
10
+
11
+ desc 'Stop sidekiq (graceful shutdown within timeout, put unfinished tasks back to Redis)'
12
+ task :stop do
13
+ on roles fetch(:sidekiq_roles) do |role|
14
+ git_plugin.switch_user(role) do
15
+ sudo :service, fetch(:sidekiq_service_unit_name), :stop
16
+ end
17
+ end
18
+ end
19
+
20
+ desc 'Start sidekiq'
21
+ task :start do
22
+ on roles fetch(:sidekiq_roles) do |role|
23
+ git_plugin.switch_user(role) do
24
+ sudo :service, fetch(:sidekiq_service_unit_name), :start
25
+ end
26
+ end
27
+ end
28
+
29
+ def switch_user(role)
30
+ su_user = sidekiq_user(role)
31
+ if su_user == role.user
32
+ yield
33
+ else
34
+ as su_user do
35
+ yield
36
+ end
37
+ end
38
+ end
39
+
40
+ def sidekiq_user(role)
41
+ properties = role.properties
42
+ properties.fetch(:sidekiq_user) || # local property for sidekiq only
43
+ fetch(:sidekiq_user) ||
44
+ properties.fetch(:run_as) || # global property across multiple capistrano gems
45
+ role.user
46
+ end
47
+ end
@@ -4,11 +4,12 @@ After=syslog.target network.target
4
4
 
5
5
  [Service]
6
6
  Type=simple
7
- Environment=RAILS_ENV=<%= fetch(:rails_env) %>
8
- WorkingDirectory=<%= fetch(:deploy_to) %>/current
9
- ExecStart=<%= fetch(:bundler_path, '/usr/local/bin/bundler') %> exec sidekiq -e <%= fetch(:rails_env) %>
7
+ WorkingDirectory=<%= File.join(fetch(:deploy_to), 'current') %>
8
+ ExecStart=<%= SSHKit.config.command_map[:bundler] %> exec sidekiq -e <%= fetch(:sidekiq_env) %> -l <%= fetch(:sidekiq_log) %>
10
9
  ExecReload=/bin/kill -TSTP $MAINPID
11
10
  ExecStop=/bin/kill -TERM $MAINPID
11
+ <%="User=#{sidekiq_user}" if sidekiq_user %>
12
+ <%="EnvironmentFile=#{fetch(:sidekiq_service_unit_env_file)}" if fetch(:sidekiq_service_unit_env_file) %>
12
13
 
13
14
  RestartSec=1
14
15
  Restart=on-failure
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capistrano-sidekiq
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 2.0.0.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-02 00:00:00.000000000 Z
11
+ date: 2020-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: capistrano
@@ -25,13 +25,24 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: 3.9.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: sidekiq
28
+ name: capistrano-bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '3.4'
34
- - - "<"
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sidekiq
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
35
46
  - !ruby/object:Gem::Version
36
47
  version: '6.0'
37
48
  type: :runtime
@@ -39,9 +50,6 @@ dependencies:
39
50
  version_requirements: !ruby/object:Gem::Requirement
40
51
  requirements:
41
52
  - - ">="
42
- - !ruby/object:Gem::Version
43
- version: '3.4'
44
- - - "<"
45
53
  - !ruby/object:Gem::Version
46
54
  version: '6.0'
47
55
  description: Sidekiq integration for Capistrano
@@ -52,6 +60,7 @@ extensions: []
52
60
  extra_rdoc_files: []
53
61
  files:
54
62
  - ".gitignore"
63
+ - ".tool-versions"
55
64
  - CHANGELOG.md
56
65
  - CONTRIBUTORS.md
57
66
  - Gemfile
@@ -62,9 +71,13 @@ files:
62
71
  - lib/capistrano-sidekiq.rb
63
72
  - lib/capistrano/sidekiq.rb
64
73
  - lib/capistrano/sidekiq/monit.rb
74
+ - lib/capistrano/sidekiq/systemd.rb
75
+ - lib/capistrano/sidekiq/upstart.rb
65
76
  - lib/capistrano/sidekiq/version.rb
66
77
  - lib/capistrano/tasks/monit.rake
67
78
  - lib/capistrano/tasks/sidekiq.rake
79
+ - lib/capistrano/tasks/systemd.rake
80
+ - lib/capistrano/tasks/upstart.rake
68
81
  - lib/generators/capistrano/sidekiq/monit/template_generator.rb
69
82
  - lib/generators/capistrano/sidekiq/monit/templates/sidekiq_monit.conf.erb
70
83
  - lib/generators/capistrano/sidekiq/systemd/templates/sidekiq.service.capistrano.erb
@@ -83,9 +96,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
83
96
  version: 2.0.0
84
97
  required_rubygems_version: !ruby/object:Gem::Requirement
85
98
  requirements:
86
- - - ">="
99
+ - - ">"
87
100
  - !ruby/object:Gem::Version
88
- version: '0'
101
+ version: 1.3.1
89
102
  requirements: []
90
103
  rubygems_version: 3.0.3
91
104
  signing_key: