activerecord-simple-batch-job 0.1.2
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/.rspec +2 -0
- data/.standard.yml +3 -0
- data/CHANGELOG.md +24 -0
- data/README.md +200 -0
- data/Rakefile +12 -0
- data/db/migrate/20250820072125_create_batch_jobs.rb +28 -0
- data/lib/activerecord-simple-batch-job.rb +1 -0
- data/lib/activerecord-simple_batch_job.rb +3 -0
- data/lib/simple_batch_job/active_record/job_base.rb +90 -0
- data/lib/simple_batch_job/active_record/job_error.rb +8 -0
- data/lib/simple_batch_job/active_record/job_event.rb +12 -0
- data/lib/simple_batch_job/active_record/job_execution.rb +101 -0
- data/lib/simple_batch_job/errors.rb +13 -0
- data/lib/simple_batch_job/mutex_file_job_processor.rb +107 -0
- data/lib/simple_batch_job/operation_base.rb +83 -0
- data/lib/simple_batch_job/organizer.rb +90 -0
- data/lib/simple_batch_job/tasks.rb +64 -0
- data/lib/simple_batch_job/version.rb +5 -0
- data/lib/simple_batch_job.rb +8 -0
- data/sig/simple_batch_job/active_record/job_base.rbs +47 -0
- data/sig/simple_batch_job/active_record/job_error.rbs +5 -0
- data/sig/simple_batch_job/active_record/job_event.rbs +5 -0
- data/sig/simple_batch_job/active_record/job_execution.rbs +58 -0
- data/sig/simple_batch_job/errors.rbs +19 -0
- data/sig/simple_batch_job/mutex_file_job_processor.rbs +58 -0
- data/sig/simple_batch_job/operation_base.rbs +50 -0
- data/sig/simple_batch_job/organizer.rbs +56 -0
- data/sig/simple_batch_job.rbs +33 -0
- metadata +103 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 7785896db0155233f0834dda0d40896be52fc743715d2f30b5f6567c1e284ead
|
|
4
|
+
data.tar.gz: 442e85c667090351642dbc87b7a6d755938afa81bc5bc7042c0fbbae354baba4
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 77931d27e4484c7dcd8ec68479a51e5b69fb833849ee9bd60868fd94a60d4f4490a56a0d8d71f71a7b6ac828ac98ab6401c8b27312efbed0d9e8bd82cb5aa230
|
|
7
|
+
data.tar.gz: 2583bc4c34a6df58eb8df9fab02e016de1d45cb51bbb573d7d2b7877fa2d542687659ebd1716378f622e89d5f889169dca6eb6b9095b920b29ddb458cc732f19
|
data/.rspec
ADDED
data/.standard.yml
ADDED
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
## [0.1.2] - 2026-11-30
|
|
2
|
+
|
|
3
|
+
### Fixed
|
|
4
|
+
|
|
5
|
+
- `JobBase#incompleted?` and `JobBase#last_got_event` now look at the job's own latest execution instead of the latest event across all jobs, so reentrant runs no longer resume from another job's progress
|
|
6
|
+
- `JobExecution#completed?` checks for a `completed` event within the execution
|
|
7
|
+
- completion checks no longer build `IN (... LIMIT 1)` subqueries, which MySQL / MariaDB reject
|
|
8
|
+
- `JobExecution#current_event` breaks `created_at` ties by event id. Events with identical `created_at` values previously came back in an undefined order, so an earlier event could be taken as the current one; this was most likely on MySQL, where `DATETIME` columns default to second precision
|
|
9
|
+
|
|
10
|
+
### Removed
|
|
11
|
+
|
|
12
|
+
- `JobEvent.latest`, `JobEvent.completed`, `JobEvent.not_completed`, `JobExecution.not_completed` and `JobBase.not_completed` scopes
|
|
13
|
+
|
|
14
|
+
## [0.1.1]
|
|
15
|
+
|
|
16
|
+
- fix(simple-batch-job): make SimpleBatchJob#type to unique
|
|
17
|
+
|
|
18
|
+
## [0.1.0] - 2025-09-09
|
|
19
|
+
|
|
20
|
+
- release
|
|
21
|
+
|
|
22
|
+
## [0.0.0] - 2025-08-19
|
|
23
|
+
|
|
24
|
+
- concept
|
data/README.md
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
|
|
2
|
+
# SimpleBatchJob
|
|
3
|
+
|
|
4
|
+
A library that uses ActiveRecord and dry-operation to control batch jobs, record their execution as events, and enable resumption if necessary.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
### Gemfile
|
|
9
|
+
|
|
10
|
+
Add the following to your Gemfile:
|
|
11
|
+
|
|
12
|
+
```ruby
|
|
13
|
+
gem "activerecord-simple-batch-job",
|
|
14
|
+
git: "https://github.com/colorfulcompany/activerecord-collection",
|
|
15
|
+
glob: "simple-batch-job/*.gemspec"
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Then run `bundle install`.
|
|
19
|
+
|
|
20
|
+
### Migration
|
|
21
|
+
|
|
22
|
+
1. Add the following to your Rakefile:
|
|
23
|
+
|
|
24
|
+
```ruby
|
|
25
|
+
require "simple_batch_job/tasks"
|
|
26
|
+
SimpleBatchJob::Tasks.new(verbose: true) # Enable verbose if needed
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
2. Run `bundle exec rake simple_batch_job:install`
|
|
30
|
+
3. Run ActiveRecord migrate using the generated migration file
|
|
31
|
+
|
|
32
|
+
#### Database Schema
|
|
33
|
+
|
|
34
|
+
The library uses four tables:
|
|
35
|
+
|
|
36
|
+
- `simple_batch_jobs`: Stores batch job types
|
|
37
|
+
- `simple_batch_job_executions`: Stores execution records
|
|
38
|
+
- `simple_batch_job_events`: Stores events for each step
|
|
39
|
+
- `simple_batch_job_errors`: Stores detailed error info on failure
|
|
40
|
+
|
|
41
|
+
## Features
|
|
42
|
+
|
|
43
|
+
1. **Store batch job types and execution records in DB**
|
|
44
|
+
- Define job types, record execution history and event history
|
|
45
|
+
- Enables tracking and debugging of execution status
|
|
46
|
+
- Detailed error recording on failure
|
|
47
|
+
|
|
48
|
+
2. **Describe actual processing with Operation**
|
|
49
|
+
- Define batch processing based on Dry::Operation
|
|
50
|
+
- Manage progress step by step
|
|
51
|
+
|
|
52
|
+
3. **Concurrent execution control with MutexFileJobProcessor**
|
|
53
|
+
- Exclusive control using file locks
|
|
54
|
+
- Prevents duplicate access to the same resource
|
|
55
|
+
|
|
56
|
+
## Basic Usage
|
|
57
|
+
|
|
58
|
+
### 1. Define a Batch Job ActiveRecord Class
|
|
59
|
+
|
|
60
|
+
```ruby
|
|
61
|
+
class SampleBatch < SimpleBatchJob::JobBase
|
|
62
|
+
# Define the sequence of steps to execute
|
|
63
|
+
self.sequence = [:received_webhook, :processed_data]
|
|
64
|
+
|
|
65
|
+
# Optional: Specify guard file name (default is underscored class name)
|
|
66
|
+
self.guardfile = "sample_batch_guard"
|
|
67
|
+
end
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### 2. Define an Operation Class
|
|
71
|
+
|
|
72
|
+
Describe process unit as event with proceed! block.
|
|
73
|
+
|
|
74
|
+
```ruby
|
|
75
|
+
class SampleOperation < SimpleBatchJob::OperationBase
|
|
76
|
+
def call(data:)
|
|
77
|
+
proceed! do # :receivr_webhook
|
|
78
|
+
step process_webhook(data)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
proceed! do # :processed_data
|
|
82
|
+
step process_data(data)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
..
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### 3. Execute a Batch Job
|
|
91
|
+
|
|
92
|
+
```ruby
|
|
93
|
+
# Basic execution
|
|
94
|
+
result = SampleOperation.execute(
|
|
95
|
+
job_class: SampleBatch,
|
|
96
|
+
data: { key: "value" }
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
if result.success?
|
|
100
|
+
execution = result.value!
|
|
101
|
+
puts "Job completed: #{execution.completed?}"
|
|
102
|
+
else
|
|
103
|
+
puts "Job failed"
|
|
104
|
+
end
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Advanced Features
|
|
108
|
+
|
|
109
|
+
### Reentrant Execution
|
|
110
|
+
|
|
111
|
+
You can resume a failed job from where it left off:
|
|
112
|
+
|
|
113
|
+
```ruby
|
|
114
|
+
SampleOperation.execute(job_class: SampleBatch, data: data, reentrant: true)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
On subsequent executions, interpret the previously successful event and skip the proceed! block. Success is recorded in the event with a skip message.
|
|
118
|
+
|
|
119
|
+
However, to actually make a job resumable, you need to properly save its intermediate state and structure the operation so it can resume from that saved state.
|
|
120
|
+
|
|
121
|
+
### Check Execution Status
|
|
122
|
+
|
|
123
|
+
```ruby
|
|
124
|
+
# Check if job is running
|
|
125
|
+
if SampleOperation.running?(job_class: SampleBatch)
|
|
126
|
+
puts "Job is currently running"
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# Check execution history
|
|
130
|
+
job = SampleBatch.create_or_find(type: SampleBatch.to_s)
|
|
131
|
+
job.executions.each do |execution|
|
|
132
|
+
puts "Current event: #{execution.current_event}"
|
|
133
|
+
puts "Completed: #{execution.completed?}"
|
|
134
|
+
|
|
135
|
+
# Check failure info
|
|
136
|
+
if execution.error
|
|
137
|
+
puts "Failed at process: #{execution.error.process}"
|
|
138
|
+
puts "Error content: #{execution.error.content}"
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Get Failure Information
|
|
144
|
+
|
|
145
|
+
Errors and failures during batch job execution are recorded in detail:
|
|
146
|
+
|
|
147
|
+
```ruby
|
|
148
|
+
# Example of a failing Operation
|
|
149
|
+
class FailingOperation < SimpleBatchJob::OperationBase
|
|
150
|
+
def call(data:)
|
|
151
|
+
proceed! do
|
|
152
|
+
step validate_data(data)
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
proceed! do
|
|
156
|
+
# Intentionally raise an error
|
|
157
|
+
raise StandardError.new("Processing failed")
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
private
|
|
162
|
+
|
|
163
|
+
def validate_data(data)
|
|
164
|
+
return Failure("Invalid data") unless data&.present?
|
|
165
|
+
Success(data)
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# Execute and check failure info
|
|
170
|
+
result = FailingOperation.execute(job: SampleBatch, data: nil)
|
|
171
|
+
|
|
172
|
+
if result.failure?
|
|
173
|
+
execution = result.failure
|
|
174
|
+
|
|
175
|
+
# Check execution result
|
|
176
|
+
puts "Execution failed: #{execution.result.failure}"
|
|
177
|
+
|
|
178
|
+
# Check error details
|
|
179
|
+
if execution.error
|
|
180
|
+
puts "Failed process: #{execution.error.process}"
|
|
181
|
+
puts "Error content: #{execution.error.content}"
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# Check event history
|
|
185
|
+
execution.events.order(:created_at).each do |event|
|
|
186
|
+
puts "Event: #{event.name}, Message: #{event.message}"
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### Error Handling
|
|
192
|
+
|
|
193
|
+
This library does not raise any exceptions in production environments. You should use PubSub to connect to the monitoring system.
|
|
194
|
+
|
|
195
|
+
```ruby
|
|
196
|
+
ActiveSupport::Notifications.subscribe("simple_batch_job.exception") do |ex|
|
|
197
|
+
..
|
|
198
|
+
end
|
|
199
|
+
result = SampleOperation.execute(job_class: SampleBatch, data: data)
|
|
200
|
+
```
|
data/Rakefile
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bundler/gem_tasks"
|
|
4
|
+
require "rspec/core/rake_task"
|
|
5
|
+
require "standard/rake"
|
|
6
|
+
require "standalone_migrations"
|
|
7
|
+
|
|
8
|
+
RSpec::Core::RakeTask.new(:spec)
|
|
9
|
+
|
|
10
|
+
StandaloneMigrations::Tasks.load_tasks
|
|
11
|
+
|
|
12
|
+
task default: %i[spec standard]
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
class CreateBatchJobs < ActiveRecord::Migration[6.0]
|
|
2
|
+
def change
|
|
3
|
+
create_table :simple_batch_jobs do |t|
|
|
4
|
+
t.string :type, null: false, index: {unique: true}
|
|
5
|
+
t.string :description
|
|
6
|
+
t.timestamps
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
create_table :simple_batch_job_executions do |t|
|
|
10
|
+
t.references :simple_batch_job, foreign_key: true
|
|
11
|
+
t.datetime :created_at, null: false
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
create_table :simple_batch_job_events do |t|
|
|
15
|
+
t.references :simple_batch_job_execution, foreign_key: true
|
|
16
|
+
t.string :name, null: false
|
|
17
|
+
t.string :message
|
|
18
|
+
t.datetime :created_at, null: false
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
create_table :simple_batch_job_errors do |t|
|
|
22
|
+
t.references :simple_batch_job_execution, foreign_key: true
|
|
23
|
+
t.string :process, null: false
|
|
24
|
+
t.string :content, null: false
|
|
25
|
+
t.datetime :created_at, null: false
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require_relative "activerecord-simple_batch_job"
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
require "active_record"
|
|
2
|
+
require "dry/monads"
|
|
3
|
+
require_relative "../errors"
|
|
4
|
+
|
|
5
|
+
module SimpleBatchJob
|
|
6
|
+
class JobBase < ActiveRecord::Base
|
|
7
|
+
include Dry::Monads[:result]
|
|
8
|
+
|
|
9
|
+
self.table_name = "simple_batch_jobs"
|
|
10
|
+
|
|
11
|
+
#
|
|
12
|
+
# associations
|
|
13
|
+
#
|
|
14
|
+
|
|
15
|
+
has_many :batch_job_executions, class_name: "SimpleBatchJob::JobExecution", foreign_key: :simple_batch_job_id
|
|
16
|
+
alias_method :executions, :batch_job_executions
|
|
17
|
+
|
|
18
|
+
#
|
|
19
|
+
# Callbacks
|
|
20
|
+
#
|
|
21
|
+
|
|
22
|
+
after_initialize do
|
|
23
|
+
raise SequenceNotDefined.new if sequence.nil?
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
#
|
|
27
|
+
# ClassMethods
|
|
28
|
+
#
|
|
29
|
+
|
|
30
|
+
class << self
|
|
31
|
+
#
|
|
32
|
+
# @param [Array<Symbol>]
|
|
33
|
+
# @param [Array<Symbol>]
|
|
34
|
+
#
|
|
35
|
+
def sequence=(seq)
|
|
36
|
+
@_sequence = [:invoked] + seq + [:completed]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
#
|
|
40
|
+
# @param [String] guardfile
|
|
41
|
+
#
|
|
42
|
+
def guardfile=(guardfile)
|
|
43
|
+
@_guardfile = guardfile
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
#
|
|
48
|
+
# InstanceMethods
|
|
49
|
+
#
|
|
50
|
+
|
|
51
|
+
#
|
|
52
|
+
# @return [Array<Symbol>]
|
|
53
|
+
#
|
|
54
|
+
def sequence
|
|
55
|
+
self.class.instance_variable_get :@_sequence
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
#
|
|
59
|
+
# @return [String]
|
|
60
|
+
#
|
|
61
|
+
def guardfile
|
|
62
|
+
self.class.instance_variable_get(:@_guardfile) || self.class.to_s.underscore
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
#
|
|
66
|
+
# @return [Symbol, nil]
|
|
67
|
+
#
|
|
68
|
+
def last_got_event
|
|
69
|
+
execution = latest_execution
|
|
70
|
+
execution.current_event if execution && !execution.completed?
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
#
|
|
74
|
+
# @return [boolish]
|
|
75
|
+
#
|
|
76
|
+
def incompleted?
|
|
77
|
+
execution = latest_execution
|
|
78
|
+
!execution.nil? && !execution.completed?
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
private
|
|
82
|
+
|
|
83
|
+
#
|
|
84
|
+
# @return [JobExecution, nil]
|
|
85
|
+
#
|
|
86
|
+
def latest_execution
|
|
87
|
+
batch_job_executions.order(created_at: :desc, id: :desc).first
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
module SimpleBatchJob
|
|
2
|
+
class JobError < ActiveRecord::Base
|
|
3
|
+
self.table_name = "simple_batch_job_errors"
|
|
4
|
+
|
|
5
|
+
belongs_to :batch_job_execution, class_name: "SimpleBatchJob::JobExecution", foreign_key: :simple_batch_job_execution_id
|
|
6
|
+
alias_method :execution, :batch_job_execution
|
|
7
|
+
end
|
|
8
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
module SimpleBatchJob
|
|
2
|
+
class JobEvent < ActiveRecord::Base
|
|
3
|
+
self.table_name = "simple_batch_job_events"
|
|
4
|
+
|
|
5
|
+
#
|
|
6
|
+
# associations
|
|
7
|
+
#
|
|
8
|
+
|
|
9
|
+
belongs_to :batch_job_execution, class_name: "SimpleBatchJob::JobExecution", foreign_key: :simple_batch_job_execution_id
|
|
10
|
+
alias_method :execution, :batch_job_execution
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
require "active_record"
|
|
2
|
+
require "dry/monads"
|
|
3
|
+
require_relative "../errors"
|
|
4
|
+
|
|
5
|
+
module SimpleBatchJob
|
|
6
|
+
class JobExecution < ActiveRecord::Base
|
|
7
|
+
include Dry::Monads[:result]
|
|
8
|
+
|
|
9
|
+
self.table_name = "simple_batch_job_executions"
|
|
10
|
+
|
|
11
|
+
#
|
|
12
|
+
# associations
|
|
13
|
+
#
|
|
14
|
+
|
|
15
|
+
belongs_to :batch_job, class_name: "SimpleBatchJob::JobBase", foreign_key: :simple_batch_job_id
|
|
16
|
+
has_many :batch_job_events, class_name: "SimpleBatchJob::JobEvent", foreign_key: :simple_batch_job_execution_id
|
|
17
|
+
has_one :batch_job_error, class_name: "SimpleBatchJob::JobError", foreign_key: :simple_batch_job_execution_id
|
|
18
|
+
|
|
19
|
+
alias_method :job, :batch_job
|
|
20
|
+
alias_method :events, :batch_job_events
|
|
21
|
+
alias_method :error, :batch_job_error
|
|
22
|
+
|
|
23
|
+
#
|
|
24
|
+
# InstanceMethods
|
|
25
|
+
#
|
|
26
|
+
|
|
27
|
+
#
|
|
28
|
+
# @return [Dry::Monads::Result]
|
|
29
|
+
#
|
|
30
|
+
def result
|
|
31
|
+
@_result
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
#
|
|
35
|
+
# @return [Symbol]
|
|
36
|
+
#
|
|
37
|
+
def current_event
|
|
38
|
+
batch_job_events.order(created_at: :desc, id: :desc).limit(1).first.name&.to_sym
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
#
|
|
42
|
+
# @return [boolish]
|
|
43
|
+
#
|
|
44
|
+
def completed?
|
|
45
|
+
batch_job_events.exists?(name: :completed)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
#
|
|
49
|
+
# @param [String | Symbol] name
|
|
50
|
+
# @param [String] message
|
|
51
|
+
# @param [Proc] block
|
|
52
|
+
# @return [JobEvent]
|
|
53
|
+
#
|
|
54
|
+
def claim_event!(name:, message: nil)
|
|
55
|
+
raise AlreadyCompleted.new if completed?
|
|
56
|
+
|
|
57
|
+
batch_job_events.create(batch_job_execution: self, name: name, message: message)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
#
|
|
61
|
+
# @param [String] message
|
|
62
|
+
# @return [void]
|
|
63
|
+
#
|
|
64
|
+
def complete!(message: nil)
|
|
65
|
+
claim_event!(name: :completed, message: message)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
#
|
|
69
|
+
# Record JobbEvent according to the order of the sequence defined in JobBase
|
|
70
|
+
#
|
|
71
|
+
# given block, execute it
|
|
72
|
+
#
|
|
73
|
+
# @param [String] message
|
|
74
|
+
# @yield process chunk for next event
|
|
75
|
+
# @yieldreturn [Dry::Monads::Result<untyped>]
|
|
76
|
+
# @return [Dry::Monads::Result<untyped>]
|
|
77
|
+
# @raise [AlreadyCompleted]
|
|
78
|
+
#
|
|
79
|
+
def proceed!(message: nil, &block)
|
|
80
|
+
result = nil
|
|
81
|
+
|
|
82
|
+
seq = batch_job.sequence
|
|
83
|
+
next_sequence = seq[seq.find_index(current_event) + 1]
|
|
84
|
+
raise AlreadyCompleted.new unless next_sequence
|
|
85
|
+
|
|
86
|
+
result = block.call if block_given?
|
|
87
|
+
claim_event!(name: next_sequence, message: message)
|
|
88
|
+
|
|
89
|
+
result
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
#
|
|
93
|
+
# @param [String | Symbol] process
|
|
94
|
+
# @param [String] content
|
|
95
|
+
# @return [JobError]
|
|
96
|
+
#
|
|
97
|
+
def error!(process:, content:)
|
|
98
|
+
create_batch_job_error(process: process, content: content)
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module SimpleBatchJob
|
|
2
|
+
class Error < StandardError; end
|
|
3
|
+
|
|
4
|
+
class AlreadyCompleted < Error; end
|
|
5
|
+
|
|
6
|
+
class SequenceNotDefined < Error; end
|
|
7
|
+
|
|
8
|
+
class InvokingWithoutBlock < Error; end
|
|
9
|
+
|
|
10
|
+
class ProceedWithoutOperationChunkBlock < Error; end
|
|
11
|
+
|
|
12
|
+
class BasePathNotSpecified < Error; end
|
|
13
|
+
end
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
require "dry/monads"
|
|
2
|
+
|
|
3
|
+
module SimpleBatchJob
|
|
4
|
+
class MutexFileJobProcessor
|
|
5
|
+
include Dry::Monads[:result]
|
|
6
|
+
|
|
7
|
+
#
|
|
8
|
+
# @param [String | nil] base_path - directory path to store mutex and guard files
|
|
9
|
+
#
|
|
10
|
+
def initialize(base_path: nil)
|
|
11
|
+
@base_path = base_path
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
#
|
|
15
|
+
# @return [Pathname]
|
|
16
|
+
# @raise [BasePathNotSpecified]
|
|
17
|
+
#
|
|
18
|
+
def base_path
|
|
19
|
+
if @base_path
|
|
20
|
+
Pathname.new(@base_path)
|
|
21
|
+
elsif defined?(Rails) && Rails.respond_to?(:root)
|
|
22
|
+
Rails.root.join("storage", "lock_files")
|
|
23
|
+
else
|
|
24
|
+
raise BasePathNotSpecified.new "base_path must be specified in non-Rails environments"
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
#
|
|
29
|
+
# @return [Pathname]
|
|
30
|
+
#
|
|
31
|
+
def mutex
|
|
32
|
+
base_path.join("mutex")
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
#
|
|
36
|
+
# @param [JobBase] job - instance of job subclass
|
|
37
|
+
# @return [Pathname]
|
|
38
|
+
#
|
|
39
|
+
def guardfile(job:)
|
|
40
|
+
base_path.join(job.guardfile)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
#
|
|
44
|
+
# @yield code block want to control excusively
|
|
45
|
+
# @yieldreturn [untyped]
|
|
46
|
+
#
|
|
47
|
+
def with_mutex(&block)
|
|
48
|
+
prepare_mutex!
|
|
49
|
+
File.open(mutex, "w") do |f|
|
|
50
|
+
f.flock(File::LOCK_EX)
|
|
51
|
+
block.call
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
#
|
|
56
|
+
# @param [JobBase] job - instance of JobBase subclass
|
|
57
|
+
# @yield [job] all code with operation and ActiveRecord recording and more
|
|
58
|
+
# @yieldparam [JobBase] job - instance of Job subclass
|
|
59
|
+
# @yieldreturn [Dry::Monads::Result<untyped>]
|
|
60
|
+
#
|
|
61
|
+
def call(job:, &block)
|
|
62
|
+
@job = job
|
|
63
|
+
result = nil
|
|
64
|
+
|
|
65
|
+
with_mutex do
|
|
66
|
+
g = guardfile(job: job)
|
|
67
|
+
if running?(job: job)
|
|
68
|
+
return Failure("#{job.class} busy, skipped.")
|
|
69
|
+
else
|
|
70
|
+
File.write(g, "")
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
begin
|
|
75
|
+
result = block.call(job: job)
|
|
76
|
+
ensure
|
|
77
|
+
with_mutex do
|
|
78
|
+
FileUtils.rm(guardfile(job: job))
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
result
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
#
|
|
86
|
+
# @return [Integer | nil]
|
|
87
|
+
#
|
|
88
|
+
def prepare_mutex!
|
|
89
|
+
prepare_base_path!
|
|
90
|
+
File.write(mutex, "") unless File.exist? mutex
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
#
|
|
94
|
+
# @return [Array<String>]
|
|
95
|
+
#
|
|
96
|
+
def prepare_base_path!
|
|
97
|
+
FileUtils.mkdir_p(base_path) unless File.exist? mutex
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
#
|
|
101
|
+
# @return [boolish]
|
|
102
|
+
#
|
|
103
|
+
def running?(job:)
|
|
104
|
+
File.exist?(guardfile(job: job))
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
require "dry/operation"
|
|
2
|
+
require "dry/monads"
|
|
3
|
+
|
|
4
|
+
module SimpleBatchJob
|
|
5
|
+
class OperationBase < Dry::Operation
|
|
6
|
+
include Dry::Monads[:result]
|
|
7
|
+
extend Dry::Monads[:result]
|
|
8
|
+
|
|
9
|
+
class << self
|
|
10
|
+
#
|
|
11
|
+
# @param [JobProcessor] job_processor - JobProcessor instance
|
|
12
|
+
#
|
|
13
|
+
def job_processor=(job_processor)
|
|
14
|
+
@_job_processor = job_processor
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
#
|
|
18
|
+
# @return [JobProcessor] job_processor - JobProcessor instance
|
|
19
|
+
#
|
|
20
|
+
def processor
|
|
21
|
+
@_job_processor
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
#
|
|
25
|
+
# @param [JobBase] job_class - subclass of JobBase
|
|
26
|
+
# @param [String] message
|
|
27
|
+
# @param [boolish] reentrant
|
|
28
|
+
# @param [Organizer] organizer - organizer class
|
|
29
|
+
# @param [JobProcessor] job_processor - JobProcessor instance
|
|
30
|
+
# @return [JobExecution]
|
|
31
|
+
#
|
|
32
|
+
def execute(
|
|
33
|
+
*args,
|
|
34
|
+
job_class:,
|
|
35
|
+
message: nil,
|
|
36
|
+
reentrant: false,
|
|
37
|
+
organizer: Organizer,
|
|
38
|
+
job_processor: nil,
|
|
39
|
+
**kwargs
|
|
40
|
+
)
|
|
41
|
+
job_processor ||= processor || MutexFileJobProcessor.new
|
|
42
|
+
org = organizer.new(job_class: job_class, job_processor: job_processor, reentrant: reentrant)
|
|
43
|
+
org.call(*args, operation: self, message: message, **kwargs)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
#
|
|
47
|
+
# @param [JobBase] job - subclass of JobBase
|
|
48
|
+
# @param [JobProcessor] job_processor
|
|
49
|
+
# @return [boolish]
|
|
50
|
+
#
|
|
51
|
+
def running?(job_class:, job_processor: nil)
|
|
52
|
+
job_processor ||= processor || MutexFileJobProcessor.new
|
|
53
|
+
job_processor.running?(job: job_class.create_or_find_by(type: job_class.to_s))
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
#
|
|
58
|
+
# @param [Method | Proc] proceeder
|
|
59
|
+
#
|
|
60
|
+
def initialize(proceeder: nil)
|
|
61
|
+
@proceeder = proceeder
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
#
|
|
65
|
+
# given block, execute it
|
|
66
|
+
# ( with event recording when called from organizer )
|
|
67
|
+
#
|
|
68
|
+
# @param [String] message - message for recording event
|
|
69
|
+
# @yield process chunk for next event
|
|
70
|
+
# @yieldreturn [Dry::Monads::Result<untyped>]
|
|
71
|
+
# @return [Dry::Monads::Result<untyped>]
|
|
72
|
+
#
|
|
73
|
+
def proceed!(message: nil, &block)
|
|
74
|
+
if @proceeder
|
|
75
|
+
@proceeder.call(message: message) do
|
|
76
|
+
block.call
|
|
77
|
+
end
|
|
78
|
+
else
|
|
79
|
+
block.call
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
require "dry/monads"
|
|
2
|
+
require_relative "errors"
|
|
3
|
+
|
|
4
|
+
module SimpleBatchJob
|
|
5
|
+
class Organizer
|
|
6
|
+
include Dry::Monads[:result]
|
|
7
|
+
|
|
8
|
+
#
|
|
9
|
+
# @param [JobBase] job_class - batch job subclass
|
|
10
|
+
# @param [JobProcessor] job_processor - JobProcessor instance
|
|
11
|
+
# @param [boolish] reentrant
|
|
12
|
+
# @param [boolish] raise_error
|
|
13
|
+
#
|
|
14
|
+
def initialize(job_class:, job_processor:, reentrant: false, raise_error: false)
|
|
15
|
+
@job = job_class.create_or_find_by(type: job_class.to_s)
|
|
16
|
+
@job_processor = job_processor
|
|
17
|
+
@reentrant = reentrant
|
|
18
|
+
@sequence = @job.sequence
|
|
19
|
+
@last_got_event = @job.last_got_event
|
|
20
|
+
@raise_error = raise_error || (defined?(Rails) && Rails.respond_to?(:env) && Rails.env.production?)
|
|
21
|
+
end
|
|
22
|
+
attr_reader :sequence, :execution, :reentrant, :last_got_event, :raise_error
|
|
23
|
+
|
|
24
|
+
#
|
|
25
|
+
# roles:
|
|
26
|
+
# * wrap job operation with processor, and record first and last event
|
|
27
|
+
# * rescue all exceptions and publish as events and store to ActiveRecord
|
|
28
|
+
#
|
|
29
|
+
# @param [OperationBase] operation - subclass of Operation
|
|
30
|
+
# @param [String] message - start message
|
|
31
|
+
# @return [Dry::Monads::Result<JobExecution>]
|
|
32
|
+
#
|
|
33
|
+
def call(*args, operation:, message: nil, **kwargs)
|
|
34
|
+
@job_processor.call(job: @job) do
|
|
35
|
+
ex = @job.batch_job_executions.create(batch_job: @job)
|
|
36
|
+
@execution = ex
|
|
37
|
+
|
|
38
|
+
ex.claim_event!(name: :invoked, message: message)
|
|
39
|
+
result = nil
|
|
40
|
+
begin
|
|
41
|
+
result = operation.new(proceeder: method(:proceed!)).call(*args, **kwargs)
|
|
42
|
+
rescue => e
|
|
43
|
+
ActiveSupport::Notifications.instrument("simple_batch_job.exception", e) do
|
|
44
|
+
result = Failure(e.full_message(highlight: false))
|
|
45
|
+
end
|
|
46
|
+
raise e if raise_error
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
ex.instance_variable_set :@_result, result
|
|
50
|
+
if result.success?
|
|
51
|
+
ex.complete!
|
|
52
|
+
else
|
|
53
|
+
ex.error!(
|
|
54
|
+
process: sequence[sequence.find_index(ex.current_event) + 1],
|
|
55
|
+
content: result.failure
|
|
56
|
+
)
|
|
57
|
+
end
|
|
58
|
+
ex.freeze
|
|
59
|
+
|
|
60
|
+
result.either(->(r) { Success(ex) }, ->(r) { Failure(ex) })
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
#
|
|
65
|
+
# responsible for event recoding with JobExecution and branching
|
|
66
|
+
# execution and skipping process when reentrant
|
|
67
|
+
#
|
|
68
|
+
# @param [String] message - message for recording event
|
|
69
|
+
# @yield process chunk for next event
|
|
70
|
+
# @yieldreturn [Dry::Monads::Result<untyped>]
|
|
71
|
+
# @return [Dry::Monads::Result<untyped>]
|
|
72
|
+
# @raise [ProceedWithoutOperationChunkBlock]
|
|
73
|
+
#
|
|
74
|
+
def proceed!(message: nil, &block)
|
|
75
|
+
raise ProceedWithoutOperationChunkBlock.new unless block_given?
|
|
76
|
+
|
|
77
|
+
idx = sequence.find_index(execution.current_event)
|
|
78
|
+
|
|
79
|
+
if last_got_event && reentrant && idx < sequence.find_index(last_got_event)
|
|
80
|
+
execution.proceed!(message: "skip until catch up with last wip execution") do
|
|
81
|
+
Success(:skip)
|
|
82
|
+
end
|
|
83
|
+
else
|
|
84
|
+
execution.proceed!(message: message) do
|
|
85
|
+
block.call
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rake/tasklib"
|
|
4
|
+
require "fileutils"
|
|
5
|
+
|
|
6
|
+
module SimpleBatchJob
|
|
7
|
+
#
|
|
8
|
+
# Usage: write as below in your Rakefile
|
|
9
|
+
# ```
|
|
10
|
+
# require "simple_batch_job/tasks"
|
|
11
|
+
# SimpleBatchJob::Tasks.new
|
|
12
|
+
# ```
|
|
13
|
+
#
|
|
14
|
+
class Tasks < Rake::TaskLib
|
|
15
|
+
attr_accessor :verbose
|
|
16
|
+
|
|
17
|
+
# options: { verbose: true/false }
|
|
18
|
+
def initialize(options = {})
|
|
19
|
+
env_verbose = ENV["VERBOSE"] == "1" || ENV["VERBOSE"] == "true"
|
|
20
|
+
@verbose = options.fetch(:verbose, env_verbose)
|
|
21
|
+
define
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def define
|
|
25
|
+
namespace :simple_batch_job do
|
|
26
|
+
tasks = self
|
|
27
|
+
desc "Copy migration file to your project with current timestamp"
|
|
28
|
+
task :install do
|
|
29
|
+
source_dir = File.expand_path("../../db/migrate", __dir__)
|
|
30
|
+
target_dir = File.join(Dir.pwd, "db", "migrate")
|
|
31
|
+
|
|
32
|
+
files = Dir.glob(File.join(source_dir, "*.rb")).sort
|
|
33
|
+
|
|
34
|
+
dests = tasks.dest_files(files, target_dir)
|
|
35
|
+
abort("[ERROR] destination exists: \n" + dests.join("\n")) if dests.any? { |e| File.exist?(e) }
|
|
36
|
+
|
|
37
|
+
# Phase 2: perform copies
|
|
38
|
+
FileUtils.mkdir_p(target_dir)
|
|
39
|
+
files.zip(dests).each do |src, dest|
|
|
40
|
+
abort("[ERROR] destination exists: \n" + dests.join("\n")) if File.exist?(dest)
|
|
41
|
+
|
|
42
|
+
FileUtils.cp(src, dest, verbose: @verbose)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
puts "[simple_batch_job] install finished" if @verbose
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
#
|
|
51
|
+
# @param [Array<String>] files
|
|
52
|
+
# @param [String] target_dir
|
|
53
|
+
# @return [Array<String>]
|
|
54
|
+
#
|
|
55
|
+
def dest_files(files, target_dir)
|
|
56
|
+
start_time = Time.now.strftime("%Y%m%d%H%M%S").to_i
|
|
57
|
+
|
|
58
|
+
files.map.with_index do |src, i|
|
|
59
|
+
name = File.basename(src).sub(/^\d+_/, "")
|
|
60
|
+
File.join(target_dir, "#{start_time + i}_#{name}")
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
module SimpleBatchJob
|
|
2
|
+
class JobBase < ActiveRecord::Base
|
|
3
|
+
self.@_sequence: Array[Symbol]
|
|
4
|
+
|
|
5
|
+
self.@_guardfile: (String | nil)
|
|
6
|
+
|
|
7
|
+
alias executions batch_job_executions
|
|
8
|
+
|
|
9
|
+
#
|
|
10
|
+
# @param [Array<Symbol>]
|
|
11
|
+
# @param [Array<Symbol>]
|
|
12
|
+
#
|
|
13
|
+
def self.sequence=: (Array[Symbol] seq) -> Array[Symbol]
|
|
14
|
+
|
|
15
|
+
#
|
|
16
|
+
# @param [String] guardfile
|
|
17
|
+
#
|
|
18
|
+
def self.guardfile=: (String guardfile) -> String
|
|
19
|
+
|
|
20
|
+
#
|
|
21
|
+
# @return [Array<Symbol>]
|
|
22
|
+
#
|
|
23
|
+
def sequence: () -> Array[Symbol]
|
|
24
|
+
|
|
25
|
+
#
|
|
26
|
+
# @return [String]
|
|
27
|
+
#
|
|
28
|
+
def guardfile: () -> String
|
|
29
|
+
|
|
30
|
+
#
|
|
31
|
+
# @return [Symbol, nil]
|
|
32
|
+
#
|
|
33
|
+
def last_got_event: () -> (Symbol | nil)
|
|
34
|
+
|
|
35
|
+
#
|
|
36
|
+
# @return [boolish]
|
|
37
|
+
#
|
|
38
|
+
def incompleted?: () -> bool
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
#
|
|
43
|
+
# @return [JobExecution, nil]
|
|
44
|
+
#
|
|
45
|
+
def latest_execution: () -> (SimpleBatchJob::JobExecution | nil)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
module SimpleBatchJob
|
|
2
|
+
class JobExecution < ActiveRecord::Base
|
|
3
|
+
alias job batch_job
|
|
4
|
+
|
|
5
|
+
alias events batch_job_events
|
|
6
|
+
|
|
7
|
+
alias error batch_job_error
|
|
8
|
+
|
|
9
|
+
#
|
|
10
|
+
# @return [Dry::Monads::Result]
|
|
11
|
+
#
|
|
12
|
+
def result: () -> Dry::Monads::Result[untyped]
|
|
13
|
+
|
|
14
|
+
#
|
|
15
|
+
# @return [Symbol]
|
|
16
|
+
#
|
|
17
|
+
def current_event: () -> (Symbol | nil)
|
|
18
|
+
|
|
19
|
+
#
|
|
20
|
+
# @return [boolish]
|
|
21
|
+
#
|
|
22
|
+
def completed?: () -> bool
|
|
23
|
+
|
|
24
|
+
#
|
|
25
|
+
# @param [String | Symbol] name
|
|
26
|
+
# @param [String] message
|
|
27
|
+
# @param [Proc] block
|
|
28
|
+
# @return [JobEvent]
|
|
29
|
+
#
|
|
30
|
+
def claim_event!: (name: (String | Symbol), ?message: String?) -> SimpleBatchJob::JobbEvent
|
|
31
|
+
|
|
32
|
+
#
|
|
33
|
+
# @param [String] message
|
|
34
|
+
# @return [JobEvent]
|
|
35
|
+
#
|
|
36
|
+
def complete!: (?message: String?) -> SimpleBatchJob::JobEvent
|
|
37
|
+
|
|
38
|
+
#
|
|
39
|
+
# Record JobbEvent according to the order of the sequence defined in JobBase
|
|
40
|
+
#
|
|
41
|
+
# given block, execute it
|
|
42
|
+
#
|
|
43
|
+
# @param [String] message
|
|
44
|
+
# @yield process chunk for next event
|
|
45
|
+
# @yieldreturn [Dry::Monads::Result<untyped>]
|
|
46
|
+
# @return [Dry::Monads::Result<untyped>]
|
|
47
|
+
# @raise [AlreadyCompleted]
|
|
48
|
+
#
|
|
49
|
+
def proceed!: (?message: String?) ?{ () -> Dry::Monads::Result[untyped] } -> Dry::Monads::Result[untyped]
|
|
50
|
+
|
|
51
|
+
#
|
|
52
|
+
# @param [String | Symbol] process
|
|
53
|
+
# @param [String] content
|
|
54
|
+
# @return [JobError]
|
|
55
|
+
#
|
|
56
|
+
def error!: (process: (String | Symbol), content: String) -> SimpleBatchJob::JobError
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module SimpleBatchJob
|
|
2
|
+
class Error < StandardError
|
|
3
|
+
end
|
|
4
|
+
|
|
5
|
+
class AlreadyCompleted < Error
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
class SequenceNotDefined < Error
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
class InvokingWithoutBlock < Error
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class ProceedWithoutOperationChunkBlock < Error
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
class BasePathNotSpecified < Error
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
module SimpleBatchJob
|
|
2
|
+
class MutexFileJobProcessor
|
|
3
|
+
@base_path: (String | Pathname | nil)
|
|
4
|
+
|
|
5
|
+
@job: (SimpleBatchJob::JobBase | nil)
|
|
6
|
+
|
|
7
|
+
#
|
|
8
|
+
# @param [String | nil] base_path - directory path to store mutex and guard files
|
|
9
|
+
#
|
|
10
|
+
def initialize: (?base_path: (String | Pathname)?) -> void
|
|
11
|
+
|
|
12
|
+
#
|
|
13
|
+
# @return [Pathname]
|
|
14
|
+
# @raise [BasePathNotSpecified]
|
|
15
|
+
#
|
|
16
|
+
def base_path: () -> Pathname
|
|
17
|
+
|
|
18
|
+
#
|
|
19
|
+
# @return [Pathname]
|
|
20
|
+
#
|
|
21
|
+
def mutex: () -> Pathname
|
|
22
|
+
|
|
23
|
+
#
|
|
24
|
+
# @param [JobBase] job - instance of job subclass
|
|
25
|
+
# @return [Pathname]
|
|
26
|
+
#
|
|
27
|
+
def guardfile: (job: SimpleBatchJob::JobBase) -> Pathname
|
|
28
|
+
|
|
29
|
+
#
|
|
30
|
+
# @yield code block want to control excusively
|
|
31
|
+
# @yieldreturn [untyped]
|
|
32
|
+
#
|
|
33
|
+
def with_mutex: () { () -> untyped } -> untyped
|
|
34
|
+
|
|
35
|
+
#
|
|
36
|
+
# @param [JobBase] job - instance of JobBase subclass
|
|
37
|
+
# @yield [job] all code with operation and ActiveRecord recording and more
|
|
38
|
+
# @yieldparam [JobBase] job - instance of Job subclass
|
|
39
|
+
# @yieldreturn [Dry::Monads::Result<untyped>]
|
|
40
|
+
#
|
|
41
|
+
def call: (job: SimpleBatchJob::JobBase) { (job: SimpleBatchJob::JobBase) -> Dry::Monads::Result[untyped] } -> Dry::Monads::Result[untyped]
|
|
42
|
+
|
|
43
|
+
#
|
|
44
|
+
# @return [Integer | nil]
|
|
45
|
+
#
|
|
46
|
+
def prepare_mutex!: () -> (Integer | nil)
|
|
47
|
+
|
|
48
|
+
#
|
|
49
|
+
# @return [Array<String>]
|
|
50
|
+
#
|
|
51
|
+
def prepare_base_path!: () -> (Array[String] | nil)
|
|
52
|
+
|
|
53
|
+
#
|
|
54
|
+
# @return [boolish]
|
|
55
|
+
#
|
|
56
|
+
def running?: (job: SimpleBatchJob::JobBase) -> bool
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
module SimpleBatchJob
|
|
2
|
+
class OperationBase < Dry::Operation
|
|
3
|
+
self.@_job_processor: (SimpleBatchJob::MutexFileJobProcessor | untyped)
|
|
4
|
+
|
|
5
|
+
@proceeder: (Method | Proc)?
|
|
6
|
+
|
|
7
|
+
#
|
|
8
|
+
# @param [JobProcessor] job_processor - JobProcessor instance
|
|
9
|
+
#
|
|
10
|
+
def self.job_processor=: ((SimpleBatchJob::MutexFileJobProcessor | untyped) job_processor) -> void
|
|
11
|
+
|
|
12
|
+
#
|
|
13
|
+
# @return [JobProcessor] job_processor - JobProcessor instance
|
|
14
|
+
#
|
|
15
|
+
def self.processor: () -> (SimpleBatchJob::MutexFileJobProcessor | untyped)
|
|
16
|
+
|
|
17
|
+
#
|
|
18
|
+
# @param [JobBase] job_class - subclass of JobBase
|
|
19
|
+
# @param [String] message
|
|
20
|
+
# @param [boolish] reentrant
|
|
21
|
+
# @param [Organizer] organizer - organizer class
|
|
22
|
+
# @param [JobProcessor] job_processor - JobProcessor instance
|
|
23
|
+
# @return [JobExecution]
|
|
24
|
+
#
|
|
25
|
+
def self.execute: (*untyped args, job_class: Class[SimpleBatchJob::JobBase], ?message: String?, ?reentrant: bool, ?organizer: Class[SimpleBatchJob::Organizer], ?job_processor: (SimpleBatchJob::MutexFileJobProcessor | untyped)?, **untyped kwargs) -> Dry::Monads::Result[SimpleBatchJob::JobExecution]
|
|
26
|
+
|
|
27
|
+
#
|
|
28
|
+
# @param [JobBase] job - subclass of JobBase
|
|
29
|
+
# @param [JobProcessor] job_processor
|
|
30
|
+
# @return [boolish]
|
|
31
|
+
#
|
|
32
|
+
def self.running?: (job_class: Class[SimpleBatchJob::JobBase], ?job_processor: (SimpleBatchJob::MutexFileJobProcessor | untyped)?) -> bool
|
|
33
|
+
|
|
34
|
+
#
|
|
35
|
+
# @param [Method | Proc] proceeder
|
|
36
|
+
#
|
|
37
|
+
def initialize: (?proceeder: (Method | Proc)?) -> void
|
|
38
|
+
|
|
39
|
+
#
|
|
40
|
+
# given block, execute it
|
|
41
|
+
# ( with event recording when called from organizer )
|
|
42
|
+
#
|
|
43
|
+
# @param [String] message - message for recording event
|
|
44
|
+
# @yield process chunk for next event
|
|
45
|
+
# @yieldreturn [Dry::Monads::Result<untyped>]
|
|
46
|
+
# @return [Dry::Monads::Result<untyped>]
|
|
47
|
+
#
|
|
48
|
+
def proceed!: (?message: String?) { () -> Dry::Monads::Result[untyped] } -> Dry::Monads::Result[untyped]
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
module SimpleBatchJob
|
|
2
|
+
class Organizer
|
|
3
|
+
@job: SimpleBatchJob::JobBase
|
|
4
|
+
|
|
5
|
+
@job_processor: (SimpleBatchJob::MutexFileJobProcessor | untyped)
|
|
6
|
+
|
|
7
|
+
@reentrant: bool
|
|
8
|
+
|
|
9
|
+
@sequence: Array[Symbol]
|
|
10
|
+
|
|
11
|
+
@last_got_event: Symbol?
|
|
12
|
+
|
|
13
|
+
@raise_error: bool
|
|
14
|
+
|
|
15
|
+
#
|
|
16
|
+
# @param [JobBase] job_class - batch job subclass
|
|
17
|
+
# @param [JobProcessor] job_processor - JobProcessor instance
|
|
18
|
+
# @param [boolish] reentrant
|
|
19
|
+
# @param [boolish] raise_error
|
|
20
|
+
#
|
|
21
|
+
def initialize: (job_class: Class[SimpleBatchJob::JobBase], job_processor: (SimpleBatchJob::MutexFileJobProcessor | untyped), ?reentrant: bool, ?raise_error: bool) -> void
|
|
22
|
+
|
|
23
|
+
attr_reader sequence: Array[Symbol]
|
|
24
|
+
|
|
25
|
+
attr_reader execution: (SimpleBatchJob::JobExecution | nil)
|
|
26
|
+
|
|
27
|
+
attr_reader reentrant: bool
|
|
28
|
+
|
|
29
|
+
attr_reader last_got_event: Symbol?
|
|
30
|
+
|
|
31
|
+
attr_reader raise_error: bool
|
|
32
|
+
|
|
33
|
+
#
|
|
34
|
+
# roles:
|
|
35
|
+
# * wrap job operation with processor, and record first and last event
|
|
36
|
+
# * rescue all exceptions and publish as events and store to ActiveRecord
|
|
37
|
+
#
|
|
38
|
+
# @param [OperationBase] operation - subclass of Operation
|
|
39
|
+
# @param [String] message - start message
|
|
40
|
+
# @return [Dry::Monads::Result<JobExecution>]
|
|
41
|
+
#
|
|
42
|
+
def call: (*untyped args, operation: Class[SimpleBatchJob::OperationBase], ?message: String?, **untyped kwargs) -> Dry::Monads::Result[SimpleBatchJob::JobExecution]
|
|
43
|
+
|
|
44
|
+
#
|
|
45
|
+
# responsible for event recoding with JobExecution and branching
|
|
46
|
+
# execution and skipping process when reentrant
|
|
47
|
+
#
|
|
48
|
+
# @param [String] message - message for recording event
|
|
49
|
+
# @yield process chunk for next event
|
|
50
|
+
# @yieldreturn [Dry::Monads::Result<untyped>]
|
|
51
|
+
# @return [Dry::Monads::Result<untyped>]
|
|
52
|
+
# @raise [ProceedWithoutOperationChunkBlock]
|
|
53
|
+
#
|
|
54
|
+
def proceed!: (?message: String?) { () -> Dry::Monads::Result[untyped] } -> Dry::Monads::Result[untyped]
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
module SimpleBatchJob
|
|
2
|
+
end
|
|
3
|
+
|
|
4
|
+
module SimpleBatchJob
|
|
5
|
+
VERSION: "0.1.0"
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
module SimpleBatchJob
|
|
9
|
+
#
|
|
10
|
+
# Usage: write as below in your Rakefile
|
|
11
|
+
# ```
|
|
12
|
+
# require "simple_batch_job/tasks"
|
|
13
|
+
# SimpleBatchJob::Tasks.new
|
|
14
|
+
# ```
|
|
15
|
+
#
|
|
16
|
+
class Tasks < Rake::TaskLib
|
|
17
|
+
@verbose: untyped
|
|
18
|
+
|
|
19
|
+
attr_accessor verbose: untyped
|
|
20
|
+
|
|
21
|
+
# options: { verbose: true/false }
|
|
22
|
+
def initialize: (?::Hash[untyped, untyped] options) -> void
|
|
23
|
+
|
|
24
|
+
def define: () -> untyped
|
|
25
|
+
|
|
26
|
+
#
|
|
27
|
+
# @param [Array<String>] files
|
|
28
|
+
# @param [String] target_dir
|
|
29
|
+
# @return [Array<String>]
|
|
30
|
+
#
|
|
31
|
+
def dest_files: (untyped files, untyped target_dir) -> untyped
|
|
32
|
+
end
|
|
33
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: activerecord-simple-batch-job
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.2
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Colorfulcompany, Inc.
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-11-29 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: activerecord
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '6.0'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '6.0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: dry-operation
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '1.0'
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '1.0'
|
|
41
|
+
description: A library for managing batch jobs with execution records, concurrency
|
|
42
|
+
control, and error handling, built on ActiveRecord and dry-operation. Supports both
|
|
43
|
+
Rails and non-Rails environments.
|
|
44
|
+
email:
|
|
45
|
+
executables: []
|
|
46
|
+
extensions: []
|
|
47
|
+
extra_rdoc_files: []
|
|
48
|
+
files:
|
|
49
|
+
- ".rspec"
|
|
50
|
+
- ".standard.yml"
|
|
51
|
+
- CHANGELOG.md
|
|
52
|
+
- README.md
|
|
53
|
+
- Rakefile
|
|
54
|
+
- db/migrate/20250820072125_create_batch_jobs.rb
|
|
55
|
+
- lib/activerecord-simple-batch-job.rb
|
|
56
|
+
- lib/activerecord-simple_batch_job.rb
|
|
57
|
+
- lib/simple_batch_job.rb
|
|
58
|
+
- lib/simple_batch_job/active_record/job_base.rb
|
|
59
|
+
- lib/simple_batch_job/active_record/job_error.rb
|
|
60
|
+
- lib/simple_batch_job/active_record/job_event.rb
|
|
61
|
+
- lib/simple_batch_job/active_record/job_execution.rb
|
|
62
|
+
- lib/simple_batch_job/errors.rb
|
|
63
|
+
- lib/simple_batch_job/mutex_file_job_processor.rb
|
|
64
|
+
- lib/simple_batch_job/operation_base.rb
|
|
65
|
+
- lib/simple_batch_job/organizer.rb
|
|
66
|
+
- lib/simple_batch_job/tasks.rb
|
|
67
|
+
- lib/simple_batch_job/version.rb
|
|
68
|
+
- sig/simple_batch_job.rbs
|
|
69
|
+
- sig/simple_batch_job/active_record/job_base.rbs
|
|
70
|
+
- sig/simple_batch_job/active_record/job_error.rbs
|
|
71
|
+
- sig/simple_batch_job/active_record/job_event.rbs
|
|
72
|
+
- sig/simple_batch_job/active_record/job_execution.rbs
|
|
73
|
+
- sig/simple_batch_job/errors.rbs
|
|
74
|
+
- sig/simple_batch_job/mutex_file_job_processor.rbs
|
|
75
|
+
- sig/simple_batch_job/operation_base.rbs
|
|
76
|
+
- sig/simple_batch_job/organizer.rbs
|
|
77
|
+
homepage: https://github.com/colorfulcompany/activerecord-collection
|
|
78
|
+
licenses: []
|
|
79
|
+
metadata:
|
|
80
|
+
allowed_push_host: https://rubygems.org
|
|
81
|
+
homepage_uri: https://github.com/colorfulcompany/activerecord-collection
|
|
82
|
+
source_code_uri: https://github.com/colorfulcompany/activerecord-collection
|
|
83
|
+
changelog_uri: https://github.com/colorfulcompany/activerecord-collection/blob/main/simple-batch-job/CHANGELOG.md
|
|
84
|
+
post_install_message:
|
|
85
|
+
rdoc_options: []
|
|
86
|
+
require_paths:
|
|
87
|
+
- lib
|
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
89
|
+
requirements:
|
|
90
|
+
- - ">="
|
|
91
|
+
- !ruby/object:Gem::Version
|
|
92
|
+
version: 3.1.0
|
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
|
+
requirements:
|
|
95
|
+
- - ">="
|
|
96
|
+
- !ruby/object:Gem::Version
|
|
97
|
+
version: '0'
|
|
98
|
+
requirements: []
|
|
99
|
+
rubygems_version: 3.5.22
|
|
100
|
+
signing_key:
|
|
101
|
+
specification_version: 4
|
|
102
|
+
summary: Simple batch job management library using ActiveRecord and dry-operation.
|
|
103
|
+
test_files: []
|