back_ops 0.1.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: 543bd1222115f9fa55b46ef603aaebd4100b8cbf4e817d1d4e598d50106e2227
4
+ data.tar.gz: 8fc4ca44128a2d74ca4ca714ebf6dea6139fc0ee0c250f4103314013b5c7bcd2
5
+ SHA512:
6
+ metadata.gz: b32d62c84c8703135789dc506243f7c5d27cec7e8987bcecf34ae9a78298ef296f0d04e27676132dca5325ef5ee1b2ac356fa413bd43f6eb77537918f66e7e89
7
+ data.tar.gz: 88ab67dc53c0604f95edc4f7c8e4d0473d972b7685c42bc558deaebeb714bf7d91921388b703a093ece8caff81d07311e9721a96fb9af49876e9d607f389febd
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 Aaron Price
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,92 @@
1
+ # BackOps
2
+
3
+ Back Ops is intended for background processing of jobs that require multiple tasks to be completed. It executes each task in sequence in a separate Sidekiq worker. This allows for jobs to be retryable if failures occur, but completed tasks are not retried.
4
+
5
+ Progress and error states are tracked in the database, so that that you are always aware of what was processed, and if any task fails, where the failure occured in the process, what the error message is, what the stack trace is, so you know what's happening and you can always retry the job from the failed task.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'back_ops'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install back_ops
22
+ ```
23
+
24
+ ## Install migrations
25
+ Copy the migration from the gem to your application, then run migrations.
26
+
27
+ ```bash
28
+ $ rails g back_ops:install
29
+ $ rails db:migrate
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ There are two parts to this process. First, define an operation, which accepts a set of params, and an array of actions (as the names of the classes those actions will be called). Parameters should be simple types (e.g.: Integer, String, Date/Timestamp) instead of full objects because these params will be sent serialized and sent to redis for each defined task.
35
+
36
+ ```ruby
37
+ module Subscriptions
38
+ module Operations
39
+ class Fullfillment
40
+ def self.call(subscription)
41
+ BackOps::Worker.perform_async({
42
+ 'subscription_id' => subscription.id
43
+ }, [
44
+ Subscriptions::Actions::Fulfillment::ChargeCreditCard,
45
+ Subscriptions::Actions::Fulfillment::SendEmailReceipt
46
+ ])
47
+ end
48
+ end
49
+ end
50
+ end
51
+ ```
52
+
53
+ Each action receives the operation object which contains the context.
54
+
55
+ ```ruby
56
+ module Subscriptions
57
+ module Actions
58
+ module Fulfillment
59
+ class ChargeCreditCard
60
+ def self.call(operation)
61
+ subscription_id = operation.context['subscription_id']
62
+ subscription = Subscription.find(subscription_id)
63
+ # ...
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ ```
70
+
71
+ You now also have full transparency into each operation and can view it in the admin section by invoking the following code.
72
+
73
+ ```ruby
74
+ params = { 'subscription_id' => subscription.id }
75
+ operation = BackOps::Operation.includes(:actions).where("name = 'Subscriptions::Operations::Fulfillment' AND context @> ?", params.to_json).first
76
+ ```
77
+
78
+
79
+ ## Contributing
80
+ To contribute to this project. Clone the repo and create a branch for your changes. When complete, create a pull request into the `develop` branch of this project. Ensure that positive and negative test cases cover your changes and all tests pass.
81
+
82
+ Be detailed in your commit message of what changes you're making and the motivation behind the changes.
83
+
84
+ ## License
85
+
86
+ Copyright 2021 Aaron Price
87
+
88
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
89
+
90
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
91
+
92
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
data/lib/back_ops.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'active_record'
2
+ require 'sidekiq'
3
+
4
+ require 'back_ops/action'
5
+ require 'back_ops/operation'
6
+ require 'back_ops/version'
7
+ require 'back_ops/worker'
8
+
9
+ module BackOps
10
+ end
@@ -0,0 +1,26 @@
1
+ module BackOps
2
+ class Action < ActiveRecord::Base
3
+
4
+ # == Constants ============================================================
5
+
6
+ # == Attributes ===========================================================
7
+
8
+ # == Extensions ===========================================================
9
+
10
+ # == Relationships ========================================================
11
+
12
+ belongs_to :operation, class_name: 'BackOps::Operation'
13
+
14
+ # == Validations ==========================================================
15
+
16
+ # == Scopes ===============================================================
17
+
18
+ # == Callbacks ============================================================
19
+
20
+ # == Class Methods ========================================================
21
+
22
+ self.table_name = 'back_ops_actions'
23
+
24
+ # == Instance Methods =====================================================
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ module BackOps
2
+ class Operation < ActiveRecord::Base
3
+
4
+ # == Constants ============================================================
5
+
6
+ # == Attributes ===========================================================
7
+
8
+ # == Extensions ===========================================================
9
+
10
+ # == Relationships ========================================================
11
+
12
+ has_many :actions, class_name: 'BackOps::Action'
13
+
14
+ # == Validations ==========================================================
15
+
16
+ # == Scopes ===============================================================
17
+
18
+ # == Callbacks ============================================================
19
+
20
+ # == Class Methods ========================================================
21
+
22
+ self.table_name = 'back_ops_operations'
23
+
24
+ # == Instance Methods =====================================================
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module BackOps
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,102 @@
1
+ module BackOps
2
+ class Worker
3
+
4
+ # == Constants ============================================================
5
+
6
+ # == Attributes ===========================================================
7
+
8
+ # == Extensions ===========================================================
9
+
10
+ include Sidekiq::Worker
11
+
12
+ # == Aliases ==============================================================
13
+
14
+ # == Class Methods ========================================================
15
+
16
+ def self.perform_async(context, actions)
17
+ operation = setup_operation_and_actions(context, actions)
18
+ super(operation.id)
19
+ end
20
+
21
+ def self.perform_in(interval, context, actions)
22
+ operation = setup_operation_and_actions(context, actions)
23
+ super(interval, operation.id)
24
+ end
25
+
26
+ def self.perform_at(interval, context, action)
27
+ perform_in(interval, context, action)
28
+ end
29
+
30
+ def self.setup_operation_and_actions(context, actions)
31
+ raise ArgumentError, 'Cannot process empty actions' if actions.blank?
32
+
33
+ operation = BackOps::Operation.create_or_find_by({
34
+ params_hash: Digest::MD5.hexdigest("#{context}|#{actions}"),
35
+ name: ancestors[1]
36
+ })
37
+ operation.context.merge!(context)
38
+ operation.save!
39
+
40
+ actions.each_with_index do |action, index|
41
+ BackOps::Action.create_or_find_by({
42
+ operation: operation,
43
+ name: action,
44
+ order: index
45
+ })
46
+ end
47
+
48
+ operation
49
+ end
50
+
51
+ # == Instance Methods =====================================================
52
+
53
+ def perform(operation_id)
54
+ operation = BackOps::Operation.find(operation_id)
55
+ process(operation)
56
+ end
57
+
58
+ private
59
+
60
+ def process(operation)
61
+ action_items = BackOps::Action.where({
62
+ operation: operation,
63
+ completed_at: nil
64
+ }).order(order: :asc)
65
+
66
+ return true if action_items.blank?
67
+
68
+ active_item = action_items[0]
69
+ next_item = action_items[1]
70
+
71
+ if active_item.errored_at.present?
72
+ active_item.errored_at = nil
73
+ active_item.error_message = nil
74
+ active_item.stack_trace = nil
75
+ active_item.save!
76
+ end
77
+
78
+ begin
79
+ active_item.name.constantize.call(operation)
80
+
81
+ active_item.completed_at = Time.zone.now
82
+ active_item.attempts_count += 1
83
+ active_item.save!
84
+
85
+ if next_item.present?
86
+ Sidekiq::Client.push('class' => self.class.name, 'args' => [operation.id])
87
+ else
88
+ operation.completed_at = active_item.completed_at
89
+ operation.save!
90
+ end
91
+ rescue => e
92
+ active_item.error_message = e.message
93
+ active_item.stack_trace = e.backtrace
94
+ active_item.errored_at = Time.zone.now
95
+ active_item.attempts_count += 1
96
+ active_item.save!
97
+
98
+ raise
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ # require 'rails/generators'
4
+ require 'rails/generators/active_record'
5
+
6
+ module BackOps
7
+ class InstallGenerator < Rails::Generators::Base
8
+ include ActiveRecord::Generators::Migration
9
+ source_root File.expand_path('templates', __dir__)
10
+ desc 'Installs the BackOps migration file.'
11
+
12
+ def create_migration_file
13
+ migration_template(
14
+ 'migration.rb',
15
+ 'db/migrate/create_back_ops_tables.rb',
16
+ migration_version: migration_version,
17
+ )
18
+ end
19
+
20
+ # def self.next_migration_number(dirname)
21
+ # ActiveRecord::Migration.next_migration_number(dirname)
22
+ # end
23
+
24
+ private
25
+
26
+ def migration_version
27
+ "[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version %>
4
+ def change
5
+ create_table :back_ops_operations do |t|
6
+ t.string :name
7
+ t.string :params_hash
8
+ t.jsonb :context, null: false, default: {}
9
+ t.timestamp :completed_at
10
+
11
+ t.timestamps
12
+ end
13
+
14
+ add_index :back_ops_operations, [:name, :params_hash]
15
+
16
+ create_table :back_ops_actions do |t|
17
+ t.integer :operation_id, limit: 8
18
+ t.integer :order, null: false, default: 0
19
+ t.text :name
20
+ t.text :error_message
21
+ t.text :stack_trace
22
+ t.timestamp :errored_at
23
+ t.timestamp :completed_at
24
+ t.integer :attempts_count, null: false, default: 0
25
+
26
+ t.timestamps
27
+ end
28
+
29
+ add_index :back_ops_actions, :operation_id
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: back_ops
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Price
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-03-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.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '6.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '1.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '1.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: redis
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '4.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '4.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sidekiq
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '6.1'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '6.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: factory_bot
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '6.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '6.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '4.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '4.0'
97
+ description: BackOps processes multi-step jobs using Sidekiq in a retry-from-failed
98
+ fashion.
99
+ email:
100
+ - price.aaron@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - MIT-LICENSE
106
+ - README.md
107
+ - Rakefile
108
+ - lib/back_ops.rb
109
+ - lib/back_ops/action.rb
110
+ - lib/back_ops/operation.rb
111
+ - lib/back_ops/version.rb
112
+ - lib/back_ops/worker.rb
113
+ - lib/generators/back_ops/install_generator.rb
114
+ - lib/generators/back_ops/templates/migration.rb.tt
115
+ homepage: https://github.com/aaronprice/back_ops
116
+ licenses:
117
+ - MIT
118
+ metadata:
119
+ homepage_uri: https://github.com/aaronprice/back_ops
120
+ source_code_uri: https://github.com/aaronprice/back_ops
121
+ changelog_uri: https://github.com/aaronprice/back_ops/blob/master/CHANGELOG.md
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: 2.3.0
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubygems_version: 3.2.3
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Multi-step background job processor
141
+ test_files: []