blaxter-delayed_job 2.1.8 → 2.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.1.8
1
+ 2.1.9
data/lib/delayed/job.rb CHANGED
@@ -228,7 +228,11 @@ module Delayed
228
228
 
229
229
  begin
230
230
  runtime = Benchmark.realtime do
231
- Timeout.timeout(max_run_time.to_i) { invoke_job }
231
+ Timeout.timeout(max_run_time.to_i) {
232
+ Delayed::Worker.logger.debug "-> #{Thread.current.object_id} invoke_job"
233
+ invoke_job
234
+ Delayed::Worker.logger.debug "<- #{Thread.current.object_id} invoke_job"
235
+ }
232
236
  end
233
237
  destroy_successful_jobs ? destroy :
234
238
  update_attribute(:finished_at, Time.now)
@@ -43,16 +43,20 @@ module Delayed
43
43
  return false unless can_execute job
44
44
  s = Semaphore.new
45
45
  t = Thread.new do
46
+ log "##{Thread.current.object_id} before wait"
46
47
  s.wait
48
+ log "###{Thread.current.object_id} after wait"
47
49
  begin
48
50
  job.run_with_lock Job::MAX_RUN_TIME, name
51
+ rescue Exception => e
52
+ log "ERROR: #{e}"
49
53
  ensure
50
54
  unregister_job job
51
- job.connection.release_connection rescue nil
52
55
  end
53
56
  end
54
57
  register_job job, t
55
58
  s.signal
59
+ log "Launched job #{job.name}, there are #{jobs_in_execution} jobs in execution"
56
60
  return true
57
61
  end
58
62
 
@@ -61,24 +65,23 @@ module Delayed
61
65
  if jobs_in_execution > 0
62
66
  margin = 20
63
67
  title = "Jobs In Execution"
64
- puts "\n #{'='*margin} #{title} #{'='*margin} "
65
- puts " There are #{jobs_in_execution} jobs running."
68
+ log "#{'='*margin} #{title} #{'='*margin} "
69
+ log " There are #{jobs_in_execution} jobs running."
66
70
  each_job_in_execution do |job, started_at, thread|
67
71
  duration = Duration.new(Time.now - started_at)
68
- puts "\tJob #{job.id}: #{job.name}"
69
- puts "\t Running on #{thread} (#{thread.status}) for #{duration}"
72
+ log "\tJob #{job.id}: #{job.name}"
73
+ log "\t Running on #{thread} (#{thread.status}) for #{duration}"
70
74
  end
71
- puts " #{'=' * (margin * 2 + title.size + 2)} "
75
+ log "#{'=' * (margin * 2 + title.size + 2)} "
72
76
  else
73
- puts "\n\tThere is no jobs in execution right now!"
77
+ log "\n\tThere is no jobs in execution right now!"
74
78
  end
75
79
  end
76
80
 
77
81
  # Sanity check of dead threads for precaution, but probably won't be
78
82
  # necessary
79
83
  def check_thread_sanity
80
- @jobs.values.each do |v|
81
- thread = v[:thread]
84
+ each_job_in_execution do |job, started_at, thread|
82
85
  unless thread.alive?
83
86
  log "Dead thread? Terminate it!, This should not be happening"
84
87
  thread.terminate
@@ -93,13 +96,13 @@ module Delayed
93
96
 
94
97
  def jobs_ids_in_execution
95
98
  ret = []
96
- each_job_in_execution {|job| ret << job.id }
99
+ each_job_in_execution {|job, x, xx| ret << job.id }
97
100
  ret
98
101
  end
99
102
 
100
103
  def kill_threads!
101
104
  each_job_in_execution do |job, started_at, thread|
102
- puts "Killing #{job.name}"
105
+ log "Killing #{job.name}"
103
106
  thread.terminate
104
107
  end
105
108
  end
@@ -109,10 +112,22 @@ module Delayed
109
112
 
110
113
  # Whether we can or not execute this job
111
114
  def can_execute(job)
112
- return false if is_already_in_execution(job)
115
+ if is_already_in_execution(job)
116
+ log "#{job.name}: already in execution"
117
+ return false
118
+ end
113
119
  object = get_object(job)
114
- object && ! is_there_job_in_execution_for(object) &&
115
- jobs_in_execution < @max_active_jobs
120
+ if ! object
121
+ log "No object #{job.inspect}"
122
+ return false
123
+ elsif is_there_job_in_execution_for(object)
124
+ log "#{job.name}: already a job in execution for #{object}"
125
+ return false
126
+ elsif jobs_in_execution >= @max_active_jobs
127
+ log "#{job.name}: there are already many jobs in execution"
128
+ return false
129
+ end
130
+ true
116
131
  end
117
132
 
118
133
  def is_already_in_execution(job)
@@ -130,20 +145,24 @@ module Delayed
130
145
  end
131
146
 
132
147
  def unregister_job(job)
148
+ log "<-- Let's unregister #{job.name} (#{job.id}) (#{get_object(job)})"
133
149
  @mutex.synchronize do
134
150
  @jobs.delete get_object(job)
135
151
  log "No jobs right now!" if @jobs.size == 0
136
152
  end
153
+ log "<== Unregistered #{job.name} (#{job.id}) (#{get_object(job)})"
137
154
  end
138
155
 
139
156
  def register_job(job, thread)
157
+ log "--> Let's register #{job.name} (#{job.id}) (#{get_object(job)})"
140
158
  @mutex.synchronize do
141
159
  @jobs[get_object(job)] = {
142
160
  :thread => thread,
143
161
  :job => job,
144
- :started_at => Time.now
162
+ :started_at => Time.now
145
163
  }
146
164
  end
165
+ log "==> Registered #{job.name} (#{job.id}) (#{get_object(job)})"
147
166
  end
148
167
 
149
168
  def get_object(job)
@@ -155,4 +174,4 @@ module Delayed
155
174
  end
156
175
  end
157
176
  end # module JobLauncher
158
- end # module Delayed
177
+ end # module Delayed
@@ -2,7 +2,7 @@ module Delayed
2
2
  HIDE_BACKTRACE = true
3
3
 
4
4
  class Worker
5
- SLEEP = 5
5
+ SLEEP = 60
6
6
  DEFAULT_WORKER_NAME = "host:#{Socket.gethostname} pid:#{Process.pid}" rescue "pid:#{Process.pid}"
7
7
  # Indicates that we have catched a signal and we have to exit asap
8
8
  cattr_accessor :exit
@@ -70,9 +70,14 @@ module Delayed
70
70
  else
71
71
  normal_loop
72
72
  end
73
- break if self.exit
73
+ if self.exit
74
+ log "Exit loop"
75
+ break
76
+ end
74
77
  end
75
78
  kill_threads!
79
+ rescue Exception => e
80
+ log "ERROR on worker loop: #{e}"
76
81
  ensure
77
82
  Job.clear_locks! name
78
83
  say "<=== Finishing job worker #{name}"
@@ -106,16 +111,18 @@ module Delayed
106
111
  end
107
112
 
108
113
  def sleep_for_a_little_while
114
+ log "Sleep #{sleep_time.to_i}"
109
115
  sleep(sleep_time.to_i) unless self.exit
116
+ log "Sleep done"
110
117
  end
111
118
 
112
119
  def group_by_loop
120
+ clean_pool_connections
113
121
  check_thread_sanity
114
- jobs_to_execute.each do |job|
115
- if launch job
116
- log "Launched job #{job.name}, there are #{jobs_in_execution} jobs in execution"
117
- end
118
- end
122
+ jobs = jobs_to_execute
123
+ log "There are #{jobs.size} jobs to be executed (#{jobs_in_execution})"
124
+ report_jobs_state
125
+ jobs.each{|j| launch j }
119
126
  sleep_for_a_little_while
120
127
  end
121
128
 
@@ -135,6 +142,13 @@ module Delayed
135
142
  end
136
143
  end
137
144
 
145
+ def clean_pool_connections
146
+ # Workaround for windows (at least xp and 2003 server)
147
+ # With mysql we have overflow of connections
148
+ ActiveRecord::Base.clear_active_connections!
149
+ ActiveRecord::Base.verify_active_connections!
150
+ end
151
+
138
152
  def constraints
139
153
  {:max_run_time => Job::MAX_RUN_TIME,
140
154
  :worker_name => name,
metadata CHANGED
@@ -1,13 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blaxter-delayed_job
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease:
6
- segments:
7
- - 2
8
- - 1
9
- - 8
10
- version: 2.1.8
4
+ version: 2.1.9
11
5
  platform: ruby
12
6
  authors:
13
7
  - "Tobias L\xC3\xBCtke"
@@ -16,7 +10,7 @@ autorequire:
16
10
  bindir: bin
17
11
  cert_chain: []
18
12
 
19
- date: 2011-04-14 00:00:00 +02:00
13
+ date: 2011-07-13 00:00:00 +02:00
20
14
  default_executable:
21
15
  dependencies: []
22
16
 
@@ -42,10 +36,6 @@ files:
42
36
  - lib/delayed/yaml_ext.rb
43
37
  - lib/delayed_job.rb
44
38
  - lib/semaphore.rb
45
- - spec/database.rb
46
- - spec/delayed_method_spec.rb
47
- - spec/job_spec.rb
48
- - spec/story_spec.rb
49
39
  has_rdoc: true
50
40
  homepage: http://github.com/blaxter/delayed_job
51
41
  licenses: []
@@ -59,32 +49,26 @@ rdoc_options:
59
49
  require_paths:
60
50
  - lib
61
51
  required_ruby_version: !ruby/object:Gem::Requirement
62
- none: false
63
52
  requirements:
64
53
  - - ">="
65
54
  - !ruby/object:Gem::Version
66
- hash: 3
67
- segments:
68
- - 0
69
55
  version: "0"
56
+ version:
70
57
  required_rubygems_version: !ruby/object:Gem::Requirement
71
- none: false
72
58
  requirements:
73
59
  - - ">="
74
60
  - !ruby/object:Gem::Version
75
- hash: 3
76
- segments:
77
- - 0
78
61
  version: "0"
62
+ version:
79
63
  requirements: []
80
64
 
81
65
  rubyforge_project:
82
- rubygems_version: 1.4.2
66
+ rubygems_version: 1.3.5
83
67
  signing_key:
84
68
  specification_version: 3
85
69
  summary: Database-backed asynchronous priority queue system -- Extracted from Shopify
86
70
  test_files:
87
71
  - spec/database.rb
72
+ - spec/story_spec.rb
88
73
  - spec/delayed_method_spec.rb
89
74
  - spec/job_spec.rb
90
- - spec/story_spec.rb