sidekiq-status 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,7 +8,7 @@ module Sidekiq
8
8
  module Status
9
9
  extend Storage
10
10
  DEFAULT_EXPIRY = 60 * 30
11
- UUID_REGEXP = /[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/
11
+ UUID_REGEXP = /[0-9A-F]{24}/i #RegEx for SecureRandom.hex(12) which is the format Sidekiq uses for its jid
12
12
 
13
13
  # Job status by id
14
14
  # @param [String] id job id returned by async_perform
@@ -16,5 +16,12 @@ module Sidekiq
16
16
  def self.get(id)
17
17
  read_field_for_id(id, :status)
18
18
  end
19
+
20
+ # Get all status fields for a job
21
+ # @params [String] id job id returned by async_perform
22
+ # @return [Hash] hash of all fields stored for the job
23
+ def self.get_all(id)
24
+ read_hash_for_id(id)
25
+ end
19
26
  end
20
27
  end
@@ -2,13 +2,13 @@ module Sidekiq::Status
2
2
  # Should be in the client middleware chain
3
3
  class ClientMiddleware
4
4
  include Storage
5
- # Uses the first argument as id and puts :queued status in the job's Redis hash
5
+ # Uses msg['jid'] id and puts :queued status in the job's Redis hash
6
6
  # @param [Class] worker_class if includes Sidekiq::Status::Worker, the job gets processed with the plugin
7
- # @param [Array] msg job arguments, the firs one becomes the id
7
+ # @param [Array] msg job arguments
8
8
  # @param [String] queue the queue's name
9
9
  def call(worker_class, msg, queue)
10
10
  if worker_class.include? Worker
11
- store_for_id(msg['args'][0], :status => :queued)
11
+ store_for_id(msg['jid'], :status => :queued)
12
12
  end
13
13
  yield
14
14
  end
@@ -9,21 +9,21 @@ module Sidekiq::Status
9
9
  @expiration = opts[:expiration]
10
10
  end
11
11
 
12
- # Takes out the first job argument to use as id
12
+ # Uses sidekiq's internal jid as id
13
13
  # puts :working status into Redis hash
14
14
  # initializes worker instance with id
15
15
  #
16
- # Exception handler sets :failed status, re-inserts worker it to job args and re-throws the exception
16
+ # Exception handler sets :failed status, re-inserts worker and re-throws the exception
17
17
  # Worker::Stopped exception type are processed separately - :stopped status is set, no re-throwing
18
18
  #
19
19
  # @param [Worker] worker worker instance, processed here if its class includes Status::Worker
20
- # @param [Array] msg job args, first of them used as job id, should have uuid format
20
+ # @param [Array] msg job args, should have jid format
21
21
  # @param [String] queue queue name
22
22
  def call(worker, msg, queue)
23
23
  if worker.is_a? Worker
24
- worker.id = msg['args'].shift
24
+ worker.id = msg['jid']
25
25
  unless worker.id.is_a?(String) && UUID_REGEXP.match(worker.id)
26
- raise ArgumentError, "First job argument for a #{worker.class.name} should have uuid format"
26
+ raise ArgumentError, "First job argument for a #{worker.class.name} should have jid format"
27
27
  end
28
28
  worker.store 'status' => 'working'
29
29
  yield
@@ -36,7 +36,6 @@ module Sidekiq::Status
36
36
  rescue
37
37
  if worker.is_a? Worker
38
38
  worker.store 'status' => 'failed'
39
- msg['args'].unshift worker.id
40
39
  end
41
40
  raise
42
41
  ensure
@@ -28,4 +28,13 @@ module Sidekiq::Status::Storage
28
28
  conn.hmget(uuid, field)[0]
29
29
  end
30
30
  end
31
- end
31
+
32
+ # Gets the whole status hash from the job status
33
+ # @param [String] id job id
34
+ # @return [Hash] Hash stored in redis
35
+ def read_hash_for_id(id)
36
+ Sidekiq.redis do |conn|
37
+ conn.hgetall id
38
+ end
39
+ end
40
+ end
@@ -1,5 +1,5 @@
1
1
  module Sidekiq
2
2
  module Status
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -1,26 +1,5 @@
1
- require "active_support"
2
-
3
1
  module Sidekiq::Status::Worker
4
2
  include Sidekiq::Status::Storage
5
- extend ActiveSupport::Concern
6
-
7
- # Adding ID generation to .perform_async
8
- module ClassMethods
9
- # :nodoc:
10
- def self.extended(base)
11
- class << base
12
- alias_method_chain :perform_async, :uuid
13
- end
14
- end
15
-
16
- # Add an id to job arguments
17
- def perform_async_with_uuid(*args)
18
- id = SecureRandom.uuid
19
- args.unshift id
20
- perform_async_without_uuid(*args)
21
- id
22
- end
23
- end
24
3
 
25
4
  class Stopped < StandardError
26
5
  end
@@ -32,7 +11,7 @@ module Sidekiq::Status::Worker
32
11
  # @raise [RuntimeError] raised in case of second id initialization attempt
33
12
  # @return [String] id
34
13
  def id=(id)
35
- raise RuntimeError("Worker ID is already set : #@id") if @id
14
+ raise RuntimeError("Worker ID is already set : #{@id}") if @id
36
15
  @id=id
37
16
  end
38
17
 
@@ -14,6 +14,6 @@ Gem::Specification.new do |gem|
14
14
  gem.require_paths = ['lib']
15
15
  gem.version = Sidekiq::Status::VERSION
16
16
 
17
- gem.add_dependency 'sidekiq', '~> 2.2'
17
+ gem.add_dependency 'sidekiq', '~> 2.7'
18
18
  gem.add_development_dependency 'rspec'
19
19
  end
@@ -3,20 +3,20 @@ require 'spec_helper'
3
3
  describe Sidekiq::Status::ClientMiddleware do
4
4
 
5
5
  let!(:redis) { Sidekiq.redis { |conn| conn } }
6
- let!(:job_id) { SecureRandom.uuid }
6
+ let!(:job_id) { SecureRandom.hex(12) }
7
7
 
8
8
  # Clean Redis before each test
9
9
  before { redis.flushall }
10
10
 
11
11
  describe "#call" do
12
12
  it "sets queued status" do
13
- SecureRandom.should_receive(:uuid).once.and_return(job_id)
13
+ SecureRandom.should_receive(:hex).once.and_return(job_id)
14
14
  StubJob.perform_async(:arg1 => 'val1').should == job_id
15
15
  redis.hget(job_id, :status).should == 'queued'
16
16
  end
17
17
 
18
18
  it "sets status hash ttl" do
19
- SecureRandom.should_receive(:uuid).once.and_return(job_id)
19
+ SecureRandom.should_receive(:hex).once.and_return(job_id)
20
20
  StubJob.perform_async(:arg1 => 'val1').should == job_id
21
21
  (1..Sidekiq::Status::DEFAULT_EXPIRY).should cover redis.ttl(job_id)
22
22
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe Sidekiq::Status::ServerMiddleware do
4
4
 
5
5
  let!(:redis) { Sidekiq.redis { |conn| conn } }
6
- let!(:job_id) { SecureRandom.uuid }
6
+ let!(:job_id) { SecureRandom.hex(12) }
7
7
 
8
8
  # Clean Redis before each test
9
9
  # Seems like flushall has no effect on recently published messages,
@@ -13,7 +13,7 @@ describe Sidekiq::Status::ServerMiddleware do
13
13
  describe "#call" do
14
14
  it "sets working/complete status" do
15
15
  thread = confirmations_thread 4, "status_updates", "job_messages_#{job_id}"
16
- SecureRandom.should_receive(:uuid).once.and_return(job_id)
16
+ SecureRandom.should_receive(:hex).once.and_return(job_id)
17
17
  start_server do
18
18
  ConfirmationJob.perform_async(:arg1 => 'val1').should == job_id
19
19
  thread.value.should == [job_id, job_id,
@@ -24,7 +24,7 @@ describe Sidekiq::Status::ServerMiddleware do
24
24
  end
25
25
 
26
26
  it "sets failed status" do
27
- SecureRandom.should_receive(:uuid).once.and_return(job_id)
27
+ SecureRandom.should_receive(:hex).once.and_return(job_id)
28
28
  start_server do
29
29
  capture_status_updates(3) {
30
30
  FailingJob.perform_async.should == job_id
@@ -34,7 +34,7 @@ describe Sidekiq::Status::ServerMiddleware do
34
34
  end
35
35
 
36
36
  it "sets status hash ttl" do
37
- SecureRandom.should_receive(:uuid).once.and_return(job_id)
37
+ SecureRandom.should_receive(:hex).once.and_return(job_id)
38
38
  StubJob.perform_async(:arg1 => 'val1').should == job_id
39
39
  (1..Sidekiq::Status::DEFAULT_EXPIRY).should cover redis.ttl(job_id)
40
40
  end
@@ -2,11 +2,11 @@ require 'spec_helper'
2
2
 
3
3
  describe Sidekiq::Status::Worker do
4
4
 
5
- let!(:job_id) { SecureRandom.uuid }
5
+ let!(:job_id) { SecureRandom.hex(12) }
6
6
 
7
7
  describe ".perform_async" do
8
8
  it "generates and returns job id" do
9
- SecureRandom.should_receive(:uuid).once.and_return(job_id)
9
+ SecureRandom.should_receive(:hex).once.and_return(job_id)
10
10
  StubJob.perform_async().should == job_id
11
11
  end
12
12
  end
@@ -3,8 +3,8 @@ require 'spec_helper'
3
3
  describe Sidekiq::Status do
4
4
 
5
5
  let!(:redis) { Sidekiq.redis { |conn| conn } }
6
- let!(:job_id) { SecureRandom.uuid }
7
- let!(:job_id_1) { SecureRandom.uuid }
6
+ let!(:job_id) { SecureRandom.hex(12) }
7
+ let!(:job_id_1) { SecureRandom.hex(12) }
8
8
 
9
9
  # Clean Redis before each test
10
10
  # Seems like flushall has no effect on recently published messages,
@@ -13,7 +13,7 @@ describe Sidekiq::Status do
13
13
 
14
14
  describe ".get" do
15
15
  it "gets job status by id" do
16
- SecureRandom.should_receive(:uuid).once.and_return(job_id)
16
+ SecureRandom.should_receive(:hex).once.and_return(job_id)
17
17
 
18
18
  start_server do
19
19
  capture_status_updates(2) {
@@ -25,9 +25,25 @@ describe Sidekiq::Status do
25
25
  end
26
26
  end
27
27
 
28
+ describe ".get_all" do
29
+ it "gets the job hash by id" do
30
+ SecureRandom.should_receive(:hex).once.and_return(job_id)
31
+
32
+ start_server do
33
+ capture_status_updates(2) {
34
+ LongJob.perform_async(1).should == job_id
35
+ }.should == [job_id]*2
36
+ (hash = Sidekiq::Status.get_all(job_id)).should include 'status' => 'working'
37
+ hash.should include 'update_time'
38
+ end
39
+ (hash = Sidekiq::Status.get_all(job_id)).should include 'status' => 'complete'
40
+ hash.should include 'update_time'
41
+ end
42
+ end
43
+
28
44
  context "keeps normal Sidekiq functionality" do
29
45
  it "does jobs with and without status processing" do
30
- SecureRandom.should_receive(:uuid).twice.and_return(job_id, job_id_1)
46
+ SecureRandom.should_receive(:hex).exactly(4).times.and_return(job_id, job_id_1)
31
47
  start_server do
32
48
  capture_status_updates(6) {
33
49
  StubJob.perform_async.should == job_id
@@ -42,7 +58,7 @@ describe Sidekiq::Status do
42
58
  end
43
59
 
44
60
  it "retries failed jobs" do
45
- SecureRandom.should_receive(:uuid).once.and_return(job_id)
61
+ SecureRandom.should_receive(:hex).once.and_return(job_id)
46
62
  start_server do
47
63
  capture_status_updates(5) {
48
64
  RetriedJob.perform_async().should == job_id
@@ -52,4 +68,4 @@ describe Sidekiq::Status do
52
68
  end
53
69
  end
54
70
 
55
- end
71
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require "rspec"
2
2
 
3
3
  require 'sidekiq'
4
+ require 'sidekiq/processor'
5
+ require 'sidekiq/manager'
4
6
  require 'sidekiq-status'
5
7
 
6
8
 
@@ -48,7 +50,7 @@ def start_server()
48
50
  require 'sidekiq/cli'
49
51
  Sidekiq.options[:queues] << 'default'
50
52
  Sidekiq.configure_server do |config|
51
- config.redis = {:url => 'redis://localhost:6379'}
53
+ config.redis = Sidekiq::RedisConnection.create
52
54
  config.server_middleware do |chain|
53
55
  chain.add Sidekiq::Status::ServerMiddleware
54
56
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-status
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-19 00:00:00.000000000 Z
12
+ date: 2013-02-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sidekiq
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '2.2'
21
+ version: '2.7'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: '2.2'
29
+ version: '2.7'
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: rspec
32
32
  requirement: !ruby/object:Gem::Requirement