-
Notifications
You must be signed in to change notification settings - Fork 5
Use connection_pool as a dumb thread pool instead of Celluloid #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
foca
wants to merge
1
commit into
master
Choose a base branch
from
connection_pool
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,18 @@ | ||
| require 'date' | ||
| require 'disque' | ||
| require 'msgpack' | ||
| require 'connection_pool' | ||
|
|
||
| require_relative 'disc/version' | ||
|
|
||
| class Disc | ||
| attr_reader :disque, | ||
| :disque_timeout | ||
| def self.pool | ||
| @pool ||= begin | ||
| concurrency = Integer(ENV.fetch('DISC_CONCURRENCY', 25)) | ||
| tolerance = Integer(ENV.fetch('DISC_TOLERANCE', disque_timeout / 1000.0)) | ||
| ConnectionPool.new(size: concurrency, timeout: tolerance) { true } | ||
| end | ||
| end | ||
|
|
||
| def self.disque | ||
| @disque ||= Disque.new( | ||
|
|
@@ -21,7 +27,7 @@ def self.disque=(disque) | |
| end | ||
|
|
||
| def self.disque_timeout | ||
| @disque_timeout ||= 100 | ||
| @disque_timeout ||= Integer(ENV.fetch('DISQUE_TIMEOUT', 2000)) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not entirely sure why
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably not. |
||
| end | ||
|
|
||
| def self.disque_timeout=(timeout) | ||
|
|
@@ -44,39 +50,25 @@ def self.run | |
|
|
||
| def initialize(options = {}) | ||
| @disque = options.fetch(:disque, Disc.disque) | ||
| @queues = options.fetch( | ||
| :queues, | ||
| ENV.fetch('QUEUES', 'default') | ||
| ).split(',') | ||
| @count = Integer( | ||
| options.fetch( | ||
| :count, | ||
| ENV.fetch('DISQUE_COUNT', '1') | ||
| ) | ||
| ) | ||
| @timeout = Integer( | ||
| options.fetch( | ||
| :timeout, | ||
| ENV.fetch('DISQUE_TIMEOUT', '2000') | ||
| ) | ||
| ) | ||
|
|
||
| self.run if options[:run] | ||
| self | ||
| @queues = options.fetch(:queues, ENV.fetch('QUEUES', 'default')).split(',') | ||
| @count = Integer(options.fetch(:count, ENV.fetch('DISQUE_COUNT', '1'))) | ||
| @timeout = Integer(options.fetch(:timeout, Disc.disque_timeout)) | ||
| end | ||
|
|
||
| def run | ||
| $stdout.puts("Disc::Worker listening in #{queues}") | ||
| loop do | ||
| jobs = disque.fetch(from: queues, timeout: timeout, count: count) | ||
| Array(jobs).each do |queue, msgid, serialized_job| | ||
| begin | ||
| job = MessagePack.unpack(serialized_job) | ||
| instance = Object.const_get(job['class']).new | ||
| instance.perform(*job['arguments']) | ||
| disque.call('ACKJOB', msgid) | ||
| rescue => err | ||
| Disc.on_error(err, job.update('id' => msgid, 'queue' => queue)) | ||
| Disc.pool.with do | ||
| begin | ||
| job = MessagePack.unpack(serialized_job) | ||
| instance = Object.const_get(job['class']).new | ||
| instance.perform(*job['arguments']) | ||
| disque.call('ACKJOB', msgid) | ||
| rescue => err | ||
| Disc.on_error(err, job.update('id' => msgid, 'queue' => queue)) | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
@@ -94,7 +86,7 @@ def self.included(base) | |
|
|
||
| module ClassMethods | ||
| def disque | ||
| defined?(@disque) ? @disque : Disc.disque | ||
| @disque ||= Disc.disque | ||
| end | ||
|
|
||
| def disque=(disque) | ||
|
|
@@ -116,10 +108,7 @@ def queue | |
| def enqueue(args = [], at: nil, queue: nil) | ||
| disque.push( | ||
| queue || self.queue, | ||
| { | ||
| class: self.name, | ||
| arguments: Array(args) | ||
| }.to_msgpack, | ||
| { class: self.name, arguments: Array(args) }.to_msgpack, | ||
| Disc.disque_timeout, | ||
| delay: at.nil? ? nil : (at.to_time.to_i - DateTime.now.to_time.to_i) | ||
| ) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default timeout for each thread would be the same as for disque to consider a job was not finished. Maybe this should be lower? Dunno, seemed like a decent default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Decent defaults are all we can hope for, if a reason to change it presents itself then we'll reevaluate, for now that's good enough :)