migratiq 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2fea372348191f76c19066d1cb8237c6a21e0e30
4
+ data.tar.gz: d9ed7d3ef0689ded84ce0d1a8089db25b183e2be
5
+ SHA512:
6
+ metadata.gz: 478f7c9582fa3fcd1cb42c8fbb3d5fab64546144cbf877af0d2f84e8ac7ac1452134488e7e6fda5fbd0ef1ab9c3006dd127990b08cfda1e918d272749fc6c4a1
7
+ data.tar.gz: fae1fc4d8287a5ba3dd196e861da69e15e39088546f03f64987dd1b9ab6750525237a0527bbdf3efdf290fe7b355e45db75473a21a91d4f57e8bbfe1e8f52446
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /*.rdb
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in migratiq.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Brody Berson
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,122 @@
1
+ # Migratiq
2
+
3
+ Migrate your scheduled sidekiq jobs when their arguements change. :tada:
4
+
5
+ After a deploy, if the number of arguements on a worker has changed
6
+ those workers tend to fail unless you delete them manually
7
+ now you can either specify a migration plan for a particular number of arguments
8
+ i.e.
9
+
10
+ ```Ruby
11
+ class HardWorker
12
+ include Migratiq
13
+
14
+ def perform(a, b)
15
+ # Do work here...
16
+ end
17
+
18
+ migrate_by(arity: 5) do |a, b, c, d, e|
19
+ [b, a]
20
+ end
21
+ end
22
+
23
+ pry> HardWorker.migrate!
24
+ ```
25
+
26
+ or remove all jobs that don't match the current interface
27
+
28
+ ```Ruby
29
+ class HardWorker
30
+ include Migratiq
31
+
32
+ def perform(a, b)
33
+ # Do work here...
34
+ end
35
+ end
36
+
37
+ pry> HardWorker.migrate!(delete: true)
38
+ ```
39
+
40
+ ## Installation
41
+
42
+ Add this line to your application's Gemfile:
43
+
44
+ ```ruby
45
+ gem 'migratiq'
46
+ ```
47
+
48
+ And then execute:
49
+
50
+ $ bundle
51
+
52
+ Or install it yourself as:
53
+
54
+ $ gem install migratiq
55
+
56
+ ## Usage
57
+
58
+ Say your current worker looks something like,
59
+
60
+ ```Ruby
61
+ class HardWorker
62
+ def perform(a, b, c, d)
63
+ # Do work here...
64
+ end
65
+ end
66
+ ```
67
+
68
+ and you find that you no longer need to take arguments `c` and `d`. So now your worker looks like,
69
+
70
+ ```Ruby
71
+ class HardWorker
72
+ def perform(a, b)
73
+ # Do work here...
74
+ end
75
+ end
76
+ ```
77
+
78
+ Now if you deploy this change and `HardWorker` has work that is already scheduled, **all those jobs will fail**.
79
+
80
+ if you aren't concerned about loosing that work, all you need to do is add migratiq and you can remove them all and clean up your logs
81
+
82
+ ```Ruby
83
+ class HardWorker
84
+ include 'migratiq'
85
+ def perform(a, b)
86
+ # Do work here...
87
+ end
88
+ end
89
+
90
+ pry>HardWorker.migrate!(delete: true)
91
+ ```
92
+
93
+ It will remove all the jobs that do not match the current arity of the perform function.
94
+ However, if you don't want to loose your work, you can tell Migratiq how to migrate the worker params
95
+
96
+ ```Ruby
97
+ class HardWorker
98
+ include Migratiq
99
+
100
+ def perform(a, b)
101
+
102
+ end
103
+
104
+ migrate_by(arity: 4) do |a, b, c, d|
105
+ [b, a]
106
+ end
107
+ end
108
+
109
+ pry> HardWorker.migrate!
110
+ ```
111
+
112
+ This work will be rescheduled with the new arguements. if you also want to delete the old jobs you can add the `delete: true` flag on migrate.
113
+
114
+ ## Development
115
+
116
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
117
+
118
+ 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).
119
+
120
+ ## Contributing
121
+
122
+ Bug reports and pull requests are welcome on GitHub at https://github.com/HParker/migratiq.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+
4
+ task :spec do
5
+ begin
6
+ require 'rspec/core/rake_task'
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ rescue LoadError
9
+ end
10
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "migratiq"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,36 @@
1
+ module Migratiq
2
+ class ArityChecker
3
+ def initialize(method_definition)
4
+ @method_definition = method_definition
5
+ end
6
+
7
+ def accepts?(arg_count)
8
+ allowed_range.include?(arg_count)
9
+ end
10
+
11
+ private
12
+
13
+ attr_reader :method_definition
14
+
15
+ def parameters
16
+ method_definition.parameters
17
+ end
18
+
19
+ def required
20
+ parameters.select { |param|
21
+ param[0] == :req
22
+ }.size
23
+ end
24
+
25
+ def optional
26
+ parameters.select { |param|
27
+ param[0] == :opt
28
+ }.size
29
+ end
30
+
31
+ def allowed_range
32
+ (required..required+optional)
33
+ end
34
+
35
+ end
36
+ end
data/lib/migratiq.rb ADDED
@@ -0,0 +1,59 @@
1
+ require "migratiq/version"
2
+ require "arity_checker"
3
+ require 'sidekiq/api'
4
+
5
+ module Migratiq
6
+ def self.included(base)
7
+ @classes ||= []
8
+ @classes << base
9
+ base.extend(ClassMethods)
10
+ end
11
+
12
+ def self.migrate_all!
13
+ @classes.each do |klass|
14
+ klass.migrate!
15
+ end
16
+ end
17
+
18
+ module ClassMethods
19
+ def migrate!(delete: false)
20
+ method = self.new.method(:perform)
21
+ checker = ArityChecker.new(method)
22
+ outdated_jobs = check_arity(checker, Sidekiq::ScheduledSet.new)
23
+ migrate_outdated(outdated_jobs, delete)
24
+ end
25
+
26
+ def migrate_by(arity:, &block)
27
+ migraiton_plans[self.name][arity] = block
28
+ end
29
+
30
+ private
31
+
32
+ def check_arity(checker, set)
33
+ set.select { |job|
34
+ !checker.accepts?(job['args'].size)
35
+ }
36
+ end
37
+
38
+ def migrate_outdated(outdated, delete)
39
+ outdated.each do |job|
40
+ next if Sidekiq::ScheduledSet.new.find_job(job['jid']).nil?
41
+ Sidekiq::ScheduledSet.new.find_job(job['jid']).delete if delete || plan_for(job['args'].size)
42
+ if plan_for(job['args'].size)
43
+ new_args = plan_for(job['args'].size).call(job['args'])
44
+ self.perform_in(5, *new_args)
45
+ end
46
+ end
47
+ end
48
+
49
+ def migraiton_plans
50
+ @migration_plans ||= Hash.new({})
51
+ end
52
+
53
+ def plan_for(arity)
54
+ if migraiton_plans[self.name] && migraiton_plans[self.name][arity]
55
+ migraiton_plans[self.name][arity]
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module Migratiq
2
+ VERSION = "0.1.0"
3
+ end
data/migratiq.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'migratiq/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "migratiq"
8
+ spec.version = Migratiq::VERSION
9
+ spec.authors = ["Adam Hess"]
10
+ spec.email = ["adamhess1991@gmail.com"]
11
+
12
+ spec.summary = %q{migrate sidekiq jobs when changing arguments that a worker accepts.}
13
+ # spec.description = %q{TODO: Write a longer description or delete this line.}
14
+ spec.homepage = "https://github.com/HParker/migratiq"
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.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry"
25
+ spec.add_development_dependency "sidekiq"
26
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: migratiq
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Hess
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sidekiq
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
+ description:
84
+ email:
85
+ - adamhess1991@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - LICENSE
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/setup
98
+ - lib/arity_checker.rb
99
+ - lib/migratiq.rb
100
+ - lib/migratiq/version.rb
101
+ - migratiq.gemspec
102
+ homepage: https://github.com/HParker/migratiq
103
+ licenses: []
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.2.2
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: migrate sidekiq jobs when changing arguments that a worker accepts.
125
+ test_files: []