sidekiq-superworker 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -26,6 +26,10 @@ module Sidekiq
26
26
  def next
27
27
  relatives.where(subjob_id: next_id).first
28
28
  end
29
+
30
+ def to_info
31
+ "Subjob ##{id} (#{superworker_class} > #{subworker_class})"
32
+ end
29
33
  end
30
34
  end
31
35
  end
@@ -7,6 +7,14 @@ Dir.glob("#{directory}/../../app/models/sidekiq/superworker/*.rb") { |file| requ
7
7
 
8
8
  module Sidekiq
9
9
  module Superworker
10
+ def self.logger
11
+ Logging.logger
12
+ end
13
+
14
+ def self.debug(message)
15
+ logger.debug(message)
16
+ end
17
+
10
18
  def self.table_name_prefix
11
19
  'sidekiq_superworker_'
12
20
  end
@@ -0,0 +1,30 @@
1
+ module Sidekiq
2
+ module Superworker
3
+ class Logging
4
+ class Pretty < Logger::Formatter
5
+ def call(severity, time, program_name, message)
6
+ "#{Time.now.utc.iso8601} Superworker #{severity}: #{message}\n"
7
+ end
8
+ end
9
+
10
+ def self.initialize_logger(log_target = STDOUT)
11
+ @logger = Logger.new(log_target)
12
+ @logger.level = Logger::INFO
13
+ @logger.formatter = Pretty.new
14
+ @logger
15
+ end
16
+
17
+ def self.logger
18
+ @logger || initialize_logger
19
+ end
20
+
21
+ def self.logger=(log)
22
+ @logger = (log ? log : Logger.new('/dev/null'))
23
+ end
24
+
25
+ def logger
26
+ Sidekiq::Superworker::Logging.logger
27
+ end
28
+ end
29
+ end
30
+ end
@@ -2,6 +2,7 @@ module Sidekiq
2
2
  module Superworker
3
3
  class Processor
4
4
  def complete(item, new_thread=true)
5
+ Superworker.debug "JID ##{item['jid']}: Sidekiq job complete"
5
6
  if new_thread
6
7
  # Run this in a new thread so that its execution isn't considered to be part of the
7
8
  # completed job's execution.
@@ -16,6 +17,7 @@ module Sidekiq
16
17
  def error(worker, item, queue, exception)
17
18
  raise "Job has nil jid: #{item}" if item['jid'].nil?
18
19
 
20
+ Superworker.debug "JID ##{item['jid']}: Error thrown"
19
21
  subjob = find_subjob_by_jid(item['jid'])
20
22
  SuperjobProcessor.error(subjob.superjob_id, worker, item, exception) if subjob
21
23
  end
@@ -25,21 +27,26 @@ module Sidekiq
25
27
  def complete_item(item)
26
28
  raise "Job has nil jid: #{item}" if item['jid'].nil?
27
29
 
30
+ Superworker.debug "JID ##{item['jid']}: Passing job from Sidekiq to Superworker"
28
31
  subjob = find_subjob_by_jid(item['jid'])
29
32
  SubjobProcessor.complete(subjob) if subjob
30
33
  end
31
34
 
32
- # The job may've been created outside of sidekiq-superworker, so a nil return value for
33
- # this method isn't necessarily problematic
35
+ # Note: The job may've been created outside of sidekiq-superworker, so a nil return value
36
+ # for this method isn't necessarily problematic
34
37
  def find_subjob_by_jid(jid)
38
+ Superworker.debug "JID ##{jid}: Trying to find Subjob"
35
39
  # The job may complete before the Subjob record is created; in case that happens,
36
40
  # we need to sleep briefly and requery.
37
- max_tries = 5
38
- try = 0
39
- while !(subjob = Subjob.find_by_jid(jid)) && try < max_tries
40
- sleep 2 ** try
41
- try += 1
41
+ tries = 5
42
+ subjob = nil
43
+ (1..tries).each do |try|
44
+ subjob = Subjob.find_by_jid(jid)
45
+ break if subjob
46
+ Superworker.debug "JID ##{jid}: Sleeping before trying to find Subjob again"
47
+ sleep (2 ** try)
42
48
  end
49
+ Superworker.debug "JID ##{jid}: Subjob found: #{subjob ? subjob.to_info : 'nil'}"
43
50
  subjob
44
51
  end
45
52
  end
@@ -2,12 +2,16 @@ module Sidekiq
2
2
  module Superworker
3
3
  class SubjobProcessor
4
4
  def self.enqueue(subjob)
5
+ Superworker.debug "#{subjob.to_info}: Trying to enqueue"
5
6
  # Only enqueue subjobs that aren't running, complete, etc
6
7
  return unless subjob.status == 'initialized'
7
8
 
9
+ Superworker.debug "#{subjob.to_info}: Enqueueing"
8
10
  # If this is a parallel subjob, enqueue all of its children
9
11
  if subjob.subworker_class == 'parallel'
10
12
  subjob.update_attribute(:status, 'running')
13
+
14
+ Superworker.debug "#{subjob.to_info}: Enqueueing parallel children"
11
15
  jids = subjob.children.collect do |child|
12
16
  enqueue(child)
13
17
  end
@@ -31,6 +35,8 @@ module Sidekiq
31
35
  end
32
36
 
33
37
  def self.enqueue_in_sidekiq(subjob, klass)
38
+ Superworker.debug "#{subjob.to_info}: Enqueueing in Sidekiq"
39
+
34
40
  # If sidekiq-unique-jobs is being used for this worker, a number of issues arise if the subjob isn't
35
41
  # queued, so we'll bypass the unique functionality of the worker while running the subjob.
36
42
  is_unique = klass.respond_to?(:sidekiq_options_hash) && !!klass.sidekiq_options_hash['unique']
@@ -52,11 +58,13 @@ module Sidekiq
52
58
  end
53
59
 
54
60
  def self.complete(subjob)
61
+ Superworker.debug "#{subjob.to_info}: Complete"
55
62
  subjob.update_attribute(:status, 'complete')
56
63
 
57
64
  # If children are present, enqueue the first one
58
65
  children = subjob.children
59
66
  if children.present?
67
+ Superworker.debug "#{subjob.to_info}: Enqueueing children"
60
68
  enqueue(children.first)
61
69
  return
62
70
  # Otherwise, set this as having its descendants complete
@@ -66,6 +74,7 @@ module Sidekiq
66
74
  end
67
75
 
68
76
  def self.descendants_are_complete(subjob)
77
+ Superworker.debug "#{subjob.to_info}: Descendants are complete"
69
78
  subjob.update_attribute(:descendants_are_complete, true)
70
79
 
71
80
  parent = subjob.parent
@@ -76,6 +85,7 @@ module Sidekiq
76
85
  if parent
77
86
  siblings_descendants_are_complete = parent.children.all? { |child| child.descendants_are_complete }
78
87
  if siblings_descendants_are_complete
88
+ Superworker.debug "#{subjob.to_info}: Parent (#{parent.to_info}) is complete"
79
89
  descendants_are_complete(parent)
80
90
  parent.update_attribute(:status, 'complete') if is_child_of_parallel
81
91
  end
@@ -94,6 +104,7 @@ module Sidekiq
94
104
  descendants_are_complete(parent)
95
105
  # Otherwise, this is the final subjob of the superjob
96
106
  else
107
+ Superworker.debug "#{subjob.to_info}: Superjob is complete"
97
108
  SuperjobProcessor.complete(subjob.superjob_id)
98
109
  end
99
110
  end
@@ -1,5 +1,5 @@
1
1
  module Sidekiq
2
2
  module Superworker
3
- VERSION = '0.0.4'
3
+ VERSION = '0.0.5'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-superworker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
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: 2013-06-11 00:00:00.000000000 Z
12
+ date: 2013-07-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sidekiq
@@ -103,6 +103,7 @@ files:
103
103
  - lib/generators/sidekiq/superworker/install/templates/create_sidekiq_superworker_subjobs.rb
104
104
  - lib/sidekiq/superworker/dsl_evaluator.rb
105
105
  - lib/sidekiq/superworker/dsl_parser.rb
106
+ - lib/sidekiq/superworker/logging.rb
106
107
  - lib/sidekiq/superworker/processor.rb
107
108
  - lib/sidekiq/superworker/server/middleware.rb
108
109
  - lib/sidekiq/superworker/subjob_processor.rb