chained_job 0.3.0 → 0.6.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: b302104a73850add4edc180a181961885c7c5a5c8bc520fc26b3dfa1a82c6d39
4
- data.tar.gz: 25c67ef1d56381c0704ef70e182937412c120ff995b01ef6036e2fd840f5ab29
3
+ metadata.gz: 4699f818627cf6babf70086a5c5d4ac5016b17bedfb48670fd262f28b2da9350
4
+ data.tar.gz: 3d2f5a59e1fedaa8b11894f819739d254aa75620da658dafbb4435d1796412f1
5
5
  SHA512:
6
- metadata.gz: 46c772e243c573a1cb4dd16b21d2cd34127665e174eab7c90946dd3065941f509b35a05b5c6fb498f634f5b03d4b926ccd3747626f66fbe2591f53423ace47a3
7
- data.tar.gz: fc1881df18aa578066ca959c22e21617dc089b0a7615fc4c340c501bb06a35e52e99f587084b50729e6ab3c62b7aafb6bec7e60de4a2663968423db88b267fc9
6
+ metadata.gz: c4d6af80816570f45cc5b3e02aadb8d53f63fa10c009278401318a0a608d93d9e789038c594dedd4acd7cc54d6fd9459b13169f70d29d1687b1c82af2546e05e
7
+ data.tar.gz: c4f945a8a699eeda1fa4524ba526442efa91dc83c7a5aab1485bcdda5f79c65e94d085bb706791b65262012770dd8ea85f97b6979631576ee30c8dabea8c2257
@@ -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
@@ -13,6 +13,7 @@ module ChainedJob
13
13
  :around_start_chains,
14
14
  :around_chain_process,
15
15
  :around_array_of_job_arguments,
16
+ :after_worker_finished,
16
17
  :debug,
17
18
  :logger,
18
19
  :redis,
@@ -28,6 +29,7 @@ module ChainedJob
28
29
  self.around_start_chains = ->(_options, &block) { block.call }
29
30
  self.around_chain_process = ->(_options, &block) { block.call }
30
31
  self.around_array_of_job_arguments = ->(_options, &block) { block.call }
32
+ self.after_worker_finished = ->(_options) {}
31
33
 
32
34
  self.debug = true
33
35
  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)
@@ -9,11 +9,15 @@ 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, parallelism)
17
21
  end
18
22
  end
19
23
 
@@ -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,24 +4,26 @@ 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
18
20
 
19
21
  def run
20
22
  with_hooks do
21
- return log_finished_worker unless argument
23
+ return finished_worker unless argument
22
24
 
23
25
  job_instance.process(argument)
24
- job_instance.class.perform_later(worker_id, job_tag)
26
+ job_instance.class.perform_later(args, worker_id, job_tag)
25
27
  end
26
28
  end
27
29
 
@@ -32,7 +34,13 @@ module ChainedJob
32
34
  end
33
35
 
34
36
  def options
35
- { job_class: job_instance.class, worker_id: worker_id }
37
+ @options ||= { job_class: job_instance.class, worker_id: worker_id }
38
+ end
39
+
40
+ def finished_worker
41
+ log_finished_worker
42
+
43
+ ChainedJob.config.after_worker_finished&.call(options)
36
44
  end
37
45
 
38
46
  def log_finished_worker
@@ -42,7 +50,19 @@ module ChainedJob
42
50
  end
43
51
 
44
52
  def argument
45
- @argument ||= ChainedJob.redis.lpop(redis_key)
53
+ @argument ||= deserialized_argument
54
+ end
55
+
56
+ def deserialized_argument
57
+ return unless serialized_argument
58
+
59
+ Marshal.load(serialized_argument)
60
+ end
61
+
62
+ def serialized_argument
63
+ return @serialized_argument if defined?(@serialized_argument)
64
+
65
+ @serialized_argument = ChainedJob.redis.lpop(redis_key)
46
66
  end
47
67
 
48
68
  def redis_key
@@ -50,7 +70,7 @@ module ChainedJob
50
70
  end
51
71
 
52
72
  def job_key
53
- Helpers.job_key(job_instance.class)
73
+ Helpers.job_key(job_arguments_key)
54
74
  end
55
75
  end
56
76
  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
@@ -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, sublist)
23
+ ChainedJob.redis.rpush(redis_key, serialize(sublist))
24
24
  end
25
25
 
26
26
  ChainedJob.redis.expire(redis_key, config.arguments_queue_expiration)
@@ -41,7 +41,11 @@ module ChainedJob
41
41
  end
42
42
 
43
43
  def job_key
44
- @job_key ||= Helpers.job_key(job_class)
44
+ @job_key ||= Helpers.job_key(job_arguments_key)
45
+ end
46
+
47
+ def serialize(arguments)
48
+ arguments.map { |argument| Marshal.dump(argument) }
45
49
  end
46
50
 
47
51
  def config
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ChainedJob
4
- VERSION = '0.3.0'
4
+ VERSION = '0.6.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.3.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mantas Kūjalis
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-10-19 00:00:00.000000000 Z
12
+ date: 2021-05-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - "~>"
18
+ - - ">="
19
19
  - !ruby/object:Gem::Version
20
- version: '1.17'
20
+ version: '0'
21
21
  type: :development
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - "~>"
25
+ - - ">="
26
26
  - !ruby/object:Gem::Version
27
- version: '1.17'
27
+ version: '0'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: minitest
30
30
  requirement: !ruby/object:Gem::Requirement
@@ -53,25 +53,10 @@ dependencies:
53
53
  - - "~>"
54
54
  - !ruby/object:Gem::Version
55
55
  version: '12.0'
56
- - !ruby/object:Gem::Dependency
57
- name: rubocop-vinted
58
- requirement: !ruby/object:Gem::Requirement
59
- requirements:
60
- - - "~>"
61
- - !ruby/object:Gem::Version
62
- version: '0.3'
63
- type: :development
64
- prerelease: false
65
- version_requirements: !ruby/object:Gem::Requirement
66
- requirements:
67
- - - "~>"
68
- - !ruby/object:Gem::Version
69
- version: '0.3'
70
56
  description: Chained job allows you to define an array of queued jobs that should
71
57
  be run in sequence after the main job has been executed successfully.
72
58
  email:
73
- - mantas.kujalis@vinted.com
74
- - titas@vinted.com
59
+ - backend@vinted.com
75
60
  executables: []
76
61
  extensions: []
77
62
  extra_rdoc_files: []
@@ -104,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
89
  - !ruby/object:Gem::Version
105
90
  version: '0'
106
91
  requirements: []
107
- rubygems_version: 3.0.3
92
+ rubygems_version: 3.1.4
108
93
  signing_key:
109
94
  specification_version: 4
110
95
  summary: Chained job helper