chained_job 0.5.0 → 0.7.0

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
  SHA256:
3
- metadata.gz: dd4890f548ef2ee47ff48180b593134f61b83fedc9017733346ab15d518d2c8d
4
- data.tar.gz: c315bdee23b062ba3834bd23f8fcf18f07b8c7ca111dff9082d1bd251a5071e7
3
+ metadata.gz: 627fdbd88729785e5f036aa8eead67eba9e829ab8fff3fa7d7792249a9dcea0e
4
+ data.tar.gz: d3e9721bfae1f4676428d73a3efba5c417c89527f2750de10ef859c0846e567e
5
5
  SHA512:
6
- metadata.gz: 0205f904ea84911f3258ac4798f52077c9e61e6850165db93b01b461ecbbf734d385093741211583aabddffcdda4e2cd4bc560ec10033dde9d5467f0232a28f5
7
- data.tar.gz: 1e65c4a589d8db54ab53a76d80da2049a619945ed4a75428edf8aba28309a3e90b9ec222bb916fcd43842c945c38bf7fa9b881e9a5e2adefc90be92562fcf4eb
6
+ metadata.gz: cadabdd000cb69d656b930d0ad960af23901634c02725e6299669aa963ec9e0d732b98d6adce7785d1b1f37b8e0bab4453fa7b6ed558dbe208d7bbe07d3a7d95
7
+ data.tar.gz: c1b8e3ed98fe248078ac19bcc07db5c0a04bd2b747fb6b88f43698835a6da805db33b7b36685d5a38432fc42613e329418080035e564b1b95c56c946b9cafc5f
@@ -4,16 +4,16 @@ require 'chained_job/helpers'
4
4
 
5
5
  module ChainedJob
6
6
  class CleanUpQueue
7
- def self.run(job_class)
8
- new(job_class).run
7
+ def self.run(job_arguments_key)
8
+ new(job_arguments_key).run
9
9
  end
10
10
 
11
11
  TRIM_STEP_SIZE = 1_000
12
12
 
13
- attr_reader :job_class
13
+ attr_reader :job_arguments_key
14
14
 
15
- def initialize(job_class)
16
- @job_class = job_class
15
+ def initialize(job_arguments_key)
16
+ @job_arguments_key = job_arguments_key
17
17
  end
18
18
 
19
19
  # rubocop:disable Metrics/AbcSize
@@ -39,7 +39,7 @@ module ChainedJob
39
39
  end
40
40
 
41
41
  def job_key
42
- @job_key ||= Helpers.job_key(job_class)
42
+ @job_key ||= Helpers.job_key(job_arguments_key)
43
43
  end
44
44
  end
45
45
  end
@@ -4,8 +4,8 @@ module ChainedJob
4
4
  module Helpers
5
5
  module_function
6
6
 
7
- def job_key(job_class)
8
- "chained_job:#{job_class}"
7
+ def job_key(job_arguments_key)
8
+ "chained_job:#{job_arguments_key}"
9
9
  end
10
10
 
11
11
  def redis_key(job_key, tag)
@@ -15,5 +15,9 @@ module ChainedJob
15
15
  def tag_list(prefix)
16
16
  "#{prefix}:tags"
17
17
  end
18
+
19
+ def serialize(arguments)
20
+ arguments.map { |argument| Marshal.dump(argument) }
21
+ end
18
22
  end
19
23
  end
@@ -9,16 +9,20 @@ module ChainedJob
9
9
  base.queue_as ChainedJob.config.queue if ChainedJob.config.queue
10
10
  end
11
11
 
12
- def perform(worker_id = nil, tag = nil)
12
+ def perform(args = {}, worker_id = nil, tag = nil)
13
+ unless Hash === args
14
+ # backward compatibility
15
+ args, worker_id, tag = {}, args, worker_id
16
+ end
13
17
  if worker_id
14
- ChainedJob::Process.run(self, worker_id, tag)
18
+ ChainedJob::Process.run(args, self, job_arguments_key, worker_id, tag)
15
19
  else
16
- ChainedJob::StartChains.run(self.class, arguments_array, parallelism)
20
+ ChainedJob::StartChains.run(args, self.class, job_arguments_key, arguments_array(args), parallelism)
17
21
  end
18
22
  end
19
23
 
20
- def arguments_array
21
- options = { job_class: self.class }
24
+ def arguments_array(args)
25
+ options = { job_class: self.class, args: args }
22
26
  ChainedJob.config.around_array_of_job_arguments.call(options) { array_of_job_arguments }
23
27
  end
24
28
 
@@ -29,5 +33,9 @@ module ChainedJob
29
33
  def parallelism
30
34
  raise NoMethodError, 'undefined method parallelism'
31
35
  end
36
+
37
+ def job_arguments_key
38
+ self.class
39
+ end
32
40
  end
33
41
  end
@@ -4,14 +4,16 @@ require 'chained_job/helpers'
4
4
 
5
5
  module ChainedJob
6
6
  class Process
7
- def self.run(job_instance, worker_id, job_tag)
8
- new(job_instance, worker_id, job_tag).run
7
+ def self.run(args, job_instance, job_arguments_key, worker_id, job_tag)
8
+ new(args, job_instance, job_arguments_key, worker_id, job_tag).run
9
9
  end
10
10
 
11
- attr_reader :job_instance, :worker_id, :job_tag
11
+ attr_reader :args, :job_instance, :job_arguments_key, :worker_id, :job_tag
12
12
 
13
- def initialize(job_instance, worker_id, job_tag)
13
+ def initialize(args, job_instance, job_arguments_key, worker_id, job_tag)
14
+ @args = args
14
15
  @job_instance = job_instance
16
+ @job_arguments_key = job_arguments_key
15
17
  @worker_id = worker_id
16
18
  @job_tag = job_tag
17
19
  end
@@ -20,19 +22,28 @@ module ChainedJob
20
22
  with_hooks do
21
23
  return finished_worker unless argument
22
24
 
23
- job_instance.process(argument)
24
- job_instance.class.perform_later(worker_id, job_tag)
25
+ begin
26
+ job_instance.process(argument)
27
+ rescue StandardError => e
28
+ push_job_arguments_back if handle_retry?
29
+ raise e
30
+ end
31
+ job_instance.class.perform_later(args, worker_id, job_tag)
25
32
  end
26
33
  end
27
34
 
28
35
  private
29
36
 
37
+ def handle_retry?
38
+ job_instance.try(:handle_retry?)
39
+ end
40
+
30
41
  def with_hooks
31
42
  ChainedJob.config.around_chain_process.call(options) { yield }
32
43
  end
33
44
 
34
45
  def options
35
- @options ||= { job_class: job_instance.class, worker_id: worker_id }
46
+ @options ||= { job_class: job_instance.class, worker_id: worker_id, args: args }
36
47
  end
37
48
 
38
49
  def finished_worker
@@ -52,10 +63,9 @@ module ChainedJob
52
63
  end
53
64
 
54
65
  def deserialized_argument
66
+ return unless serialized_argument
67
+
55
68
  Marshal.load(serialized_argument)
56
- rescue TypeError, ArgumentError
57
- # backward compatibility
58
- serialized_argument
59
69
  end
60
70
 
61
71
  def serialized_argument
@@ -69,7 +79,11 @@ module ChainedJob
69
79
  end
70
80
 
71
81
  def job_key
72
- Helpers.job_key(job_instance.class)
82
+ Helpers.job_key(job_arguments_key)
83
+ end
84
+
85
+ def push_job_arguments_back
86
+ ChainedJob.redis.rpush(redis_key, Helpers.serialize([argument]))
73
87
  end
74
88
  end
75
89
  end
@@ -6,14 +6,16 @@ require 'chained_job/store_job_arguments'
6
6
 
7
7
  module ChainedJob
8
8
  class StartChains
9
- def self.run(job_class, array_of_job_arguments, parallelism)
10
- new(job_class, array_of_job_arguments, parallelism).run
9
+ def self.run(args, job_class, job_arguments_key, array_of_job_arguments, parallelism)
10
+ new(args, job_class, job_arguments_key, array_of_job_arguments, parallelism).run
11
11
  end
12
12
 
13
- attr_reader :job_class, :array_of_job_arguments, :parallelism
13
+ attr_reader :args, :job_class, :job_arguments_key, :array_of_job_arguments, :parallelism
14
14
 
15
- def initialize(job_class, array_of_job_arguments, parallelism)
15
+ def initialize(args, job_class, job_arguments_key, array_of_job_arguments, parallelism)
16
+ @args = args
16
17
  @job_class = job_class
18
+ @job_arguments_key = job_arguments_key
17
19
  @array_of_job_arguments = array_of_job_arguments
18
20
  @parallelism = parallelism
19
21
  end
@@ -23,15 +25,15 @@ module ChainedJob
23
25
  with_hooks do
24
26
  log_chained_job_cleanup
25
27
 
26
- ChainedJob::CleanUpQueue.run(job_class)
28
+ ChainedJob::CleanUpQueue.run(job_arguments_key)
27
29
 
28
30
  next unless array_of_job_arguments.count.positive?
29
31
 
30
- ChainedJob::StoreJobArguments.run(job_class, job_tag, array_of_job_arguments)
32
+ ChainedJob::StoreJobArguments.run(job_arguments_key, job_tag, array_of_job_arguments)
31
33
 
32
34
  log_chained_job_start
33
35
 
34
- parallelism.times { |worked_id| job_class.perform_later(worked_id, job_tag) }
36
+ parallelism.times { |worked_id| job_class.perform_later(args, worked_id, job_tag) }
35
37
  end
36
38
  end
37
39
  # rubocop:enable Metrics/AbcSize
@@ -47,6 +49,7 @@ module ChainedJob
47
49
  job_class: job_class,
48
50
  array_of_job_arguments: array_of_job_arguments,
49
51
  parallelism: parallelism,
52
+ args: args,
50
53
  }
51
54
  end
52
55
 
@@ -4,14 +4,14 @@ require 'chained_job/helpers'
4
4
 
5
5
  module ChainedJob
6
6
  class StoreJobArguments
7
- def self.run(job_class, job_tag, array_of_job_arguments)
8
- new(job_class, job_tag, array_of_job_arguments).run
7
+ def self.run(job_arguments_key, job_tag, array_of_job_arguments)
8
+ new(job_arguments_key, job_tag, array_of_job_arguments).run
9
9
  end
10
10
 
11
- attr_reader :job_class, :job_tag, :array_of_job_arguments
11
+ attr_reader :job_arguments_key, :job_tag, :array_of_job_arguments
12
12
 
13
- def initialize(job_class, job_tag, array_of_job_arguments)
14
- @job_class = job_class
13
+ def initialize(job_arguments_key, job_tag, array_of_job_arguments)
14
+ @job_arguments_key = job_arguments_key
15
15
  @job_tag = job_tag
16
16
  @array_of_job_arguments = array_of_job_arguments
17
17
  end
@@ -20,7 +20,7 @@ module ChainedJob
20
20
  set_tag_list
21
21
 
22
22
  array_of_job_arguments.each_slice(config.arguments_batch_size) do |sublist|
23
- ChainedJob.redis.rpush(redis_key, serialize(sublist))
23
+ ChainedJob.redis.rpush(redis_key, Helpers.serialize(sublist))
24
24
  end
25
25
 
26
26
  ChainedJob.redis.expire(redis_key, config.arguments_queue_expiration)
@@ -41,11 +41,7 @@ module ChainedJob
41
41
  end
42
42
 
43
43
  def job_key
44
- @job_key ||= Helpers.job_key(job_class)
45
- end
46
-
47
- def serialize(arguments)
48
- arguments.map { |argument| Marshal.dump(argument) }
44
+ @job_key ||= Helpers.job_key(job_arguments_key)
49
45
  end
50
46
 
51
47
  def config
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ChainedJob
4
- VERSION = '0.5.0'
4
+ VERSION = '0.7.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chained_job
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mantas Kūjalis
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-04-22 00:00:00.000000000 Z
12
+ date: 2021-08-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler