perfectqueue 0.8.22 → 0.8.23

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,4 +1,10 @@
1
1
 
2
+ == 2013-01-03 version 0.8.23
3
+
4
+ * rdb_compat: added retry_count field
5
+ * Added Backend#release
6
+
7
+
2
8
  == 2012-10-22 version 0.8.22
3
9
 
4
10
  * rdb_compat backend fixed heartbeat method to check created_at properly
@@ -45,7 +45,7 @@ module PerfectQueue
45
45
 
46
46
  if config[:disable_resource_limit]
47
47
  @sql = <<SQL
48
- SELECT id, timeout, data, created_at, resource
48
+ SELECT id, timeout, data, created_at, retry_count, resource
49
49
  FROM `#{@table}`
50
50
  WHERE timeout <= ? AND timeout <= ? AND created_at IS NOT NULL
51
51
  ORDER BY timeout ASC
@@ -53,7 +53,7 @@ LIMIT ?
53
53
  SQL
54
54
  else
55
55
  @sql = <<SQL
56
- SELECT id, timeout, data, created_at, resource, max_running, max_running/running AS weight
56
+ SELECT id, timeout, data, created_at, retry_count, resource, max_running, max_running/running AS weight
57
57
  FROM `#{@table}`
58
58
  LEFT JOIN (
59
59
  SELECT resource AS res, COUNT(1) AS running
@@ -100,6 +100,7 @@ SQL
100
100
  timeout INT NOT NULL,
101
101
  data BLOB NOT NULL,
102
102
  created_at INT,
103
+ retry_count INT NOT NULL DEFAULT 0,
103
104
  resource VARCHAR(256),
104
105
  max_running INT,
105
106
  PRIMARY KEY (id)
@@ -114,7 +115,7 @@ SQL
114
115
  now = (options[:now] || Time.now).to_i
115
116
 
116
117
  connect {
117
- row = @db.fetch("SELECT timeout, data, created_at, resource, max_running FROM `#{@table}` WHERE id=? LIMIT 1", key).first
118
+ row = @db.fetch("SELECT timeout, data, created_at, retry_count, resource, max_running FROM `#{@table}` WHERE id=? LIMIT 1", key).first
118
119
  unless row
119
120
  raise NotFoundError, "task key=#{key} does no exist"
120
121
  end
@@ -133,8 +134,8 @@ SQL
133
134
  now = (options[:now] || Time.now).to_i
134
135
 
135
136
  connect {
136
- #@db.fetch("SELECT id, timeout, data, created_at, resource FROM `#{@table}` WHERE !(created_at IS NULL AND timeout <= ?) ORDER BY timeout ASC;", now) {|row|
137
- @db.fetch("SELECT id, timeout, data, created_at, resource, max_running FROM `#{@table}` ORDER BY timeout ASC", now) {|row|
137
+ #@db.fetch("SELECT id, timeout, data, created_at, retry_count, resource FROM `#{@table}` WHERE !(created_at IS NULL AND timeout <= ?) ORDER BY timeout ASC;", now) {|row|
138
+ @db.fetch("SELECT id, timeout, data, created_at, retry_count, resource, max_running FROM `#{@table}` ORDER BY timeout ASC", now) {|row|
138
139
  attributes = create_attributes(now, row)
139
140
  task = TaskWithMetadata.new(@client, row[:id], attributes)
140
141
  yield task
@@ -200,7 +201,7 @@ SQL
200
201
  return nil
201
202
  end
202
203
 
203
- sql = "UPDATE `#{@table}` SET timeout = ? WHERE id IN ("
204
+ sql = "UPDATE `#{@table}` SET timeout=?, retry_count=(retry_count+1) WHERE id IN ("
204
205
  params = [sql, next_timeout]
205
206
  tasks.each {|t| params << t.key }
206
207
  sql << (1..tasks.size).map { '?' }.join(',')
@@ -274,6 +275,10 @@ SQL
274
275
  nil
275
276
  end
276
277
 
278
+ def release(task_token, alive_time, options)
279
+ heartbeat(task_token, alive_time, options)
280
+ end
281
+
277
282
  protected
278
283
  def connect(&block)
279
284
  #now = Time.now.to_i
@@ -282,15 +287,15 @@ SQL
282
287
  # @db.disconnect
283
288
  #end
284
289
  #@last_time = now
285
- retry_count = 0
290
+ count = 0
286
291
  begin
287
292
  block.call
288
293
  rescue
289
294
  # workaround for "Mysql2::Error: Deadlock found when trying to get lock; try restarting transaction" error
290
295
  if $!.to_s.include?('try restarting transaction')
291
296
  err = ([$!] + $!.backtrace.map {|bt| " #{bt}" }).join("\n")
292
- retry_count += 1
293
- if retry_count < MAX_RETRY
297
+ count += 1
298
+ if count < MAX_RETRY
294
299
  STDERR.puts err + "\n retrying."
295
300
  sleep rand
296
301
  retry
@@ -342,6 +347,7 @@ SQL
342
347
  :type => type,
343
348
  :user => row[:resource],
344
349
  :timeout => row[:timeout],
350
+ :retry_count => row[:retry_count],
345
351
  :max_running => row[:max_running],
346
352
  :message => nil, # not supported
347
353
  :node => nil, # not supported
@@ -99,13 +99,13 @@ module PerfectQueue
99
99
  def release(task_token, options={})
100
100
  alive_time = options[:alive_time] || 0
101
101
 
102
- @backend.heartbeat(task_token, alive_time, options)
102
+ @backend.release(task_token, alive_time, options)
103
103
  end
104
104
 
105
105
  def retry(task_token, options={})
106
106
  alive_time = options[:retry_wait] || @retry_wait
107
107
 
108
- @backend.heartbeat(task_token, alive_time, options)
108
+ @backend.release(task_token, alive_time, options)
109
109
  end
110
110
 
111
111
  def close
@@ -40,6 +40,10 @@ module PerfectQueue
40
40
  @attributes[:user]
41
41
  end
42
42
 
43
+ def retry_count
44
+ @attributes[:retry_count]
45
+ end
46
+
43
47
  def created_at
44
48
  if t = @attributes[:created_at]
45
49
  return Time.at(t)
@@ -1,3 +1,3 @@
1
1
  module PerfectQueue
2
- VERSION = "0.8.22"
2
+ VERSION = "0.8.23"
3
3
  end
data/spec/queue_spec.rb CHANGED
@@ -39,19 +39,22 @@ describe Queue do
39
39
  task01.finished?.should == false
40
40
  task01.type == 'type1'
41
41
  task01.key.should == 'task01'
42
+ task01.retry_count.should == 0
42
43
  task01.data["a"].should == 1
43
44
 
44
- t2 = a.shift
45
- t2.finished?.should == false
46
- t2.type == 'type1'
47
- t2.key.should == 'task02'
48
- t2.data["a"].should == 2
45
+ task02 = a.shift
46
+ task02.finished?.should == false
47
+ task02.type == 'type1'
48
+ task02.key.should == 'task02'
49
+ task01.retry_count.should == 0
50
+ task02.data["a"].should == 2
49
51
 
50
- t3 = a.shift
51
- t3.finished?.should == false
52
- t3.type == 'type1'
53
- t3.key.should == 'task03'
54
- t3.data["a"].should == 3
52
+ task03 = a.shift
53
+ task03.finished?.should == false
54
+ task03.type == 'type1'
55
+ task03.key.should == 'task03'
56
+ task01.retry_count.should == 0
57
+ task03.data["a"].should == 3
55
58
 
56
59
  a.empty?.should == true
57
60
  end
@@ -65,11 +68,11 @@ describe Queue do
65
68
  task01 = queue.poll(:now=>now+10)
66
69
  task01.key.should == 'task01'
67
70
 
68
- t2 = queue.poll(:now=>now+10)
69
- t2.key.should == 'task02'
71
+ task02 = queue.poll(:now=>now+10)
72
+ task02.key.should == 'task02'
70
73
 
71
- t3 = queue.poll(:now=>now+10)
72
- t3.key.should == 'task03'
74
+ task03 = queue.poll(:now=>now+10)
75
+ task03.key.should == 'task03'
73
76
 
74
77
  t4 = queue.poll(:now=>now+10)
75
78
  t4.should == nil
@@ -81,14 +84,16 @@ describe Queue do
81
84
 
82
85
  task01 = queue.poll(:now=>now+10)
83
86
  task01.key.should == 'task01'
87
+ task01.retry_count.should == 0
84
88
 
85
- t2 = queue.poll(:now=>now+10)
86
- t2.should == nil
89
+ task02 = queue.poll(:now=>now+10)
90
+ task02.should == nil
87
91
 
88
92
  task01.release!(:now=>now+10)
89
93
 
90
- t3 = queue.poll(:now=>now+11)
91
- t3.key.should == 'task01'
94
+ task03 = queue.poll(:now=>now+11)
95
+ task03.key.should == 'task01'
96
+ task03.retry_count.should == 1
92
97
  end
93
98
 
94
99
  it 'timeout' do
@@ -97,12 +102,14 @@ describe Queue do
97
102
 
98
103
  task01 = queue.poll(:now=>now+10, :alive_time=>10)
99
104
  task01.key.should == 'task01'
105
+ task01.retry_count.should == 0
100
106
 
101
- t2 = queue.poll(:now=>now+15)
102
- t2.should == nil
107
+ task02 = queue.poll(:now=>now+15)
108
+ task02.should == nil
103
109
 
104
- t3 = queue.poll(:now=>now+20)
105
- t3.key.should == 'task01'
110
+ task03 = queue.poll(:now=>now+20)
111
+ task03.key.should == 'task01'
112
+ task03.retry_count.should == 1
106
113
  end
107
114
 
108
115
  it 'heartbeat' do
@@ -111,14 +118,16 @@ describe Queue do
111
118
 
112
119
  task01 = queue.poll(:now=>now+10, :alive_time=>10)
113
120
  task01.key.should == 'task01'
121
+ task01.retry_count.should == 0
114
122
 
115
123
  task01.heartbeat!(:alive_time=>15, :now=>now+10)
116
124
 
117
- t2 = queue.poll(:now=>now+20)
118
- t2.should == nil
125
+ task02 = queue.poll(:now=>now+20)
126
+ task02.should == nil
119
127
 
120
- t3 = queue.poll(:now=>now+30)
121
- t3.key.should == 'task01'
128
+ task03 = queue.poll(:now=>now+30)
129
+ task03.key.should == 'task01'
130
+ task03.retry_count.should == 1
122
131
  end
123
132
 
124
133
  it 'retry' do
@@ -127,14 +136,16 @@ describe Queue do
127
136
 
128
137
  task01 = queue.poll(:now=>now+10, :alive_time=>10)
129
138
  task01.key.should == 'task01'
139
+ task01.retry_count.should == 0
130
140
 
131
141
  task01.retry!(:retry_wait=>15, :now=>now+10)
132
142
 
133
- t2 = queue.poll(:now=>now+20)
134
- t2.should == nil
143
+ task02 = queue.poll(:now=>now+20)
144
+ task02.should == nil
135
145
 
136
- t3 = queue.poll(:now=>now+30)
137
- t3.key.should == 'task01'
146
+ task03 = queue.poll(:now=>now+30)
147
+ task03.key.should == 'task01'
148
+ task03.retry_count.should == 1
138
149
  end
139
150
 
140
151
  it 'froce_finish' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: perfectqueue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.22
4
+ version: 0.8.23
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-10-22 00:00:00.000000000 Z
12
+ date: 2013-01-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sequel