delayed_job_bulk 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: adaebdb0b0dbb27b937eebcc5aaa799560968d3f0f8191f46e56272cdb1ed35d
4
+ data.tar.gz: 4653f201d5b26235074291ec4c62181b5ee7015ef1c6bdce0325b985e73a9b8f
5
+ SHA512:
6
+ metadata.gz: bfb43ca8fbcf66900631dbe81fa1859ba3041f27494129262886c090a74c20d1ba37cfb72fe85b6a8a03e54dff260accc910db09d9370ebdb29a11128d7b7b73
7
+ data.tar.gz: 0f7c3ad500d786b5bf3cd89dd5666be5c5f06c07ddeeff5319da42494bb9cbb7af4c031f649d9bce2a020e616e405325b5eb8234eeda84e469471aa1123f8258
@@ -0,0 +1,52 @@
1
+ name: CI
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ test:
7
+ runs-on: ubuntu-20.04
8
+ services:
9
+ postgres:
10
+ image: postgres:9.5
11
+ env:
12
+ POSTGRES_USER: postgres
13
+ POSTGRES_PASSWORD: postgres
14
+ ports:
15
+ - 5432:5432
16
+ options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
17
+ mysql:
18
+ image: mysql:5.7
19
+ env:
20
+ MYSQL_ALLOW_EMPTY_PASSWORD: yes
21
+ ports:
22
+ - 3306:3306
23
+ options: --health-cmd "mysqladmin ping -h localhost" --health-interval 20s --health-timeout 10s --health-retries 10
24
+ strategy:
25
+ fail-fast: false
26
+ matrix:
27
+ ruby: [2.7, '3.0', 3.1, 3.2]
28
+ gemfile: ['rails60', 'rails61', 'rails70']
29
+ database: ['postgresql', 'mysql', 'sqlite']
30
+
31
+ name: ruby ${{ matrix.ruby }}, ${{ matrix.gemfile }}, ${{ matrix.database }}
32
+
33
+ env:
34
+ POSTGRES_USER: postgres
35
+ POSTGRES_PASSWORD: postgres
36
+ DATABASE: ${{ matrix.database }}
37
+ BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
38
+
39
+ steps:
40
+ - uses: actions/checkout@v3
41
+ - uses: ruby/setup-ruby@v1
42
+ with:
43
+ ruby-version: ${{ matrix.ruby }}
44
+ bundler-cache: true
45
+ - name: Prepare test
46
+ run: |
47
+ cd spec/dummy
48
+ BUNDLE_GEMFILE=../../${{ env.BUNDLE_GEMFILE }} RAILS_ENV=test bundle exec rake db:create db:migrate
49
+ cd ../..
50
+ - name: Run test
51
+ run: |
52
+ bundle exec rspec
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /Gemfile.lock
3
+ /gemfiles/*gemfile.lock
4
+ /coverage/
5
+ /pkg/
6
+ /spec/reports/
7
+ /spec/**/db/*.sqlite3
8
+ /spec/**/log/*.log
9
+ /spec/**/tmp/*
10
+ /vendor/
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
4
+ --require rails_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # CHANGELOG
2
+
3
+ ## 1.0.0
4
+
5
+ * First release.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Yoshikazu Kaneta
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,55 @@
1
+ # DelayedJobBulk
2
+
3
+ Bulk insert many jobs at once for delayed_job.
4
+
5
+ ## Dependencies
6
+
7
+ * ruby 2.7+
8
+ * activerecord 6.0+
9
+ * delayed_job 4.1
10
+ * delayed_job_active_record 4.1
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'delayed_job_bulk'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ ## Usage
25
+
26
+ Build `ActiveJob::Base` instances and pass them to `Delayed::Job::Bulk.enqueue`:
27
+
28
+ ```ruby
29
+ class SampleJob < ActiveJob::Base
30
+ end
31
+
32
+ active_jobs = 10.times.map { SampleJob.new('sample') }
33
+ jobs = Delayed::Job::Bulk.enqueue(active_jobs)
34
+ ```
35
+
36
+ Return value of `Delayed::Job::Bulk.enqueue` is an array of Delayed::Job instance:
37
+
38
+ ```ruby
39
+ jobs.size #=> 10
40
+ jobs.each do |job|
41
+ job.id #=> 12345 (id is set only postgresql)
42
+ end
43
+ ```
44
+
45
+ ### Caveats
46
+
47
+ Callbacks related with ActiveJob (`before_enqueue`, `after_enqueue` and `around_enqueue`) are not called for bulk enqueue.
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kanety/delayed_job_bulk.
52
+
53
+ ## License
54
+
55
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'delayed/backend/bulk/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "delayed_job_bulk"
8
+ spec.version = Delayed::Backend::Bulk::VERSION
9
+ spec.authors = ["Yoshikazu Kaneta"]
10
+ spec.email = ["kaneta@sitebridge.co.jp"]
11
+ spec.summary = %q{Bulk insert many jobs at once for delayed_job}
12
+ spec.description = %q{Bulk insert many jobs at once for delayed_job}
13
+ spec.homepage = "https://github.com/kanety/delayed_job_bulk"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = '>= 2.7'
22
+
23
+ spec.add_dependency "delayed_job", ">= 4.1"
24
+ spec.add_dependency "delayed_job_active_record", ">= 4.1"
25
+ spec.add_dependency "activesupport", ">= 6.0"
26
+ spec.add_dependency "activerecord", ">= 6.0"
27
+
28
+ spec.add_development_dependency "rails"
29
+ spec.add_development_dependency "rake"
30
+ spec.add_development_dependency "rspec-rails"
31
+ spec.add_development_dependency "simplecov"
32
+ spec.add_development_dependency "pg"
33
+ spec.add_development_dependency "mysql2"
34
+ spec.add_development_dependency "sqlite3"
35
+ end
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 6.0.0"
4
+ gem "psych", "~> 3.3.0"
5
+
6
+ gemspec path: "../"
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 6.1.0"
4
+
5
+ gemspec path: "../"
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 7.0.1"
4
+
5
+ gemspec path: "../"
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Delayed
4
+ module Backend
5
+ class Bulk
6
+ module Callbacks
7
+ extend ActiveSupport::Concern
8
+ include ActiveSupport::Callbacks
9
+
10
+ included do
11
+ define_callbacks :enqueue
12
+ end
13
+
14
+ class_methods do
15
+ def before_enqueue(*args, &block)
16
+ set_callback(:enqueue, :before, *args, &block)
17
+ end
18
+
19
+ def after_enqueue(*args, &block)
20
+ set_callback(:enqueue, :after, *args, &block)
21
+ end
22
+
23
+ def around_enqueue(*args, &block)
24
+ set_callback(:enqueue, :around, *args, &block)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Delayed
4
+ module Backend
5
+ class Bulk
6
+ VERSION = "1.0.0"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'bulk/callbacks'
4
+
5
+ module Delayed
6
+ module Backend
7
+ class Bulk
8
+ include Callbacks
9
+
10
+ define_callbacks :enqueue
11
+
12
+ def initialize(active_jobs)
13
+ @active_jobs = Array(active_jobs)
14
+ end
15
+
16
+ def call
17
+ @jobs = @active_jobs.map { |active_job| build_job(active_job) }
18
+ @valid_jobs = @jobs.select(&:valid?)
19
+
20
+ if @valid_jobs.present?
21
+ @valid_jobs.each do |job|
22
+ job.send(:set_default_run_at)
23
+ end
24
+ run_callbacks :enqueue do
25
+ insert_all
26
+ end
27
+ end
28
+
29
+ @jobs
30
+ end
31
+
32
+ private
33
+
34
+ def build_job(active_job)
35
+ Delayed::Job.new(
36
+ Delayed::Backend::JobPreparer.new(
37
+ ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper.new(active_job.serialize),
38
+ queue: active_job.queue_name,
39
+ priority: active_job.priority
40
+ ).prepare
41
+ ).tap do |job|
42
+ job.created_at = current_time
43
+ job.updated_at = current_time
44
+ end
45
+ end
46
+
47
+ def current_time
48
+ @current_time ||= Time.zone.now
49
+ end
50
+
51
+ def insert_all
52
+ attrs = @valid_jobs.map { |job| job.attributes.reject { |_,v| v.nil? } }
53
+ result = Delayed::Job.insert_all(attrs)
54
+ assign_id(result.rows) if result.rows.present?
55
+ end
56
+
57
+ def assign_id(ids)
58
+ @valid_jobs.each_with_index do |job, i|
59
+ job.id = ids[i][0]
60
+ job.instance_variable_set('@new_record', false)
61
+ job.instance_variable_set('@previously_new_record', true)
62
+ end
63
+
64
+ @jobs.size.times do |i|
65
+ @active_jobs[i].provider_job_id = @jobs[i].id
66
+ end
67
+ end
68
+
69
+ class << self
70
+ def enqueue(active_jobs)
71
+ new(active_jobs).call
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+
78
+ Delayed::Job::Bulk = Delayed::Backend::Bulk
@@ -0,0 +1,4 @@
1
+ require 'active_support'
2
+ require 'delayed_job_active_record'
3
+
4
+ require_relative 'delayed/backend/bulk'
metadata ADDED
@@ -0,0 +1,213 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delayed_job_bulk
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Yoshikazu Kaneta
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-05-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: delayed_job
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: delayed_job_active_record
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '4.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '4.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '6.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '6.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '6.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '6.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec-rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pg
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: mysql2
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: sqlite3
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description: Bulk insert many jobs at once for delayed_job
168
+ email:
169
+ - kaneta@sitebridge.co.jp
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - ".github/workflows/ci.yml"
175
+ - ".gitignore"
176
+ - ".rspec"
177
+ - CHANGELOG.md
178
+ - Gemfile
179
+ - LICENSE.txt
180
+ - README.md
181
+ - Rakefile
182
+ - delayed_job_bulk.gemspec
183
+ - gemfiles/rails60.gemfile
184
+ - gemfiles/rails61.gemfile
185
+ - gemfiles/rails70.gemfile
186
+ - lib/delayed/backend/bulk.rb
187
+ - lib/delayed/backend/bulk/callbacks.rb
188
+ - lib/delayed/backend/bulk/version.rb
189
+ - lib/delayed_job_bulk.rb
190
+ homepage: https://github.com/kanety/delayed_job_bulk
191
+ licenses:
192
+ - MIT
193
+ metadata: {}
194
+ post_install_message:
195
+ rdoc_options: []
196
+ require_paths:
197
+ - lib
198
+ required_ruby_version: !ruby/object:Gem::Requirement
199
+ requirements:
200
+ - - ">="
201
+ - !ruby/object:Gem::Version
202
+ version: '2.7'
203
+ required_rubygems_version: !ruby/object:Gem::Requirement
204
+ requirements:
205
+ - - ">="
206
+ - !ruby/object:Gem::Version
207
+ version: '0'
208
+ requirements: []
209
+ rubygems_version: 3.3.3
210
+ signing_key:
211
+ specification_version: 4
212
+ summary: Bulk insert many jobs at once for delayed_job
213
+ test_files: []