queue_classic_plus 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/queue_classic_plus/version.rb +1 -1
- data/lib/queue_classic_plus/worker.rb +31 -33
- data/spec/sample_jobs.rb +1 -1
- data/spec/worker_spec.rb +13 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2401a88a47f9dcc2752db9ed026982cb6724851b
|
4
|
+
data.tar.gz: cecd7627f4a663a4b89a7bf6bfeed08f240cea62
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4da76e72bfed26e9898a1094cc6cc719caaafcb0efb4cfd59d86f4aa02e716b2ecee592f9a384149e557ca4f67ce9583b22dc4555e09befcbd2bea5586ac40ed
|
7
|
+
data.tar.gz: 4c6e779b8546ca3c91e925aa0236623516ea009532a14a9e53bf7f0b895a47a6168ddc243fdf17b5cedb8a1dd2f36f97cccd60629b808ea3c1ad81725df01a58
|
@@ -7,17 +7,8 @@ module QueueClassicPlus
|
|
7
7
|
BACKOFF_WIDTH = 10
|
8
8
|
FailedQueue = QC::Queue.new("failed_jobs")
|
9
9
|
|
10
|
-
def enqueue_failed(job, e)
|
11
|
-
sql = "INSERT INTO #{QC::TABLE_NAME} (q_name, method, args, last_error) VALUES ('failed_jobs', $1, $2, $3)"
|
12
|
-
last_error = e.backtrace ? ([e.message] + e.backtrace ).join("\n") : e.message
|
13
|
-
QC.default_conn_adapter.execute sql, job[:method], JSON.dump(job[:args]), last_error
|
14
|
-
|
15
|
-
QueueClassicPlus.exception_handler.call(e, job)
|
16
|
-
Metrics.increment("qc.errors", source: @q_name)
|
17
|
-
end
|
18
|
-
|
19
10
|
def handle_failure(job, e)
|
20
|
-
QueueClassicPlus.logger.info "Handling exception #{e.message} for job #{job[:id]}"
|
11
|
+
QueueClassicPlus.logger.info "Handling exception #{e.class} - #{e.message} for job #{job[:id]}"
|
21
12
|
|
22
13
|
force_retry = false
|
23
14
|
if connection_error?(e)
|
@@ -33,58 +24,65 @@ module QueueClassicPlus
|
|
33
24
|
force_retry = true
|
34
25
|
end
|
35
26
|
|
36
|
-
|
27
|
+
@failed_job = job
|
37
28
|
|
38
|
-
if force_retry && !(
|
29
|
+
if force_retry && !(failed_job_class.respond_to?(:disable_retries) && failed_job_class.disable_retries)
|
39
30
|
Metrics.increment("qc.force_retry", source: @q_name)
|
40
|
-
|
41
|
-
retry_with_remaining(klass, job, e, 0)
|
31
|
+
retry_with_remaining(e)
|
42
32
|
# The mailers doesn't have a retries_on?
|
43
|
-
|
33
|
+
elsif failed_job_class && failed_job_class.respond_to?(:retries_on?) && failed_job_class.retries_on?(e)
|
44
34
|
Metrics.increment("qc.retry", source: @q_name)
|
45
|
-
|
46
|
-
backoff = (max_retries(klass) - remaining_retries(klass, job)) * BACKOFF_WIDTH
|
47
|
-
|
48
|
-
retry_with_remaining(klass, job, e, backoff)
|
35
|
+
retry_with_remaining(e)
|
49
36
|
else
|
50
|
-
enqueue_failed(
|
37
|
+
enqueue_failed(e)
|
51
38
|
end
|
52
39
|
|
53
|
-
FailedQueue.delete(
|
40
|
+
FailedQueue.delete(@failed_job[:id])
|
54
41
|
end
|
55
42
|
|
56
43
|
private
|
57
44
|
|
58
|
-
def retry_with_remaining(
|
59
|
-
|
60
|
-
|
61
|
-
if @remaining_retries > 0
|
62
|
-
klass.restart_in(backoff, @remaining_retries, *job[:args])
|
45
|
+
def retry_with_remaining(e)
|
46
|
+
if remaining_retries > 0
|
47
|
+
failed_job_class.restart_in(backoff, remaining_retries - 1, *@failed_job[:args])
|
63
48
|
else
|
64
|
-
enqueue_failed(
|
49
|
+
enqueue_failed(e)
|
65
50
|
end
|
66
51
|
end
|
67
52
|
|
68
|
-
def max_retries
|
69
|
-
|
53
|
+
def max_retries
|
54
|
+
failed_job_class.respond_to?(:max_retries) ? failed_job_class.max_retries : 5
|
70
55
|
end
|
71
56
|
|
72
|
-
def remaining_retries
|
73
|
-
@
|
57
|
+
def remaining_retries
|
58
|
+
(@failed_job[:remaining_retries] || max_retries).to_i
|
74
59
|
end
|
75
60
|
|
76
|
-
def
|
61
|
+
def failed_job_class
|
77
62
|
begin
|
78
|
-
Object.const_get(
|
63
|
+
Object.const_get(@failed_job[:method].split('.')[0])
|
79
64
|
rescue NameError
|
80
65
|
nil
|
81
66
|
end
|
82
67
|
end
|
83
68
|
|
69
|
+
def backoff
|
70
|
+
(max_retries - (remaining_retries - 1)) * BACKOFF_WIDTH
|
71
|
+
end
|
72
|
+
|
84
73
|
def connection_error?(e)
|
85
74
|
CONNECTION_ERRORS.any? { |klass| e.kind_of? klass } ||
|
86
75
|
(e.respond_to?(:original_exception) &&
|
87
76
|
CONNECTION_ERRORS.any? { |klass| e.original_exception.kind_of? klass })
|
88
77
|
end
|
78
|
+
|
79
|
+
def enqueue_failed(e)
|
80
|
+
sql = "INSERT INTO #{QC::TABLE_NAME} (q_name, method, args, last_error) VALUES ('failed_jobs', $1, $2, $3)"
|
81
|
+
last_error = e.backtrace ? ([e.message] + e.backtrace ).join("\n") : e.message
|
82
|
+
QC.default_conn_adapter.execute sql, @failed_job[:method], JSON.dump(@failed_job[:args]), last_error
|
83
|
+
|
84
|
+
QueueClassicPlus.exception_handler.call(e, @failed_job)
|
85
|
+
Metrics.increment("qc.errors", source: @q_name)
|
86
|
+
end
|
89
87
|
end
|
90
88
|
end
|
data/spec/sample_jobs.rb
CHANGED
data/spec/worker_spec.rb
CHANGED
@@ -88,6 +88,19 @@ describe QueueClassicPlus::CustomWorker do
|
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
91
|
+
context 'with a custom exception having max: 1 retry' do
|
92
|
+
before { Jobs::Tests::TestJob.enqueue_perform(true) }
|
93
|
+
|
94
|
+
it 'retries' do
|
95
|
+
QueueClassicPlus::Metrics.should_receive(:increment).with('qc.retry', source: nil )
|
96
|
+
Timecop.freeze do
|
97
|
+
worker.work
|
98
|
+
expect(failed_queue.count).to eq 0
|
99
|
+
QueueClassicMatchers::QueueClassicRspec.find_by_args('low', 'Jobs::Tests::TestJob._perform', [true]).first['remaining_retries'].should eq "0"
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
91
104
|
context 'with non-connection based PG jobs' do
|
92
105
|
before { Jobs::Tests::UniqueViolationTestJob.enqueue_perform }
|
93
106
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: queue_classic_plus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Simon Mathieu
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2018-
|
13
|
+
date: 2018-05-14 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: queue_classic
|