chained_job 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/chained_job/clean_up_queue.rb +45 -0
- data/lib/chained_job/config.rb +1 -0
- data/lib/chained_job/helpers.rb +9 -1
- data/lib/chained_job/middleware.rb +6 -2
- data/lib/chained_job/process.rb +12 -7
- data/lib/chained_job/start_chains.rb +10 -22
- data/lib/chained_job/store_job_arguments.rb +51 -0
- data/lib/chained_job/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 21a653ae3a0eaaf596d741a3208b4aee1a7d297700e1d506c7005bf7cd529618
|
4
|
+
data.tar.gz: 03ab75c95d0610a9f192d3f6f7bfc7eeac516f641b4bbc32730ac45d1710f3bc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb6be468a09e6824fb2dd21fd8aa8029790a63c145d9f5048b5c29dea2632e85e6b6ebcfb29558e7504982a4ad48261140e6b96b3b8e4ef55726d58a18ed58c0
|
7
|
+
data.tar.gz: 75a1aefe58b764b69d05514bfa47209ec4ff9d3f743379301841d132e15fc60c34f6254e0fae7dfacbca393c973f4ad2809dffcfb5a7041a42e62d3155a95857
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'chained_job/helpers'
|
4
|
+
|
5
|
+
module ChainedJob
|
6
|
+
class CleanUpQueue
|
7
|
+
def self.run(job_class)
|
8
|
+
new(job_class).run
|
9
|
+
end
|
10
|
+
|
11
|
+
TRIM_STEP_SIZE = 1_000
|
12
|
+
|
13
|
+
attr_reader :job_class
|
14
|
+
|
15
|
+
def initialize(job_class)
|
16
|
+
@job_class = job_class
|
17
|
+
end
|
18
|
+
|
19
|
+
# rubocop:disable Metrics/AbcSize
|
20
|
+
def run
|
21
|
+
loop do
|
22
|
+
tag = ChainedJob.redis.spop(tag_list)
|
23
|
+
|
24
|
+
break unless tag
|
25
|
+
|
26
|
+
redis_key = Helpers.redis_key(job_key, tag)
|
27
|
+
size = ChainedJob.redis.llen(redis_key)
|
28
|
+
(size / TRIM_STEP_SIZE).times { ChainedJob.redis.ltrim(redis_key, 0, -TRIM_STEP_SIZE) }
|
29
|
+
|
30
|
+
ChainedJob.redis.del(redis_key)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
# rubocop:enable Metrics/AbcSize
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def tag_list
|
38
|
+
@tag_list ||= Helpers.tag_list(job_key)
|
39
|
+
end
|
40
|
+
|
41
|
+
def job_key
|
42
|
+
@job_key ||= Helpers.job_key(job_class)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/chained_job/config.rb
CHANGED
data/lib/chained_job/helpers.rb
CHANGED
@@ -4,8 +4,16 @@ module ChainedJob
|
|
4
4
|
module Helpers
|
5
5
|
module_function
|
6
6
|
|
7
|
-
def
|
7
|
+
def job_key(job_class)
|
8
8
|
"chained_job:#{job_class}"
|
9
9
|
end
|
10
|
+
|
11
|
+
def redis_key(job_key, tag)
|
12
|
+
"#{job_key}:#{tag}"
|
13
|
+
end
|
14
|
+
|
15
|
+
def tag_list(prefix)
|
16
|
+
"#{prefix}:tags"
|
17
|
+
end
|
10
18
|
end
|
11
19
|
end
|
@@ -5,9 +5,13 @@ require 'chained_job/process'
|
|
5
5
|
|
6
6
|
module ChainedJob
|
7
7
|
module Middleware
|
8
|
-
def
|
8
|
+
def self.included(base)
|
9
|
+
base.queue_as ChainedJob.config.queue if ChainedJob.config.queue
|
10
|
+
end
|
11
|
+
|
12
|
+
def perform(worker_id = nil, tag = nil)
|
9
13
|
if worker_id
|
10
|
-
ChainedJob::Process.run(self, worker_id)
|
14
|
+
ChainedJob::Process.run(self, worker_id, tag)
|
11
15
|
else
|
12
16
|
ChainedJob::StartChains.run(self.class, array_of_job_arguments, parallelism)
|
13
17
|
end
|
data/lib/chained_job/process.rb
CHANGED
@@ -4,15 +4,16 @@ require 'chained_job/helpers'
|
|
4
4
|
|
5
5
|
module ChainedJob
|
6
6
|
class Process
|
7
|
-
def self.run(job_instance, worker_id)
|
8
|
-
new(job_instance, worker_id).run
|
7
|
+
def self.run(job_instance, worker_id, job_tag)
|
8
|
+
new(job_instance, worker_id, job_tag).run
|
9
9
|
end
|
10
10
|
|
11
|
-
attr_reader :job_instance, :worker_id
|
11
|
+
attr_reader :job_instance, :worker_id, :job_tag
|
12
12
|
|
13
|
-
def initialize(job_instance, worker_id)
|
13
|
+
def initialize(job_instance, worker_id, job_tag)
|
14
14
|
@job_instance = job_instance
|
15
15
|
@worker_id = worker_id
|
16
|
+
@job_tag = job_tag
|
16
17
|
end
|
17
18
|
|
18
19
|
def run
|
@@ -20,7 +21,7 @@ module ChainedJob
|
|
20
21
|
return log_finished_worker unless argument
|
21
22
|
|
22
23
|
job_instance.process(argument)
|
23
|
-
job_instance.class.perform_later(worker_id)
|
24
|
+
job_instance.class.perform_later(worker_id, job_tag)
|
24
25
|
end
|
25
26
|
end
|
26
27
|
|
@@ -36,7 +37,7 @@ module ChainedJob
|
|
36
37
|
|
37
38
|
def log_finished_worker
|
38
39
|
ChainedJob.logger.info(
|
39
|
-
"#{job_instance.class} worker #{worker_id} finished"
|
40
|
+
"#{job_instance.class}:#{job_tag} worker #{worker_id} finished"
|
40
41
|
)
|
41
42
|
end
|
42
43
|
|
@@ -45,7 +46,11 @@ module ChainedJob
|
|
45
46
|
end
|
46
47
|
|
47
48
|
def redis_key
|
48
|
-
Helpers.redis_key(
|
49
|
+
Helpers.redis_key(job_key, job_tag)
|
50
|
+
end
|
51
|
+
|
52
|
+
def job_key
|
53
|
+
Helpers.job_key(job_instance.class)
|
49
54
|
end
|
50
55
|
end
|
51
56
|
end
|
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'chained_job/helpers'
|
4
|
+
require 'chained_job/clean_up_queue'
|
5
|
+
require 'chained_job/store_job_arguments'
|
4
6
|
|
5
7
|
module ChainedJob
|
6
8
|
class StartChains
|
@@ -16,19 +18,21 @@ module ChainedJob
|
|
16
18
|
@parallelism = parallelism
|
17
19
|
end
|
18
20
|
|
21
|
+
# rubocop:disable Metrics/AbcSize
|
19
22
|
def run
|
20
23
|
with_hooks do
|
21
|
-
|
24
|
+
ChainedJob::CleanUpQueue.run(job_class)
|
22
25
|
|
23
26
|
next unless array_of_job_arguments.count.positive?
|
24
27
|
|
25
|
-
|
28
|
+
ChainedJob::StoreJobArguments.run(job_class, job_tag, array_of_job_arguments)
|
26
29
|
|
27
30
|
log_chained_job_start
|
28
31
|
|
29
|
-
parallelism.times { |worked_id| job_class.perform_later(worked_id) }
|
32
|
+
parallelism.times { |worked_id| job_class.perform_later(worked_id, job_tag) }
|
30
33
|
end
|
31
34
|
end
|
35
|
+
# rubocop:enable Metrics/AbcSize
|
32
36
|
|
33
37
|
private
|
34
38
|
|
@@ -44,31 +48,15 @@ module ChainedJob
|
|
44
48
|
}
|
45
49
|
end
|
46
50
|
|
47
|
-
def
|
48
|
-
|
49
|
-
redis.rpush(redis_key, sublist)
|
50
|
-
end
|
51
|
-
|
52
|
-
redis.expire(redis_key, config.arguments_queue_expiration)
|
51
|
+
def job_tag
|
52
|
+
@job_tag ||= Time.now.to_f.to_s
|
53
53
|
end
|
54
54
|
|
55
55
|
def log_chained_job_start
|
56
56
|
ChainedJob.logger.info(
|
57
|
-
"#{job_class} starting #{parallelism} workers "\
|
57
|
+
"#{job_class}:#{job_tag} starting #{parallelism} workers "\
|
58
58
|
"processing #{array_of_job_arguments.count} items"
|
59
59
|
)
|
60
60
|
end
|
61
|
-
|
62
|
-
def redis
|
63
|
-
ChainedJob.redis
|
64
|
-
end
|
65
|
-
|
66
|
-
def redis_key
|
67
|
-
Helpers.redis_key(job_class)
|
68
|
-
end
|
69
|
-
|
70
|
-
def config
|
71
|
-
ChainedJob.config
|
72
|
-
end
|
73
61
|
end
|
74
62
|
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'chained_job/helpers'
|
4
|
+
|
5
|
+
module ChainedJob
|
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
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_reader :job_class, :job_tag, :array_of_job_arguments
|
12
|
+
|
13
|
+
def initialize(job_class, job_tag, array_of_job_arguments)
|
14
|
+
@job_class = job_class
|
15
|
+
@job_tag = job_tag
|
16
|
+
@array_of_job_arguments = array_of_job_arguments
|
17
|
+
end
|
18
|
+
|
19
|
+
def run
|
20
|
+
set_tag_list
|
21
|
+
|
22
|
+
array_of_job_arguments.each_slice(config.arguments_batch_size) do |sublist|
|
23
|
+
ChainedJob.redis.rpush(redis_key, sublist)
|
24
|
+
end
|
25
|
+
|
26
|
+
ChainedJob.redis.expire(redis_key, config.arguments_queue_expiration)
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def set_tag_list
|
32
|
+
ChainedJob.redis.sadd(tag_list, job_tag)
|
33
|
+
end
|
34
|
+
|
35
|
+
def tag_list
|
36
|
+
Helpers.tag_list(job_key)
|
37
|
+
end
|
38
|
+
|
39
|
+
def redis_key
|
40
|
+
@redis_key ||= Helpers.redis_key(job_key, job_tag)
|
41
|
+
end
|
42
|
+
|
43
|
+
def job_key
|
44
|
+
@job_key ||= Helpers.job_key(job_class)
|
45
|
+
end
|
46
|
+
|
47
|
+
def config
|
48
|
+
ChainedJob.config
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/chained_job/version.rb
CHANGED
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
|
+
version: 0.2.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: 2020-
|
12
|
+
date: 2020-07-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -77,11 +77,13 @@ extensions: []
|
|
77
77
|
extra_rdoc_files: []
|
78
78
|
files:
|
79
79
|
- lib/chained_job.rb
|
80
|
+
- lib/chained_job/clean_up_queue.rb
|
80
81
|
- lib/chained_job/config.rb
|
81
82
|
- lib/chained_job/helpers.rb
|
82
83
|
- lib/chained_job/middleware.rb
|
83
84
|
- lib/chained_job/process.rb
|
84
85
|
- lib/chained_job/start_chains.rb
|
86
|
+
- lib/chained_job/store_job_arguments.rb
|
85
87
|
- lib/chained_job/version.rb
|
86
88
|
homepage: https://github.com/vinted/chained_job
|
87
89
|
licenses:
|