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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7d499c51f34d828e8abf2c5e405d82ea3ae49b19
4
- data.tar.gz: 335713b0d79d1b54b9dc342a36c88c2008fccab2
3
+ metadata.gz: 2401a88a47f9dcc2752db9ed026982cb6724851b
4
+ data.tar.gz: cecd7627f4a663a4b89a7bf6bfeed08f240cea62
5
5
  SHA512:
6
- metadata.gz: a529d8ca6f1a179f186f6b57bc90543bec9607a2c87e700db996a87301bea7073b353880ade9796d93d733573112f5545ef1e22440c29156127bd11edeb87212
7
- data.tar.gz: c23fde5d11939355a2c8a90a92001f336da6ecf3c422aca1bd11ebed5e55f019b0d8fc5720495ad65439a95b79c11f77cd00f4433533a8d7baf38b19f807fcc7
6
+ metadata.gz: 4da76e72bfed26e9898a1094cc6cc719caaafcb0efb4cfd59d86f4aa02e716b2ecee592f9a384149e557ca4f67ce9583b22dc4555e09befcbd2bea5586ac40ed
7
+ data.tar.gz: 4c6e779b8546ca3c91e925aa0236623516ea009532a14a9e53bf7f0b895a47a6168ddc243fdf17b5cedb8a1dd2f36f97cccd60629b808ea3c1ad81725df01a58
@@ -1,3 +1,3 @@
1
1
  module QueueClassicPlus
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -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
- klass = job_klass(job)
27
+ @failed_job = job
37
28
 
38
- if force_retry && !(klass.respond_to?(:disable_retries) && klass.disable_retries)
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
- elsif klass && klass.respond_to?(:retries_on?) && klass.retries_on?(e)
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(job, e)
37
+ enqueue_failed(e)
51
38
  end
52
39
 
53
- FailedQueue.delete(job[:id])
40
+ FailedQueue.delete(@failed_job[:id])
54
41
  end
55
42
 
56
43
  private
57
44
 
58
- def retry_with_remaining(klass, job, e, backoff)
59
- @remaining_retries = remaining_retries(klass, job)
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(job, e)
49
+ enqueue_failed(e)
65
50
  end
66
51
  end
67
52
 
68
- def max_retries(klass)
69
- klass.respond_to?(:max_retries) ? klass.max_retries : 5
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(klass, job)
73
- @remaining_retries ? @remaining_retries - 1 : (job[:remaining_retries] || max_retries(klass)).to_i - 1
57
+ def remaining_retries
58
+ (@failed_job[:remaining_retries] || max_retries).to_i
74
59
  end
75
60
 
76
- def job_klass(job)
61
+ def failed_job_class
77
62
  begin
78
- Object.const_get(job[:method].split('.')[0])
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
@@ -35,7 +35,7 @@ module Jobs
35
35
 
36
36
  class TestJob < QueueClassicPlus::Base
37
37
  @queue = :low
38
- retry! on: SomeException, max: 5
38
+ retry! on: SomeException, max: 1
39
39
 
40
40
  def self.perform should_raise
41
41
  raise SomeException if should_raise
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.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-04-18 00:00:00.000000000 Z
13
+ date: 2018-05-14 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: queue_classic