Skip to content

Commit

Permalink
Merge pull request sidekiq#2157 from mperham/client-push-cleanup
Browse files Browse the repository at this point in the history
Clean up logic and docs for Client#push(_bulk)
  • Loading branch information
mperham committed Jan 27, 2015
2 parents be3ec3a + d748e54 commit f83cc72
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions lib/sidekiq/client.rb
Expand Up @@ -55,7 +55,7 @@ def initialize(redis_pool=nil)
# All options must be strings, not symbols. NB: because we are serializing to JSON, all
# symbols in 'args' will be converted to strings.
#
# Returns nil if not pushed to Redis or a unique Job ID if pushed.
# Returns a unique Job ID. If middleware stops the job, nil will be returned instead.
#
# Example:
# push('queue' => 'my_queue', 'class' => MyWorker, 'args' => ['foo', 1, :bat => 'bar'])
Expand All @@ -64,9 +64,10 @@ def push(item)
normed = normalize_item(item)
payload = process_single(item['class'], normed)

pushed = false
pushed = raw_push([payload]) if payload
pushed ? payload['jid'] : nil
if payload
raw_push([payload])
payload['jid']
end
end

##
Expand All @@ -80,19 +81,17 @@ def push(item)
# is run through the client middleware pipeline and each job gets its own Job ID
# as normal.
#
# Returns an array of the of pushed jobs' jids or nil if the pushed failed. The number of jobs
# pushed can be less than the number given if the middleware stopped processing for one
# or more jobs.
# Returns an array of the of pushed jobs' jids. The number of jobs pushed can be less
# than the number given if the middleware stopped processing for one or more jobs.
def push_bulk(items)
normed = normalize_item(items)
payloads = items['args'].map do |args|
raise ArgumentError, "Bulk arguments must be an Array of Arrays: [[1], [2]]" if !args.is_a?(Array)
process_single(items['class'], normed.merge('args' => args, 'jid' => SecureRandom.hex(12), 'enqueued_at' => Time.now.to_f))
end.compact

pushed = false
pushed = raw_push(payloads) if !payloads.empty?
pushed ? payloads.collect { |payload| payload['jid'] } : nil
raw_push(payloads) if !payloads.empty?
payloads.collect { |payload| payload['jid'] }
end

# Allows sharding of jobs across any number of Redis instances. All jobs
Expand Down

0 comments on commit f83cc72

Please sign in to comment.