sidekiq-sequence 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c624a6fd72d24ee9e41f0efc66acbab3ae57fc99ddc38121c76b25f34b2dbf95
4
+ data.tar.gz: 7af58740782a790349cfcb31416eda672310782887270e5709c905b6f3d5f518
5
+ SHA512:
6
+ metadata.gz: 98b0ec2a669b2b8b632f00494b48aa5e3a953e297b4ca09fd5f5402c763f96c1ae441e9276ab37d139bbbd71fb4b937a0e546ca92bafadfe0dd00f6c25e841e0
7
+ data.tar.gz: 295b926a199219d4b9a0f8df2e877ab0a9aedd658fe4afc40bc1d9c01055d8fd6a04cdf0461fb3a0e8dcd476a8ad3f7e19e4ea7288eb3a8b58ae28e06829bbde
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
@@ -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 joel@developwithstyle.com. 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
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in sidekiq-sequence.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 12.0'
9
+ gem 'minitest', '~> 5.0'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Joel Moss
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.
@@ -0,0 +1,99 @@
1
+ # Sidekiq::Sequence - Sequential Sidekiq jobs.
2
+
3
+ Sidekiq is awesome, but it doesn't provide any support for sequential jobs, where a sequence of jobs must be run in a set order. Sidekiq::Sequence provides a simple but powerful framework to run a sequence of Sidekiq jobs.
4
+
5
+ Sidekiq::Sequence is currently only intended for use in Rails applications.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'sidekiq-sequence'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install sidekiq-sequence
22
+
23
+ Run the Rails generator to install the database migration:
24
+
25
+ $ rails g sidekiq_sequence:install
26
+ $ rails db:migrate
27
+
28
+ ## Usage
29
+
30
+ Start with a Sequence class which inherits from `Sidekiq::Sequence::Base`, and define each step of the sequence:
31
+
32
+ ```ruby
33
+ class ContactFormSequence < Sidekiq::Sequence::Base
34
+ step CreateMessage
35
+ step AssignMessage
36
+ end
37
+ ```
38
+
39
+ Each step should be a class that includes `Sidekiq::Sequence::Worker`, and behaves just like a regular Sidekiq Worker:
40
+
41
+ ```ruby
42
+ class ContactFormSequence::CreateMessage
43
+ include Sidekiq::Sequence::Worker
44
+
45
+ def perform
46
+ # Perform your job
47
+ end
48
+ end
49
+ ```
50
+
51
+ Each Step is run in the order they are defined, and each is a Sidekiq worker. If a worker fails, subsequent steps will not be run, and the worker will be placed in the Sidekiq retry queue. Once it succeeds, the next stepo will be run, and so on.
52
+
53
+ Start a Sequence by simply initializing the Sequence class you created:
54
+
55
+ ```ruby
56
+ ContactFormSequence.new name: 'Joel', github: 'joelmoss'
57
+ ```
58
+
59
+ You can pass named arguments, and these will be persisted and available in the Sequence and its workers:
60
+
61
+ ```ruby
62
+ class ContactFormSequence::AssignMessage
63
+ include Sidekiq::Sequence::Worker
64
+
65
+ def perform
66
+ @data # -> { name: 'Joel', github: 'joelmoss' }
67
+ end
68
+ end
69
+ ```
70
+
71
+ You can also modify Sequence `data` in any worker, which is great for passing data to subsequent steps:
72
+
73
+ ```ruby
74
+ class ContactFormSequence::CreateMessage
75
+ include Sidekiq::Sequence::Worker
76
+
77
+ def perform
78
+ @data[:message] = 'my message' # @data is now { name: 'Joel', github: 'joelmoss', message: 'my message' }
79
+ end
80
+ end
81
+ ```
82
+
83
+ ## Development
84
+
85
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
86
+
87
+ 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).
88
+
89
+ ## Contributing
90
+
91
+ Bug reports and pull requests are welcome on GitHub at https://github.com/joelmoss/sidekiq-sequence. 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/joelmoss/sidekiq-sequence/blob/master/CODE_OF_CONDUCT.md).
92
+
93
+ ## License
94
+
95
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
96
+
97
+ ## Code of Conduct
98
+
99
+ Everyone interacting in the Sidekiq::Sequence project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/joelmoss/sidekiq-sequence/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Sidekiq::Sequence::Record < ApplicationRecord
4
+ def self.table_name_prefix
5
+ 'sidekiq_sequence_'
6
+ end
7
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sidekiq/sequence"
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__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators/active_record'
4
+
5
+ module SidekiqSequence
6
+ module Generators
7
+ class InstallGenerator < Rails::Generators::Base
8
+ include ActiveRecord::Generators::Migration
9
+
10
+ source_root File.join(__dir__, 'templates')
11
+
12
+ desc 'Install Sidekiq::Sequence, including a database migration.'
13
+
14
+ def copy_migration
15
+ migration_template 'migration.rb', 'db/migrate/create_sidekiq_sequence_record.rb',
16
+ migration_version: migration_version
17
+ end
18
+
19
+ def migration_version
20
+ "[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,9 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ create_table :sidekiq_sequence_records do |t|
4
+ t.jsonb :data, default: {}
5
+ t.integer :current_step, default: 0
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sidekiq/sequence/version'
4
+ require 'sidekiq/sequence/engine'
5
+ require 'sidekiq/sequence/base'
6
+ require 'sidekiq/sequence/worker'
7
+
8
+ module Sidekiq
9
+ module Sequence
10
+ end
11
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sidekiq
4
+ module Sequence
5
+ # rubocop:disable Style/ClassVars
6
+ class Base
7
+ @@steps = []
8
+
9
+ def self.step(worker_class)
10
+ @@steps << worker_class
11
+ end
12
+
13
+ def self.perform_step(index, id)
14
+ if index >= @@steps.size
15
+ # No more steps in the sequence, so delete the record.
16
+ Record.destroy id
17
+ else
18
+ @@steps[index].perform_async id
19
+ end
20
+ end
21
+
22
+ def initialize(data = {})
23
+ record = Record.create(data: data)
24
+
25
+ # Start first job in the sequence.
26
+ @@steps.first.perform_async record.id
27
+ end
28
+ end
29
+ # rubocop:enable Style/ClassVars
30
+ end
31
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sidekiq
4
+ module Sequence
5
+ class Engine < ::Rails::Engine
6
+ isolate_namespace Sidekiq::Sequence
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sidekiq
4
+ module Sequence
5
+ module Perform
6
+ def perform(id, *args)
7
+ @record = Record.find(id)
8
+ @data = @record.data.with_indifferent_access
9
+
10
+ super(*args)
11
+
12
+ @record.increment(:current_step).save!
13
+
14
+ # Perform the next in sequence.
15
+ self.class.parent.perform_step @record.current_step, @record.id
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sidekiq
4
+ module Sequence
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sidekiq/sequence/perform'
4
+
5
+ module Sidekiq
6
+ module Sequence
7
+ module Worker
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ include Sidekiq::Worker
12
+ prepend Perform
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/sidekiq/sequence/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'sidekiq-sequence'
7
+ spec.version = Sidekiq::Sequence::VERSION
8
+ spec.authors = ['Joel Moss']
9
+ spec.email = ['joel@developwithstyle.com']
10
+
11
+ spec.summary = 'Sequential Sidekiq jobs'
12
+ spec.description = 'Create and manage sequential jobs in Sidekiq'
13
+ spec.homepage = 'https://github.com/joelmoss/sidekiq-sequence'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = spec.homepage
19
+ spec.metadata['changelog_uri'] = "#{spec.homepage}/releases"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.require_paths = ['lib']
27
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-sequence
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joel Moss
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-06-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Create and manage sequential jobs in Sidekiq
14
+ email:
15
+ - joel@developwithstyle.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".travis.yml"
22
+ - CODE_OF_CONDUCT.md
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - app/models/sidekiq/sequence/record.rb
28
+ - bin/console
29
+ - bin/setup
30
+ - lib/generators/sidekiq_sequence/install_generator.rb
31
+ - lib/generators/sidekiq_sequence/templates/migration.rb.tt
32
+ - lib/sidekiq/sequence.rb
33
+ - lib/sidekiq/sequence/base.rb
34
+ - lib/sidekiq/sequence/engine.rb
35
+ - lib/sidekiq/sequence/perform.rb
36
+ - lib/sidekiq/sequence/version.rb
37
+ - lib/sidekiq/sequence/worker.rb
38
+ - sidekiq-sequence.gemspec
39
+ homepage: https://github.com/joelmoss/sidekiq-sequence
40
+ licenses:
41
+ - MIT
42
+ metadata:
43
+ homepage_uri: https://github.com/joelmoss/sidekiq-sequence
44
+ source_code_uri: https://github.com/joelmoss/sidekiq-sequence
45
+ changelog_uri: https://github.com/joelmoss/sidekiq-sequence/releases
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 2.3.0
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 3.1.2
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Sequential Sidekiq jobs
65
+ test_files: []