chained_job 0.4.0 → 0.6.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
  SHA256:
3
- metadata.gz: 2754a3795b49b29119699d834fa7fd30a3618c5c9b9e4e13c0d806f1e681b1c7
4
- data.tar.gz: cd5a4e01156701bb5c232a67d1731bc592b4caafd3bc1c09ec11e23acb10efd3
3
+ metadata.gz: 818800b980eebd353e45811df72d640740f5fc2e5cd994f5d3e1297151e4c0b8
4
+ data.tar.gz: 11262d0b15f247b32db77e76590186aabe332e39f9dabef876e2db015bb3f714
5
5
  SHA512:
6
- metadata.gz: 012b685a5f4b0a457d70125e4d49f1fba4634650ec8b684d34cffafe35b4327807de56670ec6026ec87a773fdb641477452474205629104be846d6757ff9444c
7
- data.tar.gz: 122694c013a12d28b1c781b8b593bcf7e1dd40209642d8c3879a69821792c0258dadaf0f7b3b1dd06990ae802489256699a42abbb8eed2cadbfe2e3efc0099dc
6
+ metadata.gz: 1d3598a379c3e8a19cd16e1004cca085a6736f258173c918d0d39a8bd4b7a0cfd283797fc3412d017dbde0efcb2080595344f01ba358ff421cd5e2a1b6e754e3
7
+ data.tar.gz: d144c5e5f36895ca6d80eb2d8df85a52149036d2b3cffd6829e9ffb37d89c5615f7b074996d6c0237f32eedcdf031baf24fc7788aeba073cb3bf6bc81c6f1a9f
@@ -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)
@@ -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
@@ -21,7 +23,7 @@ module ChainedJob
21
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,7 @@ module ChainedJob
32
34
  end
33
35
 
34
36
  def options
35
- @options ||= { job_class: job_instance.class, worker_id: worker_id }
37
+ @options ||= { job_class: job_instance.class, worker_id: worker_id, args: args }
36
38
  end
37
39
 
38
40
  def finished_worker
@@ -48,7 +50,19 @@ module ChainedJob
48
50
  end
49
51
 
50
52
  def argument
51
- @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)
52
66
  end
53
67
 
54
68
  def redis_key
@@ -56,7 +70,7 @@ module ChainedJob
56
70
  end
57
71
 
58
72
  def job_key
59
- Helpers.job_key(job_instance.class)
73
+ Helpers.job_key(job_arguments_key)
60
74
  end
61
75
  end
62
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
@@ -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, 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.4.0'
4
+ VERSION = '0.6.1'
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.4.0
4
+ version: 0.6.1
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-20 00:00:00.000000000 Z
12
+ date: 2021-06-10 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