sidekiq-merger 0.0.1 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/bin/console DELETED
@@ -1,7 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "sidekiq-merger"
5
-
6
- require "pry"
7
- Pry.start
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,100 +0,0 @@
1
- require_relative "redis"
2
- require "active_support/core_ext/hash/indifferent_access"
3
-
4
- class Sidekiq::Merger::Batch
5
- class << self
6
- def all
7
- redis = Sidekiq::Merger::Redis.new
8
-
9
- redis.all.map do |full_batch_key|
10
- keys = full_batch_key.split(":")
11
- raise "Invalid batch key" if keys.size < 3
12
- worker_class = keys[0].camelize.constantize
13
- queue = keys[1]
14
- batch_key = keys[2]
15
- new(worker_class, queue, batch_key, redis: redis)
16
- end
17
- end
18
-
19
- def initialize_with_args(worker_class, queue, args, options = {})
20
- new(worker_class, queue, batch_key(worker_class, args), options)
21
- end
22
-
23
- def batch_key(worker_class, args)
24
- options = get_options(worker_class)
25
- batch_key = options["key"]
26
- if batch_key.respond_to?(:call)
27
- batch_key.call(args)
28
- else
29
- batch_key
30
- end
31
- end
32
-
33
- def get_options(worker_class)
34
- (worker_class.get_sidekiq_options["merger"] || {}).with_indifferent_access
35
- end
36
- end
37
-
38
- attr_reader :worker_class, :queue, :batch_key
39
-
40
- def initialize(worker_class, queue, batch_key, redis: Sidekiq::Merger::Redis.new)
41
- @worker_class = worker_class
42
- @queue = queue
43
- @batch_key = batch_key
44
- @redis = redis
45
- end
46
-
47
- def add(args, execution_time)
48
- @redis.push(full_batch_key, args, execution_time)
49
- end
50
-
51
- def delete(args)
52
- @redis.delete(full_batch_key, args)
53
- end
54
-
55
- def size
56
- @redis.batch_size(full_batch_key)
57
- end
58
-
59
- def flush
60
- msgs = []
61
-
62
- if @redis.lock(full_batch_key, Sidekiq::Merger::Config.lock_ttl)
63
- msgs = @redis.pluck(full_batch_key)
64
- end
65
-
66
- unless msgs.empty?
67
- Sidekiq::Client.push(
68
- "class" => worker_class,
69
- "queue" => queue,
70
- "args" => msgs
71
- )
72
- end
73
- end
74
-
75
- def can_flush?
76
- !execution_time.nil? && execution_time < Time.now
77
- end
78
-
79
- def full_batch_key
80
- @full_batch_key ||= [worker_class.name.to_s.underscore, queue, batch_key].join(":")
81
- end
82
-
83
- def execution_time
84
- @execution_time ||= @redis.execution_time(full_batch_key)
85
- end
86
-
87
- def ==(other)
88
- self.worker_class == other.worker_class &&
89
- self.queue == other.queue &&
90
- self.batch_key == other.batch_key
91
- end
92
-
93
- private
94
-
95
- def options
96
- @options ||= self.class.get_options(worker_class)
97
- rescue NameError
98
- {}
99
- end
100
- end