sidekiq_bulk_job 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +55 -0
- data/LICENSE.txt +21 -0
- data/README.md +57 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/sidekiq_bulk_job.rb +212 -0
- data/lib/sidekiq_bulk_job/batch_runner.rb +47 -0
- data/lib/sidekiq_bulk_job/bulk_job.rb +25 -0
- data/lib/sidekiq_bulk_job/job_retry.rb +44 -0
- data/lib/sidekiq_bulk_job/monitor.rb +12 -0
- data/lib/sidekiq_bulk_job/scheduled_job.rb +26 -0
- data/lib/sidekiq_bulk_job/utils.rb +72 -0
- data/lib/sidekiq_bulk_job/version.rb +3 -0
- data/sidekiq_bulk_job.gemspec +25 -0
- metadata +107 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: abdb3b4dcd5011dee64cbedd57c0756aa2fe76750624bb5f541f0401d0d5a017
|
|
4
|
+
data.tar.gz: 60303116132214138fca45ef42f6a68e9fd8508967f833abd350afe745a6e738
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: f48334d86d62c8bfcd04ab0a48e8c8c676d02333dcbbed4082d63939159c69f383ac05586c4ccbd373c66111df0bfb57c768acddc28529485c7a277da8ba7740
|
|
7
|
+
data.tar.gz: 93b313e2378bd16b47005aafec4b5e47a706715e940b094b9fd779daaa187a04cabfca70771e629ba42a5e9276dc39f3e63628afd674e70d38820da02da46c2f
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
22
|
+
|
|
23
|
+
Examples of unacceptable behavior by participants include:
|
|
24
|
+
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
+
* Public or private harassment
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
39
|
+
|
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
+
threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
54
|
+
|
|
55
|
+
## Enforcement
|
|
56
|
+
|
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
+
reported by contacting the project team at TODO: Write your email address. All
|
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
|
63
|
+
|
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
+
members of the project's leadership.
|
|
67
|
+
|
|
68
|
+
## Attribution
|
|
69
|
+
|
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: https://contributor-covenant.org
|
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
sidekiq_bulk_job (0.1.0)
|
|
5
|
+
sidekiq (~> 5.2.7)
|
|
6
|
+
|
|
7
|
+
GEM
|
|
8
|
+
remote: https://rubygems.org/
|
|
9
|
+
specs:
|
|
10
|
+
coderay (1.1.3)
|
|
11
|
+
connection_pool (2.2.3)
|
|
12
|
+
diff-lcs (1.4.4)
|
|
13
|
+
method_source (1.0.0)
|
|
14
|
+
pry (0.13.1)
|
|
15
|
+
coderay (~> 1.1)
|
|
16
|
+
method_source (~> 1.0)
|
|
17
|
+
rack (2.2.3)
|
|
18
|
+
rack-protection (2.1.0)
|
|
19
|
+
rack
|
|
20
|
+
rake (12.3.3)
|
|
21
|
+
redis (4.1.4)
|
|
22
|
+
rspec (3.10.0)
|
|
23
|
+
rspec-core (~> 3.10.0)
|
|
24
|
+
rspec-expectations (~> 3.10.0)
|
|
25
|
+
rspec-mocks (~> 3.10.0)
|
|
26
|
+
rspec-core (3.10.0)
|
|
27
|
+
rspec-support (~> 3.10.0)
|
|
28
|
+
rspec-expectations (3.10.0)
|
|
29
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
30
|
+
rspec-support (~> 3.10.0)
|
|
31
|
+
rspec-mocks (3.10.0)
|
|
32
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
33
|
+
rspec-support (~> 3.10.0)
|
|
34
|
+
rspec-sidekiq (3.1.0)
|
|
35
|
+
rspec-core (~> 3.0, >= 3.0.0)
|
|
36
|
+
sidekiq (>= 2.4.0)
|
|
37
|
+
rspec-support (3.10.0)
|
|
38
|
+
sidekiq (5.2.9)
|
|
39
|
+
connection_pool (~> 2.2, >= 2.2.2)
|
|
40
|
+
rack (~> 2.0)
|
|
41
|
+
rack-protection (>= 1.5.0)
|
|
42
|
+
redis (>= 3.3.5, < 4.2)
|
|
43
|
+
|
|
44
|
+
PLATFORMS
|
|
45
|
+
ruby
|
|
46
|
+
|
|
47
|
+
DEPENDENCIES
|
|
48
|
+
pry (~> 0.13.1)
|
|
49
|
+
rake (~> 12.0)
|
|
50
|
+
rspec (~> 3.0)
|
|
51
|
+
rspec-sidekiq (~> 3.1.0)
|
|
52
|
+
sidekiq_bulk_job!
|
|
53
|
+
|
|
54
|
+
BUNDLED WITH
|
|
55
|
+
2.1.4
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 TODO: Write your name
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# SidekiqBulkJob
|
|
2
|
+
|
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sidekiq_bulk_job`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
4
|
+
|
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Add this line to your application's Gemfile:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem 'sidekiq_bulk_job'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
And then execute:
|
|
16
|
+
|
|
17
|
+
$ bundle install
|
|
18
|
+
|
|
19
|
+
Or install it yourself as:
|
|
20
|
+
|
|
21
|
+
$ gem install sidekiq_bulk_job
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
###
|
|
26
|
+
```ruby
|
|
27
|
+
process_fail = lambda do |job_class_name, args, exception|
|
|
28
|
+
# do somethine
|
|
29
|
+
# send email
|
|
30
|
+
end
|
|
31
|
+
client = Redis.new
|
|
32
|
+
logger = Logger.new(STDOUT)
|
|
33
|
+
logger.level = Logger::WARN
|
|
34
|
+
SidekiqBulkJob.config redis: client, logger: logger, process_fail: process_fail, queue: :default, batch_size: 3000, prefix: "SidekiqBulkJob"
|
|
35
|
+
|
|
36
|
+
// push a job
|
|
37
|
+
SidekiqBulkJob.perform_async(TestJob, 10)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Development
|
|
41
|
+
|
|
42
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
43
|
+
|
|
44
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
45
|
+
|
|
46
|
+
## Contributing
|
|
47
|
+
|
|
48
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/scalaview/sidekiq_bulk_job. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/scalaview/sidekiq_bulk_job/blob/master/CODE_OF_CONDUCT.md).
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
## License
|
|
52
|
+
|
|
53
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
54
|
+
|
|
55
|
+
## Code of Conduct
|
|
56
|
+
|
|
57
|
+
Everyone interacting in the SidekiqBulkJob project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/scalaview/sidekiq_bulk_job/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "sidekiq_bulk_job"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
require 'securerandom'
|
|
2
|
+
require "sidekiq"
|
|
3
|
+
require 'sidekiq/api'
|
|
4
|
+
|
|
5
|
+
require "sidekiq_bulk_job/version"
|
|
6
|
+
require "sidekiq_bulk_job/bulk_job"
|
|
7
|
+
require "sidekiq_bulk_job/monitor"
|
|
8
|
+
require "sidekiq_bulk_job/scheduled_job"
|
|
9
|
+
require "sidekiq_bulk_job/batch_runner"
|
|
10
|
+
require "sidekiq_bulk_job/utils"
|
|
11
|
+
|
|
12
|
+
module SidekiqBulkJob
|
|
13
|
+
class Error < StandardError; end
|
|
14
|
+
|
|
15
|
+
class Setter
|
|
16
|
+
def initialize(opts)
|
|
17
|
+
@opts = opts
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def set(options)
|
|
21
|
+
@opts.merge!(options)
|
|
22
|
+
self
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def perform_async(job_class, *args)
|
|
26
|
+
options = Utils.symbolize_keys(@opts)
|
|
27
|
+
if options[:at].nil? && options[:in].nil?
|
|
28
|
+
payload = {
|
|
29
|
+
job_class_name: job_class.to_s,
|
|
30
|
+
perfrom_args: args,
|
|
31
|
+
queue: options[:queue] || SidekiqBulkJob.queue
|
|
32
|
+
}.compact
|
|
33
|
+
SidekiqBulkJob.process payload
|
|
34
|
+
else
|
|
35
|
+
perform_in(options[:at] || options[:in], job_class, *args)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# +interval+ must be a timestamp, numeric or something that acts
|
|
40
|
+
# numeric (like an activesupport time interval).
|
|
41
|
+
def perform_in(interval, job_class, *args)
|
|
42
|
+
int = interval.to_f
|
|
43
|
+
now = SidekiqBulkJob.time_now
|
|
44
|
+
ts = (int < 1_000_000_000 ? now + int : int).to_f
|
|
45
|
+
|
|
46
|
+
# Optimization to enqueue something now that is scheduled to go out now or in the past
|
|
47
|
+
if ts > now.to_f
|
|
48
|
+
options = Utils.symbolize_keys(@opts)
|
|
49
|
+
payload = {
|
|
50
|
+
job_class_name: job_class.to_s,
|
|
51
|
+
at: ts,
|
|
52
|
+
perfrom_args: args,
|
|
53
|
+
queue: options[:queue] || SidekiqBulkJob.queue
|
|
54
|
+
}.compact
|
|
55
|
+
SidekiqBulkJob.process payload
|
|
56
|
+
else
|
|
57
|
+
perform_async(job_class, *args)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
alias_method :perform_at, :perform_in
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class << self
|
|
66
|
+
|
|
67
|
+
attr_accessor :prefix, :redis, :queue, :batch_size, :logger, :process_fail
|
|
68
|
+
|
|
69
|
+
def config(redis: , logger: , process_fail: , queue: :default, batch_size: 3000, prefix: "SidekiqBulkJob")
|
|
70
|
+
if redis.nil?
|
|
71
|
+
raise ArgumentError.new("redis not allow nil")
|
|
72
|
+
end
|
|
73
|
+
self.redis = redis
|
|
74
|
+
self.queue = queue
|
|
75
|
+
self.batch_size = batch_size
|
|
76
|
+
self.prefix = prefix
|
|
77
|
+
self.logger = logger
|
|
78
|
+
self.process_fail = process_fail
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def set(options)
|
|
82
|
+
SidekiqBulkJob::Setter.new(options)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
#
|
|
86
|
+
# 无法定义具体执行时间,相当于perform_async的批量执行
|
|
87
|
+
# example:
|
|
88
|
+
# SidekiqBulkJob.perform_async(SomeWorkerClass, *args)
|
|
89
|
+
def perform_async(job_class, *perfrom_args)
|
|
90
|
+
process(job_class_name: job_class.to_s, perfrom_args: perfrom_args)
|
|
91
|
+
nil
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# 延迟一段时间执行
|
|
95
|
+
# example:
|
|
96
|
+
# SidekiqBulkJob.perform_at(Date.parse("2020-12-01"), SomeWorkerClass, *args)
|
|
97
|
+
def perform_in(at, job_class, *perfrom_args)
|
|
98
|
+
int = at.to_f
|
|
99
|
+
now = time_now
|
|
100
|
+
ts = (int < 1_000_000_000 ? now + int : int).to_f
|
|
101
|
+
|
|
102
|
+
# Optimization to enqueue something now that is scheduled to go out now or in the past
|
|
103
|
+
if ts <= now.to_f
|
|
104
|
+
process(job_class_name: job_class.to_s, perfrom_args: perfrom_args)
|
|
105
|
+
else
|
|
106
|
+
process(at: ts, job_class_name: job_class.to_s, perfrom_args: perfrom_args)
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
alias_method :perform_at, :perform_in
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def process(job_class_name: , at: nil, perfrom_args: [], queue: self.queue)
|
|
114
|
+
if at.nil?
|
|
115
|
+
key = generate_key(job_class_name)
|
|
116
|
+
client.lpush key, perfrom_args.to_json
|
|
117
|
+
bulk_run(job_class_name, key, queue: queue) if need_flush?(key)
|
|
118
|
+
monitor(job_class_name, queue: queue)
|
|
119
|
+
else
|
|
120
|
+
scheduled_set = Sidekiq::ScheduledSet.new
|
|
121
|
+
args_redis_key = nil
|
|
122
|
+
target = scheduled_set.find do |job|
|
|
123
|
+
if job.klass == SidekiqBulkJob::ScheduledJob.to_s &&
|
|
124
|
+
job.at.to_i.between?((at - 5).to_i, (at + 30).to_i) # 允许30秒延迟
|
|
125
|
+
_job_class_name, args_redis_key = job.args
|
|
126
|
+
_job_class_name == job_class_name
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
if !target.nil? && !args_redis_key.nil? && !args_redis_key.empty?
|
|
130
|
+
# 往现有的job参数set里增加参数
|
|
131
|
+
client.lpush args_redis_key, perfrom_args.to_json
|
|
132
|
+
else
|
|
133
|
+
# 新增加一个
|
|
134
|
+
args_redis_key = SecureRandom.hex
|
|
135
|
+
client.lpush args_redis_key, perfrom_args.to_json
|
|
136
|
+
SidekiqBulkJob::ScheduledJob.client_push("queue" => queue, "class" => SidekiqBulkJob::ScheduledJob, "at" => at, "args" => [job_class_name, args_redis_key])
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def generate_key(job_class_name)
|
|
142
|
+
"#{prefix}:#{job_class_name}"
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def client
|
|
146
|
+
if redis.nil?
|
|
147
|
+
raise ArgumentError.new("Please initialize redis first!")
|
|
148
|
+
end
|
|
149
|
+
redis
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def time_now
|
|
153
|
+
Time.now
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def need_flush?(key)
|
|
157
|
+
count = client.llen key
|
|
158
|
+
return true if count >= batch_size
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def flush(key)
|
|
162
|
+
result = []
|
|
163
|
+
begin
|
|
164
|
+
_result, success = client.multi do |multi|
|
|
165
|
+
multi.lrange(key, 0, batch_size)
|
|
166
|
+
multi.ltrim(key, batch_size+1, -1)
|
|
167
|
+
end
|
|
168
|
+
result += _result
|
|
169
|
+
count = client.llen key
|
|
170
|
+
end while count > 0
|
|
171
|
+
clear(key)
|
|
172
|
+
result.reverse
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def clear(key)
|
|
176
|
+
script = %Q{
|
|
177
|
+
local size = redis.call('llen', KEYS[1])
|
|
178
|
+
if size == 0 then redis.call('del', KEYS[1]) end
|
|
179
|
+
}
|
|
180
|
+
client.eval script, [key]
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def bulk_run(job_class_name, key, queue: self.queue, async: true)
|
|
184
|
+
args_array = flush(key)
|
|
185
|
+
return if args_array.nil? || args_array.empty?
|
|
186
|
+
async ? SidekiqBulkJob::BulkJob.client_push("queue" => queue, "class" => SidekiqBulkJob::BulkJob, "args" => [job_class_name, args_array]) : SidekiqBulkJob::BulkJob.new.perform(job_class_name, args_array)
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def monitor(job_class_name, queue: self.queue)
|
|
190
|
+
scheduled_set = Sidekiq::ScheduledSet.new
|
|
191
|
+
_monitor = scheduled_set.find do |job|
|
|
192
|
+
if job.klass == SidekiqBulkJob::Monitor.to_s
|
|
193
|
+
timestamp, _job_class_name = job.args
|
|
194
|
+
_job_class_name == job_class_name
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
if !_monitor.nil?
|
|
198
|
+
# TODO debug log
|
|
199
|
+
else
|
|
200
|
+
SidekiqBulkJob::Monitor.client_push("queue" => queue, "at" => (time_now + 60).to_f, "class" => SidekiqBulkJob::Monitor, "args" => [time_now.to_f, job_class_name])
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def fail_callback(job_class_name: , args:, exception: )
|
|
205
|
+
process_fail.call(job_class_name, args, exception)
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
require "sidekiq"
|
|
2
|
+
|
|
3
|
+
module SidekiqBulkJob
|
|
4
|
+
|
|
5
|
+
module BatchRunner
|
|
6
|
+
|
|
7
|
+
module ClassMethods
|
|
8
|
+
# 当使用sidekiq work的时候可以直接使用下面的方法,会把同类型的job汇总在1分钟内批量执行
|
|
9
|
+
# 适合回调时候要执行的job,减少sidekiq job创建的数量,减少线程调度花费的时间
|
|
10
|
+
# 缺点:job批量执行之后时间会比较久,但是比全部job分开执行时间要短
|
|
11
|
+
|
|
12
|
+
# 批量异步执行
|
|
13
|
+
def batch_perform_async(*args)
|
|
14
|
+
SidekiqBulkJob.set(sidekiq_options).perform_async(self, *args)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# 批量延后一段时间执行
|
|
18
|
+
def batch_perform_in(interval, *args)
|
|
19
|
+
SidekiqBulkJob.set(sidekiq_options).perform_in(interval, self, *args)
|
|
20
|
+
end
|
|
21
|
+
alias_method :batch_perform_at, :batch_perform_in
|
|
22
|
+
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
module Setter
|
|
27
|
+
def batch_perform_async(*args)
|
|
28
|
+
SidekiqBulkJob.set(@opts).perform_async(@klass, *args)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# 批量确定时间异步执行
|
|
32
|
+
def batch_perform_in(interval, *args)
|
|
33
|
+
SidekiqBulkJob.set(@opts).perform_at(interval, @klass, *args)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
alias_method :batch_perform_at, :batch_perform_in
|
|
37
|
+
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
Sidekiq::Worker::ClassMethods.module_eval { include SidekiqBulkJob::BatchRunner::ClassMethods }
|
|
46
|
+
|
|
47
|
+
Sidekiq::Worker::Setter.class_eval { include SidekiqBulkJob::BatchRunner::Setter }
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
require "sidekiq"
|
|
2
|
+
|
|
3
|
+
require "sidekiq_bulk_job/job_retry"
|
|
4
|
+
require "sidekiq_bulk_job/utils"
|
|
5
|
+
|
|
6
|
+
module SidekiqBulkJob
|
|
7
|
+
class BulkJob
|
|
8
|
+
include Sidekiq::Worker
|
|
9
|
+
sidekiq_options queue: :default, retry: false
|
|
10
|
+
|
|
11
|
+
def perform(job_class_name, args_array)
|
|
12
|
+
job = Utils.constantize(job_class_name)
|
|
13
|
+
args_array.each do |_args|
|
|
14
|
+
begin
|
|
15
|
+
args = JSON.parse _args
|
|
16
|
+
job.new.send(:perform, *args)
|
|
17
|
+
rescue Exception => e
|
|
18
|
+
SidekiqBulkJob.logger.error("#{job_class_name} Args: #{args}, Error: #{e.full_message}")
|
|
19
|
+
SidekiqBulkJob.fail_callback(job_class_name: job_class_name, args: args, exception: e)
|
|
20
|
+
SidekiqBulkJob::JobRetry.new(job, args, e).push
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require "sidekiq"
|
|
2
|
+
|
|
3
|
+
require "sidekiq_bulk_job/utils"
|
|
4
|
+
require 'sidekiq/job_retry'
|
|
5
|
+
|
|
6
|
+
module SidekiqBulkJob
|
|
7
|
+
class JobRetry
|
|
8
|
+
|
|
9
|
+
def initialize(klass, args, exception, options={})
|
|
10
|
+
@handler = Sidekiq::JobRetry.new(options)
|
|
11
|
+
@klass = klass
|
|
12
|
+
@args = args
|
|
13
|
+
@exception = exception
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def push(options={})
|
|
17
|
+
opts = default_setting.merge(options)
|
|
18
|
+
queue_as = queue(@klass) || :default
|
|
19
|
+
begin
|
|
20
|
+
@handler.local(SidekiqBulkJob::BulkJob, opts, queue_as) do
|
|
21
|
+
raise @exception
|
|
22
|
+
end
|
|
23
|
+
rescue Exception => e
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
protected
|
|
28
|
+
|
|
29
|
+
def default_setting
|
|
30
|
+
# 0 retry: no retry and dead queue
|
|
31
|
+
{ 'class' => @klass.to_s, 'args' => @args, 'retry' => 0 }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def queue(woker)
|
|
35
|
+
if !woker.sidekiq_options.nil? && !woker.sidekiq_options.empty?
|
|
36
|
+
sidekiq_options = Utils.symbolize_keys(woker.sidekiq_options)
|
|
37
|
+
if !sidekiq_options[:queue].nil?
|
|
38
|
+
sidekiq_options[:queue]
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
require "sidekiq"
|
|
2
|
+
|
|
3
|
+
module SidekiqBulkJob
|
|
4
|
+
class Monitor
|
|
5
|
+
include Sidekiq::Worker
|
|
6
|
+
sidekiq_options queue: :default, retry: false
|
|
7
|
+
|
|
8
|
+
def perform(timestamp, job_class_name)
|
|
9
|
+
SidekiqBulkJob.bulk_run(job_class_name, SidekiqBulkJob.generate_key(job_class_name), async: false)
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
require "sidekiq"
|
|
2
|
+
|
|
3
|
+
require "sidekiq_bulk_job/job_retry"
|
|
4
|
+
require "sidekiq_bulk_job/utils"
|
|
5
|
+
|
|
6
|
+
module SidekiqBulkJob
|
|
7
|
+
class ScheduledJob
|
|
8
|
+
include Sidekiq::Worker
|
|
9
|
+
sidekiq_options queue: :default, retry: false
|
|
10
|
+
|
|
11
|
+
def perform(job_class_name, args_redis_key)
|
|
12
|
+
job = Utils.constantize(job_class_name)
|
|
13
|
+
args_array = SidekiqBulkJob.flush args_redis_key
|
|
14
|
+
args_array.each do |_args|
|
|
15
|
+
begin
|
|
16
|
+
args = JSON.parse _args
|
|
17
|
+
job.new.send(:perform, *args)
|
|
18
|
+
rescue Exception => e
|
|
19
|
+
SidekiqBulkJob.logger.error("#{job_class_name} Args: #{args}, Error: #{e.full_message}")
|
|
20
|
+
SidekiqBulkJob.fail_callback(job_class_name: job_class_name, args: args, exception: e)
|
|
21
|
+
SidekiqBulkJob::JobRetry.new(job, args, e).push
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
module Utils
|
|
2
|
+
|
|
3
|
+
class << self
|
|
4
|
+
|
|
5
|
+
def symbolize_keys(obj)
|
|
6
|
+
case obj
|
|
7
|
+
when Array
|
|
8
|
+
obj.inject([]){|res, val|
|
|
9
|
+
res << case val
|
|
10
|
+
when Hash, Array
|
|
11
|
+
symbolize_keys(val)
|
|
12
|
+
else
|
|
13
|
+
val
|
|
14
|
+
end
|
|
15
|
+
res
|
|
16
|
+
}
|
|
17
|
+
when Hash
|
|
18
|
+
obj.inject({}){|res, (key, val)|
|
|
19
|
+
nkey = case key
|
|
20
|
+
when String
|
|
21
|
+
key.to_sym
|
|
22
|
+
else
|
|
23
|
+
key
|
|
24
|
+
end
|
|
25
|
+
nval = case val
|
|
26
|
+
when Hash, Array
|
|
27
|
+
symbolize_keys(val)
|
|
28
|
+
else
|
|
29
|
+
val
|
|
30
|
+
end
|
|
31
|
+
res[nkey] = nval
|
|
32
|
+
res
|
|
33
|
+
}
|
|
34
|
+
else
|
|
35
|
+
obj
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def constantize(camel_cased_word)
|
|
40
|
+
names = camel_cased_word.split("::")
|
|
41
|
+
|
|
42
|
+
# Trigger a built-in NameError exception including the ill-formed constant in the message.
|
|
43
|
+
Object.const_get(camel_cased_word) if names.empty?
|
|
44
|
+
|
|
45
|
+
# Remove the first blank element in case of '::ClassName' notation.
|
|
46
|
+
names.shift if names.size > 1 && names.first.empty?
|
|
47
|
+
|
|
48
|
+
names.inject(Object) do |constant, name|
|
|
49
|
+
if constant == Object
|
|
50
|
+
constant.const_get(name)
|
|
51
|
+
else
|
|
52
|
+
candidate = constant.const_get(name)
|
|
53
|
+
next candidate if constant.const_defined?(name, false)
|
|
54
|
+
next candidate unless Object.const_defined?(name)
|
|
55
|
+
|
|
56
|
+
# Go down the ancestors to check if it is owned directly. The check
|
|
57
|
+
# stops when we reach Object or the end of ancestors tree.
|
|
58
|
+
constant = constant.ancestors.inject(constant) do |const, ancestor|
|
|
59
|
+
break const if ancestor == Object
|
|
60
|
+
break ancestor if ancestor.const_defined?(name, false)
|
|
61
|
+
const
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# owner is in Object, so raise
|
|
65
|
+
constant.const_get(name, false)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
require_relative 'lib/sidekiq_bulk_job/version'
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |spec|
|
|
4
|
+
spec.name = "sidekiq_bulk_job"
|
|
5
|
+
spec.version = SidekiqBulkJob::VERSION
|
|
6
|
+
spec.authors = ["scalaview"]
|
|
7
|
+
spec.email = ["chailink100@gmail.com"]
|
|
8
|
+
spec.summary = %q{ Collect same jobs to single worker, reduce job number and improve thread utilization. }
|
|
9
|
+
spec.description = %q{Collect same jobs to single worker, reduce job number and improve thread utilization.}
|
|
10
|
+
spec.homepage = "https://github.com/scalaview/sidekiq_bulk_job"
|
|
11
|
+
spec.license = "MIT"
|
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
|
13
|
+
|
|
14
|
+
# Specify which files should be added to the gem when it is released.
|
|
15
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
16
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
17
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
18
|
+
end
|
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
20
|
+
spec.require_paths = ["lib"]
|
|
21
|
+
|
|
22
|
+
spec.add_dependency("sidekiq", "~> 5.2.7")
|
|
23
|
+
spec.add_development_dependency("rspec-sidekiq", "~> 3.1.0")
|
|
24
|
+
spec.add_development_dependency('pry', '~> 0.13.1')
|
|
25
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sidekiq_bulk_job
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- scalaview
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2020-11-09 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: sidekiq
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 5.2.7
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 5.2.7
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rspec-sidekiq
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: 3.1.0
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: 3.1.0
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: pry
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: 0.13.1
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: 0.13.1
|
|
55
|
+
description: Collect same jobs to single worker, reduce job number and improve thread
|
|
56
|
+
utilization.
|
|
57
|
+
email:
|
|
58
|
+
- chailink100@gmail.com
|
|
59
|
+
executables: []
|
|
60
|
+
extensions: []
|
|
61
|
+
extra_rdoc_files: []
|
|
62
|
+
files:
|
|
63
|
+
- ".gitignore"
|
|
64
|
+
- ".rspec"
|
|
65
|
+
- ".travis.yml"
|
|
66
|
+
- CODE_OF_CONDUCT.md
|
|
67
|
+
- Gemfile
|
|
68
|
+
- Gemfile.lock
|
|
69
|
+
- LICENSE.txt
|
|
70
|
+
- README.md
|
|
71
|
+
- Rakefile
|
|
72
|
+
- bin/console
|
|
73
|
+
- bin/setup
|
|
74
|
+
- lib/sidekiq_bulk_job.rb
|
|
75
|
+
- lib/sidekiq_bulk_job/batch_runner.rb
|
|
76
|
+
- lib/sidekiq_bulk_job/bulk_job.rb
|
|
77
|
+
- lib/sidekiq_bulk_job/job_retry.rb
|
|
78
|
+
- lib/sidekiq_bulk_job/monitor.rb
|
|
79
|
+
- lib/sidekiq_bulk_job/scheduled_job.rb
|
|
80
|
+
- lib/sidekiq_bulk_job/utils.rb
|
|
81
|
+
- lib/sidekiq_bulk_job/version.rb
|
|
82
|
+
- sidekiq_bulk_job.gemspec
|
|
83
|
+
homepage: https://github.com/scalaview/sidekiq_bulk_job
|
|
84
|
+
licenses:
|
|
85
|
+
- MIT
|
|
86
|
+
metadata: {}
|
|
87
|
+
post_install_message:
|
|
88
|
+
rdoc_options: []
|
|
89
|
+
require_paths:
|
|
90
|
+
- lib
|
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - ">="
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: 2.3.0
|
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
|
+
requirements:
|
|
98
|
+
- - ">="
|
|
99
|
+
- !ruby/object:Gem::Version
|
|
100
|
+
version: '0'
|
|
101
|
+
requirements: []
|
|
102
|
+
rubygems_version: 3.0.3
|
|
103
|
+
signing_key:
|
|
104
|
+
specification_version: 4
|
|
105
|
+
summary: Collect same jobs to single worker, reduce job number and improve thread
|
|
106
|
+
utilization.
|
|
107
|
+
test_files: []
|