perfectqueue 0.8.23 → 0.8.24

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.24
3
+
4
+ * rdb_compat: removed retry_count field
5
+ * Added AcquiredTask#update_data!
6
+
7
+
2
8
  == 2013-01-03 version 0.8.23
3
9
 
4
10
  * rdb_compat: added retry_count field
@@ -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, retry_count, resource
48
+ SELECT id, timeout, data, created_at, 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, retry_count, resource, max_running, max_running/running AS weight
56
+ SELECT id, timeout, data, created_at, 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,7 +100,6 @@ 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,
104
103
  resource VARCHAR(256),
105
104
  max_running INT,
106
105
  PRIMARY KEY (id)
@@ -115,7 +114,7 @@ SQL
115
114
  now = (options[:now] || Time.now).to_i
116
115
 
117
116
  connect {
118
- row = @db.fetch("SELECT timeout, data, created_at, retry_count, resource, max_running FROM `#{@table}` WHERE id=? LIMIT 1", key).first
117
+ row = @db.fetch("SELECT timeout, data, created_at, resource, max_running FROM `#{@table}` WHERE id=? LIMIT 1", key).first
119
118
  unless row
120
119
  raise NotFoundError, "task key=#{key} does no exist"
121
120
  end
@@ -134,8 +133,8 @@ SQL
134
133
  now = (options[:now] || Time.now).to_i
135
134
 
136
135
  connect {
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|
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|
139
138
  attributes = create_attributes(now, row)
140
139
  task = TaskWithMetadata.new(@client, row[:id], attributes)
141
140
  yield task
@@ -201,7 +200,7 @@ SQL
201
200
  return nil
202
201
  end
203
202
 
204
- sql = "UPDATE `#{@table}` SET timeout=?, retry_count=(retry_count+1) WHERE id IN ("
203
+ sql = "UPDATE `#{@table}` SET timeout=? WHERE id IN ("
205
204
  params = [sql, next_timeout]
206
205
  tasks.each {|t| params << t.key }
207
206
  sql << (1..tasks.size).map { '?' }.join(',')
@@ -256,9 +255,19 @@ SQL
256
255
  now = (options[:now] || Time.now).to_i
257
256
  next_timeout = now + alive_time
258
257
  key = task_token.key
258
+ data = options[:data]
259
+
260
+ sql = "UPDATE `#{@table}` SET timeout=?"
261
+ params = [sql, next_timeout]
262
+ if data
263
+ sql << ", data=?"
264
+ params << data.to_json
265
+ end
266
+ sql << " WHERE id=? AND created_at IS NOT NULL"
267
+ params << key
259
268
 
260
269
  connect {
261
- n = @db["UPDATE `#{@table}` SET timeout=? WHERE id=? AND created_at IS NOT NULL", next_timeout, key].update
270
+ n = @db[*params].update
262
271
  if n <= 0
263
272
  row = @db.fetch("SELECT id, timeout, created_at FROM `#{@table}` WHERE id=? LIMIT 1", key).first
264
273
  if row == nil
@@ -347,7 +356,6 @@ SQL
347
356
  :type => type,
348
357
  :user => row[:resource],
349
358
  :timeout => row[:timeout],
350
- :retry_count => row[:retry_count],
351
359
  :max_running => row[:max_running],
352
360
  :message => nil, # not supported
353
361
  :node => nil, # not supported
@@ -90,6 +90,13 @@ module PerfectQueue
90
90
  @client.retry(@task_token, options)
91
91
  end
92
92
 
93
+ def update_data!(hash)
94
+ data = @attributes[:data] || {}
95
+ merged = data.merge(hash)
96
+ heartbeat!(:data => merged)
97
+ @attributes[:data] = merged
98
+ end
99
+
93
100
  #def to_json
94
101
  # [@key, @task_token, @attributes].to_json
95
102
  #end
@@ -40,10 +40,6 @@ module PerfectQueue
40
40
  @attributes[:user]
41
41
  end
42
42
 
43
- def retry_count
44
- @attributes[:retry_count]
45
- end
46
-
47
43
  def created_at
48
44
  if t = @attributes[:created_at]
49
45
  return Time.at(t)
@@ -60,16 +60,6 @@ module PerfectQueue
60
60
  @mutex.synchronize {
61
61
  @task = task
62
62
  @last_task_heartbeat = Time.now.to_i
63
- @heartbeat_message = nil
64
- }
65
- end
66
-
67
- # callback
68
- def set_heartbeat_message(task, message)
69
- @mutex.synchronize {
70
- if task == @task
71
- @heartbeat_message = message
72
- end
73
63
  }
74
64
  end
75
65
 
@@ -106,6 +96,17 @@ module PerfectQueue
106
96
  }
107
97
  end
108
98
 
99
+ # callback
100
+ def external_task_heartbeat(task, &block)
101
+ @mutex.synchronize {
102
+ if task == @task
103
+ ret = block.call if block
104
+ @last_task_heartbeat = Time.now.to_i
105
+ end
106
+ ret
107
+ }
108
+ end
109
+
109
110
  def run
110
111
  @mutex.synchronize {
111
112
  now = Time.now.to_i
@@ -144,8 +145,7 @@ module PerfectQueue
144
145
 
145
146
  private
146
147
  def task_heartbeat
147
- @task.heartbeat! :message => @heartbeat_message
148
- @heartbeat_message = nil
148
+ @task.heartbeat!
149
149
  rescue
150
150
  # finished, cancel_requested, preempted, etc.
151
151
  kill_task($!)
@@ -157,14 +157,6 @@ module PerfectQueue
157
157
  attr_accessor :task_monitor
158
158
  attr_accessor :runner
159
159
 
160
- def heartbeat_message=(message)
161
- @heartbeat_message = message
162
- @task_monitor.set_heartbeat_message(self, message)
163
- message
164
- end
165
-
166
- attr_reader :heartbeat_message
167
-
168
160
  def finish!(*args, &block)
169
161
  @log.info "finished task=#{self.key}" if @log
170
162
  @task_monitor.task_finished(self) {
@@ -192,6 +184,13 @@ module PerfectQueue
192
184
  super(*args, &block)
193
185
  }
194
186
  end
187
+
188
+ def update_data!(hash)
189
+ @log.info "update data #{hash.inspect} task=#{self.key}" if @log
190
+ @task_monitor.external_task_heartbeat(self) {
191
+ super(hash)
192
+ }
193
+ end
195
194
  end
196
195
 
197
196
  end
@@ -1,3 +1,3 @@
1
1
  module PerfectQueue
2
- VERSION = "0.8.23"
2
+ VERSION = "0.8.24"
3
3
  end
data/spec/queue_spec.rb CHANGED
@@ -39,21 +39,18 @@ 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
43
42
  task01.data["a"].should == 1
44
43
 
45
44
  task02 = a.shift
46
45
  task02.finished?.should == false
47
46
  task02.type == 'type1'
48
47
  task02.key.should == 'task02'
49
- task01.retry_count.should == 0
50
48
  task02.data["a"].should == 2
51
49
 
52
50
  task03 = a.shift
53
51
  task03.finished?.should == false
54
52
  task03.type == 'type1'
55
53
  task03.key.should == 'task03'
56
- task01.retry_count.should == 0
57
54
  task03.data["a"].should == 3
58
55
 
59
56
  a.empty?.should == true
@@ -84,7 +81,6 @@ describe Queue do
84
81
 
85
82
  task01 = queue.poll(:now=>now+10)
86
83
  task01.key.should == 'task01'
87
- task01.retry_count.should == 0
88
84
 
89
85
  task02 = queue.poll(:now=>now+10)
90
86
  task02.should == nil
@@ -93,7 +89,6 @@ describe Queue do
93
89
 
94
90
  task03 = queue.poll(:now=>now+11)
95
91
  task03.key.should == 'task01'
96
- task03.retry_count.should == 1
97
92
  end
98
93
 
99
94
  it 'timeout' do
@@ -102,14 +97,12 @@ describe Queue do
102
97
 
103
98
  task01 = queue.poll(:now=>now+10, :alive_time=>10)
104
99
  task01.key.should == 'task01'
105
- task01.retry_count.should == 0
106
100
 
107
101
  task02 = queue.poll(:now=>now+15)
108
102
  task02.should == nil
109
103
 
110
104
  task03 = queue.poll(:now=>now+20)
111
105
  task03.key.should == 'task01'
112
- task03.retry_count.should == 1
113
106
  end
114
107
 
115
108
  it 'heartbeat' do
@@ -118,7 +111,6 @@ describe Queue do
118
111
 
119
112
  task01 = queue.poll(:now=>now+10, :alive_time=>10)
120
113
  task01.key.should == 'task01'
121
- task01.retry_count.should == 0
122
114
 
123
115
  task01.heartbeat!(:alive_time=>15, :now=>now+10)
124
116
 
@@ -127,7 +119,6 @@ describe Queue do
127
119
 
128
120
  task03 = queue.poll(:now=>now+30)
129
121
  task03.key.should == 'task01'
130
- task03.retry_count.should == 1
131
122
  end
132
123
 
133
124
  it 'retry' do
@@ -136,7 +127,6 @@ describe Queue do
136
127
 
137
128
  task01 = queue.poll(:now=>now+10, :alive_time=>10)
138
129
  task01.key.should == 'task01'
139
- task01.retry_count.should == 0
140
130
 
141
131
  task01.retry!(:retry_wait=>15, :now=>now+10)
142
132
 
@@ -145,7 +135,6 @@ describe Queue do
145
135
 
146
136
  task03 = queue.poll(:now=>now+30)
147
137
  task03.key.should == 'task01'
148
- task03.retry_count.should == 1
149
138
  end
150
139
 
151
140
  it 'froce_finish' do
@@ -254,5 +243,26 @@ describe Queue do
254
243
  tasks = queue.poll_multi(:now=>now+10, :alive_time=>10, :max_acquire=>2)
255
244
  tasks.should == nil
256
245
  end
246
+
247
+ it 'data' do
248
+ now = Time.now.to_i
249
+ queue.submit('task01', 'type1', {"a"=>1}, :now=>now)
250
+
251
+ task01 = queue.poll(:now=>now+10)
252
+ task01.key.should == 'task01'
253
+ task01.data.should == {"a"=>1}
254
+
255
+ task01.update_data!({"b"=>2})
256
+ task01.data.should == {"a"=>1, "b"=>2}
257
+
258
+ task01.update_data!({"a"=>3,"c"=>4})
259
+ task01.data.should == {"a"=>3, "b"=>2, "c"=>4}
260
+
261
+ task01.release!
262
+
263
+ task01 = queue.poll(:now=>now+10)
264
+ task01.key.should == 'task01'
265
+ task01.data.should == {"a"=>3, "b"=>2, "c"=>4}
266
+ end
257
267
  end
258
268
 
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.23
4
+ version: 0.8.24
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-01-03 00:00:00.000000000 Z
12
+ date: 2013-01-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sequel