Skip to content

Commit d807256

Browse files
committed
capture job_attempts on Worker
1 parent cb3cdbc commit d807256

5 files changed

Lines changed: 37 additions & 7 deletions

File tree

app/controllers/cloudtasker/worker_controller.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ def json_payload
6363
#
6464
def payload
6565
# Return content parsed as JSON and add job retries count
66-
@payload ||= JSON.parse(json_payload).merge(job_retries: job_retries, task_id: task_id)
66+
@payload ||= JSON.parse(json_payload).merge(
67+
job_retries: job_retries, job_attempts: job_attempts, task_id: task_id
68+
)
6769
end
6870

6971
#
@@ -75,6 +77,16 @@ def job_retries
7577
request.headers[Cloudtasker::Config::RETRY_HEADER].to_i
7678
end
7779

80+
#
81+
# Extract the number of times this task was attempted at runtime.
82+
# This includes all attempts (including 50x errors).
83+
#
84+
# @return [Integer] The number of attempts.
85+
#
86+
def job_attempts
87+
request.headers[Cloudtasker::Config::ATTEMPT_HEADER].to_i
88+
end
89+
7890
#
7991
# Return the Google Cloud Task ID from headers.
8092
#

lib/cloudtasker/config.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ class Config
1919
# Retry header in Cloud Task responses
2020
#
2121
# Definitions:
22-
# X-CloudTasks-TaskRetryCount: total number of retries (including 504 "instance unreachable")
23-
# X-CloudTasks-TaskExecutionCount: number of non-503 retries (= actual number of job failures)
22+
# X-CloudTasks-TaskRetryCount: total number of retries (including 50x errors)
23+
# X-CloudTasks-TaskExecutionCount: number of non-50x retries (= actual number of job failures)
2424
#
2525
RETRY_HEADER = 'X-Cloudtasks-Taskexecutioncount'
26+
ATTEMPT_HEADER = 'X-CloudTasks-TaskRetryCount'
2627

2728
# Cloud Task ID header
2829
TASK_ID_HEADER = 'X-CloudTasks-TaskName'

lib/cloudtasker/worker.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def self.included(base)
88
base.extend(ClassMethods)
99
base.attr_writer :job_queue
1010
base.attr_accessor :job_args, :job_id, :job_meta, :job_reenqueued, :job_retries,
11-
:perform_started_at, :perform_ended_at, :task_id
11+
:job_attempts, :perform_started_at, :perform_ended_at, :task_id
1212
end
1313

1414
#
@@ -47,7 +47,10 @@ def self.from_hash(hash)
4747
return nil unless worker_klass.include?(self)
4848

4949
# Return instantiated worker
50-
worker_klass.new(**payload.slice(:job_queue, :job_args, :job_id, :job_meta, :job_retries, :task_id))
50+
worker_klass.new(**payload.slice(
51+
:job_queue, :job_args, :job_id, :job_meta,
52+
:job_retries, :job_attempts, :task_id
53+
))
5154
rescue NameError
5255
nil
5356
end
@@ -176,11 +179,13 @@ def max_retries
176179
# @param [Array<any>] job_args The list of perform args.
177180
# @param [String] job_id A unique ID identifying this job.
178181
#
179-
def initialize(job_queue: nil, job_args: nil, job_id: nil, job_meta: {}, job_retries: 0, task_id: nil)
182+
def initialize(job_queue: nil, job_args: nil, job_id: nil, job_meta: {}, job_retries: 0, job_attempts: 0,
183+
task_id: nil)
180184
@job_args = job_args || []
181185
@job_id = job_id || SecureRandom.uuid
182186
@job_meta = MetaStore.new(job_meta)
183187
@job_retries = job_retries || 0
188+
@job_attempts = job_attempts || 0
184189
@job_queue = job_queue
185190
@task_id = task_id
186191
end
@@ -331,6 +336,7 @@ def to_h
331336
job_args: job_args,
332337
job_meta: job_meta.to_h,
333338
job_retries: job_retries,
339+
job_attempts: job_attempts,
334340
job_queue: job_queue,
335341
task_id: task_id
336342
}

spec/cloudtasker/worker_controller_spec.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,25 @@
2121
end
2222
let(:mime_type) { :json }
2323
let(:request_body) { payload.to_json }
24-
let(:expected_payload) { payload.merge(job_retries: retries, task_id: task_id) }
24+
let(:expected_payload) { payload.merge(job_retries: retries, job_attempts: attempts, task_id: task_id) }
2525
let(:task_id) { 'ab2341f' }
2626
let(:id) { '111' }
2727
let(:worker_class_name) { 'TestWorker' }
2828
let(:args) { [1, 2] }
2929
let(:meta) { { 'foo' => 'bar' } }
3030
let(:retries) { 3 }
31+
let(:attempts) { 5 }
3132
let(:queue) { 'some-queue' }
3233
let(:signature) { Cloudtasker::Authenticator.sign_payload(payload.to_json) }
3334

3435
let(:signature_header) { "HTTP_#{Cloudtasker::Config::CT_SIGNATURE_HEADER.tr('-', '_').upcase}" }
3536
let(:env_retries_header) { "HTTP_#{Cloudtasker::Config::RETRY_HEADER.tr('-', '_').upcase}" }
37+
let(:env_attempts_header) { "HTTP_#{Cloudtasker::Config::ATTEMPT_HEADER.tr('-', '_').upcase}" }
3638
let(:env_task_id_header) { "HTTP_#{Cloudtasker::Config::TASK_ID_HEADER.tr('-', '_').upcase}" }
3739

3840
before do
3941
request.env[env_retries_header] = retries
42+
request.env[env_attempts_header] = attempts
4043
request.env[env_task_id_header] = task_id
4144
end
4245

spec/cloudtasker/worker_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@
3333
let(:job_retries) { 3 }
3434
let(:job_queue) { 'critical' }
3535
let(:worker_class_name) { worker_class.to_s }
36+
let(:job_attempts) { 5 }
3637
let(:worker_hash) do
3738
{
3839
'worker' => worker_class_name,
3940
'job_id' => job_id,
4041
'job_args' => job_args,
4142
'job_meta' => job_meta,
4243
'job_retries' => job_retries,
44+
'job_attempts' => job_attempts,
4345
'job_queue' => job_queue,
4446
'task_id' => task_id
4547
}
@@ -53,6 +55,7 @@
5355
job_args: job_args,
5456
job_meta: eq(job_meta),
5557
job_retries: job_retries,
58+
job_attempts: job_attempts,
5659
task_id: task_id
5760
}
5861
end
@@ -233,6 +236,7 @@
233236
let(:args) { [1, 2] }
234237
let(:meta) { { foo: 'bar' } }
235238
let(:retries) { 3 }
239+
let(:attempts) { 5 }
236240
let(:queue) { 'critical' }
237241

238242
context 'without args' do
@@ -243,6 +247,7 @@
243247
job_args: [],
244248
job_id: be_a(String),
245249
job_retries: 0,
250+
job_attempts: 0,
246251
task_id: nil
247252
}
248253
end
@@ -258,6 +263,7 @@
258263
job_id: id,
259264
job_meta: meta,
260265
job_retries: retries,
266+
job_attempts: attempts,
261267
task_id: task_id
262268
}
263269
end
@@ -268,6 +274,7 @@
268274
job_id: id,
269275
job_meta: eq(meta),
270276
job_retries: retries,
277+
job_attempts: attempts,
271278
task_id: task_id
272279
}
273280
end
@@ -597,6 +604,7 @@
597604
job_args: worker.job_args,
598605
job_meta: worker.job_meta.to_h,
599606
job_retries: worker.job_retries,
607+
job_attempts: worker.job_attempts,
600608
job_queue: worker.job_queue,
601609
task_id: task_id
602610
}

0 commit comments

Comments
 (0)