sidekiq_alive 1.2.0 → 2.0.0

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: e7b89d09d3a01cd0774ea9a4955a407f3fa397a5799e9d01d65c71e7e448bdca
4
- data.tar.gz: 5241eee33524f7115bc9345aec7803e54c5035cb5071dfd92ac1479bcc40fd7d
3
+ metadata.gz: 364bf320e50acf4acacc6b092ddc878e094762a048f03b8496b03dca50c79c67
4
+ data.tar.gz: a19db126856fbb3629994f2d0aec3fd839462c204a62abe03e0833f140fb16f5
5
5
  SHA512:
6
- metadata.gz: 5bef84e7c6f9ab1f28c3512f2628918676f7df835d1c389defb4e50ad47eca6e13cf82a2b6453cc4b9776ecf7e37a4d84d845b3af510222045e115e0316cae9a
7
- data.tar.gz: ee57bf4f7d780d5b8b45b7775989a30af47d77a2faa0db1877280a69ea374471d0143a5ab4edb004f08e28e12a8c9cc8c59e7a4fa9e972a8162b1ab82f8fa1d7
6
+ metadata.gz: c23f150276af1a182dc6dd7eef752125f12c65de9479ca9877294e0344dd02c9b49cd4174ebbb593996ab8937d538ef5a9b446eed19d5667b6462335d97e5e3a
7
+ data.tar.gz: 251ddc155702aa38adcd7368f5dd7e55c9fc40558a2e90fcd05d8641e78faa40337060deef492c23e602884250c6e66dfed1a77c46e058149658975e1e858543
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sidekiq_alive (1.2.0)
4
+ sidekiq_alive (2.0.0)
5
5
  sidekiq
6
6
  sinatra
7
7
 
data/README.md CHANGED
@@ -18,6 +18,23 @@ This worker is responsible to requeue itself for the next liveness probe.
18
18
 
19
19
  Each instance in kubernetes will be checked based on `ENV` variable `HOSTNAME` (kubernetes sets this for each replica/pod).
20
20
 
21
+ On initialization SidekiqAlive will asign to Sidekiq::Worker a queue with the current host and add this queue to the current instance queues to process.
22
+
23
+ example:
24
+
25
+ ```
26
+ hostname: foo
27
+ Worker queue: sidekiq_alive-foo
28
+ instance queues:
29
+ - sidekiq_alive-foo
30
+ *- your queues
31
+
32
+ hostname: bar
33
+ Worker queue: sidekiq_alive-bar
34
+ instance queues:
35
+ - sidekiq_alive-bar
36
+ *- your queues
37
+ ```
21
38
 
22
39
  ## Installation
23
40
 
@@ -35,29 +52,21 @@ Or install it yourself as:
35
52
 
36
53
  $ gem install sidekiq_alive
37
54
 
38
- Run `Sidekiq` with a `sidekiq_alive` queue.
39
-
40
- ```
41
- sidekiq -q sidekiq_alive
42
- ```
43
-
44
- or in your config:
45
55
 
46
- _sidekiq.yml_
47
- ```yaml
48
- queues:
49
- - default
50
- - sidekiq_alive
51
- ```
56
+ ## Usage
52
57
 
53
- __IMPORTANT:__
58
+ SidekiqAlive will start when running `sidekiq` command.
54
59
 
55
- Make sure you run a `quiet` every time before you stop the pods [(issue)](https://github.com/arturictus/sidekiq_alive/issues/10). That's not only important for SidekiqAlive it's important that your workers finish before you stop Sidekiq.
56
- Check [recommended kubernetes setup](#kubernetes-setup)
60
+ Run `Sidekiq`
57
61
 
58
- ## Usage
62
+ ```
63
+ bundle exec sidekiq
64
+ ```
59
65
 
60
- SidekiqAlive will start when running `sidekiq` command.
66
+ ```
67
+ curl localhost:7433
68
+ #=> Alive!
69
+ ```
61
70
 
62
71
 
63
72
  __how to disable?__
@@ -156,28 +165,14 @@ SidekiqAlive.setup do |config|
156
165
  # require 'net/http'
157
166
  # config.callback = proc { Net::HTTP.get("https://status.com/ping") }
158
167
 
159
- # ==> Preferred Queue
160
- # Sidekiq Alive will try to enqueue the workers to this queue. If not found
161
- # will do it to the first available queue.
162
- # It's a good practice to add a dedicated queue for sidekiq alive. If the queue
163
- # where sidekiq is processing SidekiqALive gets overloaded and takes
164
- # longer than the `ttl` to process SidekiqAlive::Worker will make the liveness probe
165
- # to fail. Sidekiq overloaded and restarting every `ttl` cicle.
166
- # Add the sidekiq alive queue!!
168
+ # ==> Queue Prefix
169
+ # SidekiqAlive will run in a independent queue for each instance/replica
170
+ # This queue name will be generated with: "#{queue_prefix}-#{hostname}.
171
+ # You can customize the prefix here.
167
172
  # default: :sidekiq_alive
168
173
  #
169
- # config.preferred_queue = :other
170
-
171
- # ==> delay_between_async_other_host_queue
172
- # When instance receives a job from another instance it requeues itself again
173
- # until the owner instance process it. This was causing a lot of read/writes in big deployments
174
- # with a lot of replicas.
175
- # Delaying the requeue proves to be less read/write intensive
176
- # default: 1
177
- #
178
- # config.delay_between_async_other_host_queue = 0.5
179
- # #or
180
- # config.delay_between_async_other_host_queue = false
174
+ # config.queue_prefix = :other
175
+
181
176
  end
182
177
  ```
183
178
 
@@ -6,10 +6,12 @@ require 'sidekiq_alive/config'
6
6
 
7
7
  module SidekiqAlive
8
8
  def self.start
9
- Sidekiq.configure_server do |config|
10
- SidekiqAlive::Worker.sidekiq_options queue: SidekiqAlive.select_queue(config.options[:queues])
9
+ SidekiqAlive::Worker.sidekiq_options queue: current_queue
10
+ Sidekiq.configure_server do |sq_config|
11
11
 
12
- config.on(:startup) do
12
+ sq_config.options[:queues] << current_queue
13
+
14
+ sq_config.on(:startup) do
13
15
  SidekiqAlive.tap do |sa|
14
16
  sa.logger.info(banner)
15
17
  sa.register_current_instance
@@ -22,10 +24,10 @@ module SidekiqAlive
22
24
  end
23
25
  end
24
26
 
25
- config.on(:quiet) do
27
+ sq_config.on(:quiet) do
26
28
  SidekiqAlive.unregister_current_instance
27
29
  end
28
- config.on(:shutdown) do
30
+ sq_config.on(:shutdown) do
29
31
  Process.kill('TERM', @server_pid) unless @server_pid.nil?
30
32
  Process.wait(@server_pid) unless @server_pid.nil?
31
33
  SidekiqAlive.unregister_current_instance
@@ -33,12 +35,8 @@ module SidekiqAlive
33
35
  end
34
36
  end
35
37
 
36
- def self.select_queue(queues)
37
- @queue = if queues.find { |e| e.to_sym == config.preferred_queue.to_sym }
38
- config.preferred_queue.to_sym
39
- else
40
- queues.first
41
- end
38
+ def self.current_queue
39
+ "#{config.queue_prefix}-#{hostname}"
42
40
  end
43
41
 
44
42
  def self.register_current_instance
@@ -57,10 +55,15 @@ module SidekiqAlive
57
55
  end
58
56
 
59
57
  def self.purge_pending_jobs
58
+ # TODO:
59
+ # Sidekiq 6 allows better way to find scheduled jobs:
60
+ # https://github.com/mperham/sidekiq/wiki/API#scan
60
61
  scheduled_set = Sidekiq::ScheduledSet.new
61
- jobs = scheduled_set.select { |job| job.klass == 'SidekiqAlive::Worker' && job.args[0] == hostname }
62
- logger.info("Purging #{jobs.count} pending for #{hostname}")
62
+ jobs = scheduled_set.select { |job| job.klass == 'SidekiqAlive::Worker' && job.queue == current_queue }
63
+ logger.info("[SidekiqAlive] Purging #{jobs.count} pending for #{hostname}")
63
64
  jobs.each(&:delete)
65
+ logger.info("[SidekiqAlive] Removing queue #{current_queue}")
66
+ Sidekiq::Queue.new(current_queue).clear
64
67
  end
65
68
 
66
69
  def self.current_instance_register_key
@@ -7,8 +7,7 @@ module SidekiqAlive
7
7
  :time_to_live,
8
8
  :callback,
9
9
  :registered_instance_key,
10
- :preferred_queue,
11
- :delay_between_async_other_host_queue
10
+ :queue_prefix
12
11
 
13
12
  def initialize
14
13
  set_defaults
@@ -20,8 +19,7 @@ module SidekiqAlive
20
19
  @time_to_live = 10 * 60
21
20
  @callback = proc {}
22
21
  @registered_instance_key = 'SIDEKIQ_REGISTERED_INSTANCE'
23
- @preferred_queue = :sidekiq_alive
24
- @delay_between_async_other_host_queue = 1
22
+ @queue_prefix = :sidekiq_alive
25
23
  end
26
24
 
27
25
  def registration_ttl
@@ -1,3 +1,3 @@
1
1
  module SidekiqAlive
2
- VERSION = '1.2.0'.freeze
2
+ VERSION = '2.0.0'.freeze
3
3
  end
@@ -4,19 +4,9 @@ module SidekiqAlive
4
4
  sidekiq_options retry: false
5
5
 
6
6
  def perform(hostname = SidekiqAlive.hostname)
7
- return unless hostname_registered?(hostname)
8
- if current_hostname == hostname
9
- write_living_probe
10
- # schedule next living probe
11
- self.class.perform_in(config.time_to_live / 2, current_hostname)
12
- else
13
- # requeue for hostname to validate it's own liveness probe
14
- if config.delay_between_async_other_host_queue
15
- self.class.perform_in(config.delay_between_async_other_host_queue, hostname)
16
- else
17
- self.class.perform_async(hostname)
18
- end
19
- end
7
+ write_living_probe
8
+ # schedule next living probe
9
+ self.class.perform_in(config.time_to_live / 2, current_hostname)
20
10
  end
21
11
 
22
12
  def hostname_registered?(hostname)
@@ -31,7 +21,7 @@ module SidekiqAlive
31
21
  # Increment ttl for current registered instance
32
22
  SidekiqAlive.register_current_instance
33
23
  # after callbacks
34
- config.callback.call()
24
+ config.callback.call() rescue nil
35
25
  end
36
26
 
37
27
  def current_hostname
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq_alive
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artur Pañach
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-21 00:00:00.000000000 Z
11
+ date: 2019-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler