sidekiq-reliable-fetch 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 35c020c4d73f87e39a4b5fa52930e42505c8e866
4
+ data.tar.gz: 1d344483e74a309efe9db2355450f20a0d5f4487
5
+ SHA512:
6
+ metadata.gz: 755c4623df1d7d672bb4c857ca53cb4b2e12d9d3e0f1cbbb37b7eb42a4f93dbba639bf91f8588607e7570eef27d50996e637f3a71d6920a8aaacdb62b9d273f5
7
+ data.tar.gz: 6c76f3015bfc44f03285d151625cf3ee88024b97c8df6c970b72140290948b8625a550fccbf93f90f3291483175dd382d63f8527742269b54046271ac3be62e6
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ *.gem
data/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
@@ -0,0 +1,134 @@
1
+ module Sidekiq
2
+ class ReliableFetcher
3
+ WORKING_QUEUE = 'working'
4
+ DEFAULT_DEAD_AFTER = 60 * 60 * 24 # 24 hours
5
+ DEFAULT_CLEANING_INTERVAL = 60 * 30 # 30 minutes
6
+
7
+ def self.setup_reliable_fetch!(config)
8
+ config.options[:fetch] = Sidekiq::ReliableFetcher
9
+ config.on(:startup) do
10
+ requeue_on_startup!(config.options[:queues])
11
+ end
12
+ end
13
+
14
+ def initialize(options)
15
+ @strictly_ordered_queues = !!options[:strict]
16
+ @queues = options[:queues].map { |q| "queue:#{q}" }
17
+ @unique_queues = @queues.uniq
18
+ @last_clean = Time.now.to_i
19
+ @cleaning_interval = options[:cleaning_interval] || DEFAULT_CLEANING_INTERVAL
20
+ @consider_dead_after = options[:consider_dead_after] || DEFAULT_DEAD_AFTER
21
+ end
22
+
23
+ def retrieve_work
24
+ clean_working_queues! if @cleaning_interval != -1 && Time.now.to_i - @last_clean > @cleaning_interval
25
+
26
+ queue = pick_queue
27
+ work = Sidekiq.redis { |conn| conn.brpoplpush(queue, "#{queue}:#{WORKING_QUEUE}", Sidekiq::Fetcher::TIMEOUT) }
28
+ UnitOfWork.new(queue, work) if work
29
+ end
30
+
31
+ def self.requeue_on_startup!(queues)
32
+ Sidekiq.logger.debug { "Re-queueing working jobs" }
33
+
34
+ counter = 0
35
+
36
+ Sidekiq.redis do |conn|
37
+ queues.uniq.each do |queue|
38
+ while conn.rpoplpush("queue:#{queue}:#{WORKING_QUEUE}", "queue:#{queue}")
39
+ counter += 1
40
+ end
41
+ end
42
+ end
43
+
44
+ Sidekiq.logger.debug { "Re-queued #{counter} jobs" }
45
+ end
46
+
47
+ # By leaving this as a class method, it can be pluggable and used by the Manager actor. Making it
48
+ # an instance method will make it async to the Fetcher actor
49
+ def self.bulk_requeue(inprogress, options)
50
+ return if inprogress.empty?
51
+
52
+ Sidekiq.logger.debug { "Re-queueing terminated jobs" }
53
+
54
+ Sidekiq.redis do |conn|
55
+ conn.pipelined do
56
+ inprogress.each do |unit_of_work|
57
+ conn.rpush("#{unit_of_work.queue}", unit_of_work.message)
58
+ conn.lrem("#{unit_of_work.queue}:#{WORKING_QUEUE}", 1, unit_of_work.message)
59
+ end
60
+ end
61
+ end
62
+
63
+ Sidekiq.logger.info("Pushed #{inprogress.size} messages back to Redis")
64
+ rescue => ex
65
+ Sidekiq.logger.warn("Failed to requeue #{inprogress.size} jobs: #{ex.message}")
66
+ end
67
+
68
+ UnitOfWork = Struct.new(:queue, :message) do
69
+ def acknowledge
70
+ # NOTE LREM is O(n), so depending on the type of jobs and their average
71
+ # duration, another data structure might be more suited.
72
+ # But as there should not be too much jobs in this queue in the same time,
73
+ # it's probably ok.
74
+ Sidekiq.redis { |conn| conn.lrem("#{queue}:#{WORKING_QUEUE}", 1, message) }
75
+ end
76
+
77
+ def queue_name
78
+ queue.gsub(/.*queue:/, '')
79
+ end
80
+
81
+ def requeue
82
+ Sidekiq.redis do |conn|
83
+ conn.pipelined do
84
+ conn.rpush(queue, message)
85
+ conn.lrem("#{queue}:#{WORKING_QUEUE}", 1, message)
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ private
92
+
93
+ # Creating the Redis#brpoplpush command takes into account any
94
+ # configured queue weights. By default Redis#brpoplpush returns
95
+ # data from the first queue that has pending elements. We
96
+ # choose the queue each time we invoke Redis#brpoplpush to honor weights
97
+ # and avoid queue starvation.
98
+ def pick_queue
99
+ queues = @strictly_ordered_queues ? @unique_queues.dup : @queues.shuffle.uniq
100
+ queues.first
101
+ end
102
+
103
+ # Detect "old" jobs and requeue them because the worker they were assigned
104
+ # to probably failed miserably.
105
+ # NOTE Potential problem here if a specific job always make a worker
106
+ # really fail.
107
+ def clean_working_queues!
108
+ Sidekiq.logger.debug "Cleaning working queues"
109
+
110
+ @unique_queues.each do |queue|
111
+ clean_working_queue!(queue)
112
+ end
113
+
114
+ @last_clean = Time.now.to_i
115
+ end
116
+
117
+ def clean_working_queue!(queue)
118
+ Sidekiq.redis do |conn|
119
+ working_jobs = conn.lrange("#{queue}:#{WORKING_QUEUE}", 0, -1)
120
+ working_jobs.each do |job|
121
+ enqueued_at = Sidekiq.load_json(job)['enqueued_at'].to_i
122
+ job_duration = Time.now.to_i - enqueued_at
123
+
124
+ if job_duration > @consider_dead_after
125
+ Sidekiq.logger.info "Requeued a dead job found in #{queue}:#{WORKING_QUEUE}"
126
+
127
+ conn.rpush("#{queue}", job)
128
+ conn.lrem("#{queue}:#{WORKING_QUEUE}", 1, job)
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,56 @@
1
+ module SidekiqReliableFetch
2
+ ##
3
+ # Encapsulates a working queue within Sidekiq.
4
+ # Allows enumeration of all jobs within the queue.
5
+ #
6
+ # queue = SidekiqReliableFetch::WorkingQueue.new("mailer")
7
+ # queue.each do |job|
8
+ # job.klass # => 'MyWorker'
9
+ # job.args # => [1, 2, 3]
10
+ # end
11
+ #
12
+ class WorkingQueue
13
+ include Enumerable
14
+
15
+ def self.all
16
+ Sidekiq.redis { |c| c.keys('queue:*:working') }
17
+ .sort
18
+ .map { |q| SidekiqReliableFetch::WorkingQueue.new(q) }
19
+ end
20
+
21
+ attr_reader :name
22
+
23
+ def initialize(name)
24
+ @name = name
25
+ end
26
+
27
+ def size
28
+ Sidekiq.redis { |con| con.llen(@name) }
29
+ end
30
+
31
+ def each
32
+ initial_size = size
33
+ deleted_size = 0
34
+ page = 0
35
+ page_size = 50
36
+
37
+ loop do
38
+ range_start = page * page_size - deleted_size
39
+ range_end = page * page_size - deleted_size + (page_size - 1)
40
+ entries = Sidekiq.redis do |conn|
41
+ conn.lrange @name, range_start, range_end
42
+ end
43
+ break if entries.empty?
44
+ page += 1
45
+ entries.each do |entry|
46
+ yield Sidekiq::Job.new(entry, @name)
47
+ end
48
+ deleted_size = initial_size - size
49
+ end
50
+ end
51
+
52
+ def find_job(jid)
53
+ detect { |j| j.jid == jid }
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,25 @@
1
+ require_relative 'api'
2
+
3
+ module SidekiqReliableFetch
4
+ # Hook into *Sidekiq::Web* Sinatra app which adds a new '/working' page
5
+ module Web
6
+ VIEW_PATH = File.expand_path('../../../web/views', __FILE__)
7
+
8
+ def self.registered(app)
9
+ app.get '/working' do
10
+ puts VIEW_PATH.inspect
11
+ @queues = SidekiqReliableFetch::WorkingQueue.all
12
+ erb File.read(File.join(VIEW_PATH, 'working_queues.erb'))
13
+ end
14
+
15
+ app.get '/working/:queue' do
16
+ @queue = SidekiqReliableFetch::WorkingQueue.new(params[:queue])
17
+ erb File.read(File.join(VIEW_PATH, 'working_queue.erb'))
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ require 'sidekiq/web' unless defined?(Sidekiq::Web)
24
+ Sidekiq::Web.register(SidekiqReliableFetch::Web)
25
+ Sidekiq::Web.tabs['Working'] = 'working'
@@ -0,0 +1,4 @@
1
+ require 'sidekiq'
2
+
3
+ require_relative 'sidekiq/reliable_fetcher'
4
+ require_relative 'sidekiq-reliable-fetch/web'
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'sidekiq-reliable-fetch'
3
+ s.version = '0.1.0'
4
+ s.authors = ['TEA']
5
+ s.email = 'technique@tea-ebook.com'
6
+ s.license = 'LGPL-3.0'
7
+ s.homepage = 'https://github.com/TEA-ebook/sidekiq-reliable-fetch'
8
+ s.summary = 'Reliable fetch extension for Sidekiq'
9
+ s.description = 'Redis reliable queue pattern implemented in Sidekiq'
10
+ s.require_paths = ['lib']
11
+
12
+ s.files = `git ls-files`.split($\)
13
+ s.test_files = []
14
+
15
+ s.add_dependency 'sidekiq', '~> 3'
16
+ end
@@ -0,0 +1,25 @@
1
+ <header class="row">
2
+ <div class="col-sm-5">
3
+ <h3><%= t('Working jobs') %></h3>
4
+ </div>
5
+ </header>
6
+
7
+ <% if @queue.size > 0 %>
8
+ <table class="table table-striped table-bordered table-white">
9
+ <thead>
10
+ <tr>
11
+ <th><%= t('Job') %></th>
12
+ <th><%= t('Arguments') %></th>
13
+ </tr>
14
+ </thead>
15
+ <% @queue.each do |entry| %>
16
+ <td><%= entry.display_class %></td>
17
+ <td>
18
+ <div class="args"><%= display_args(entry.display_args) %></div>
19
+ </td>
20
+ </tr>
21
+ <% end %>
22
+ </table>
23
+ <% else %>
24
+ <div class="alert alert-success"><%= t('No working job found') %></div>
25
+ <% end %>
@@ -0,0 +1,17 @@
1
+ <h3><%= t('Working queues') %></h3>
2
+
3
+ <div class="table_container">
4
+ <table class="queues table table-hover table-bordered table-striped table-white">
5
+ <thead>
6
+ <th><%= t('Queue') %></th>
7
+ <th><%= t('Size') %></th>
8
+ </thead>
9
+ <% @queues.each do |queue| %>
10
+ <tr>
11
+ <td>
12
+ <a href="<%= root_path %>working/<%= queue.name %>"><%= queue.name %></a>
13
+ </td>
14
+ <td><%= number_with_delimiter(queue.size) %></td>
15
+ </tr>
16
+ <% end %>
17
+ </table>
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-reliable-fetch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - TEA
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sidekiq
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
27
+ description: Redis reliable queue pattern implemented in Sidekiq
28
+ email: technique@tea-ebook.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - ".gitignore"
34
+ - LICENSE
35
+ - lib/sidekiq-reliable-fetch.rb
36
+ - lib/sidekiq-reliable-fetch/api.rb
37
+ - lib/sidekiq-reliable-fetch/web.rb
38
+ - lib/sidekiq/reliable_fetcher.rb
39
+ - sidekiq-reliable-fetch.gemspec
40
+ - web/views/working_queue.erb
41
+ - web/views/working_queues.erb
42
+ homepage: https://github.com/TEA-ebook/sidekiq-reliable-fetch
43
+ licenses:
44
+ - LGPL-3.0
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 2.2.2
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Reliable fetch extension for Sidekiq
66
+ test_files: []
67
+ has_rdoc: