dj_one 0.1.2

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
+ SHA1:
3
+ metadata.gz: a2c77556695fbf526e0b60a7bf999409a03c7ac1
4
+ data.tar.gz: ff1983338f47733296e7393264e8a85baa94b3e3
5
+ SHA512:
6
+ metadata.gz: cdcaec857e72e5fbc96e51474eb16ba4e7f7794046e08b62a2cf3f89bd688d1392286d55ffa6b4610a495932e7e5fe53dbec00cea78c02a6ec282cdc4462ef0d
7
+ data.tar.gz: '0509c74f032e8fe3bf220a6cb86a82fe65f3232f985caf8a7baa41e32d21c47a0a993a6834556b74cf590c6c21fa6528610cab9cfd581636f4a36fdcd5dec684'
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at piotrj@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dj_one.gemspec
4
+ gemspec
5
+
6
+ gem "sqlite3"
7
+ gem 'pry'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Piotr Jakubowski
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # DjOne
2
+
3
+ [![Build Status](https://travis-ci.org/piotrj/dj_one.svg?branch=master)](https://travis-ci.org/piotrj/dj_one)
4
+
5
+ ## What is DjOne
6
+
7
+ DjOne ensures that you don't have duplicate jobs scheduled or running. When you schedule a job as a reaction to some event you may end up with multiple jobs that will be basically doing the same thing.
8
+ Moreover, if you have more than 1 worker you may end up with more than 1 exactly same job being processed at a time. This means you may need to ensure that those jobs don't corrupt each others computations.
9
+ With DjOne you can ensure that only 1 job is scheduled and processed so you don't need to do synchronization and your workers don't waste time on doing the same thing multiple times.
10
+
11
+ ### Example
12
+
13
+ Let's say that your application is integrated with Google Calendar. And you listen on push notifications from Google that will let you know that something has changed.
14
+ When you get the notification you schedule a Delayed::Job that will fetch all new calendar events for the calendar you got notification for.
15
+
16
+ Now let's consider following scenario:
17
+ 1. You receive a push notification for calendar calendarA.
18
+ 2. You schedule a job to fetch everything new for this calendar. (Let's call it JobA1)
19
+ 3. The job does not start yet cause workers are processing other jobs.
20
+ 4. Something else changed in calendar calendarA. You get another notification.
21
+ 5. At this point you would normally schedule another job. But do you really need to? You already have one job for that calendar that is still pending. So we are using DjOne's functionality and we don't schedule another job.
22
+ 6. Worker picks up the job for calendarA
23
+ 7. New change in calendarA. You get another push for that calendar.
24
+ 8. So now should you schedule a job? In step #5 we didn't want to schedule new one. So probably we don't want to schedule another one now as well, right? All in all we have a job running for that calendar.
25
+ Well, so here is the thing. You don't know at which point of operation JobA1 is. It may have just started, but it may have already finished fetching the events. And if you don't schedule new one you won't have the newest data.
26
+ 9. Ok so we schedule new job for that calendar, let's call it JobA2.
27
+ 10. Worker is about to pick up JobA2. But, we already have a job for that calendar running. If we start another one maybe they would start interfering on each other's data. So maybe let's not start JobA2 yet. Let's let JobA1 finish.
28
+ 11. So using DjOne's functionality we are moving JobA2 start time to be a little bit in the future.
29
+ 12. JobA1 gets finished
30
+ 13. JobA2 gets picked up by worker again. This time it can run cause there is no other conflicting job.
31
+ 14. JobA2 finished. We get the freshest data for calendarA.
32
+
33
+ ## Installation
34
+
35
+ 1. Add `dj_one` to your Gemfile.
36
+
37
+ ```ruby
38
+ gem 'dj_one'
39
+ ```
40
+
41
+ 2. Run `bundle install`.
42
+ 3. Generate a migration that will add a column and unique index that will help us ensure uniqueness of jobs.
43
+ 4. Run `rake db:migrate`
44
+ 5. Add DjOne as a plugin in your DelayedJob initializer
45
+ ```ruby
46
+ # config/initializers/delayed_job_config.rb (or other file where you initialize DelayedJob)
47
+
48
+ Delayed::Worker.plugins << DjOne::Plugin
49
+
50
+ ```
51
+
52
+ 6. You are all set.
53
+
54
+
55
+ ## Usage
56
+
57
+ Now that we have the gem installed let's add this functionality to our job.
58
+
59
+ ```ruby
60
+ FetchCalendarEventsJob = Struct.new(:calendar_name)
61
+ # This is the id that we will try to set when enqueueing the job.
62
+ def enqueue_id
63
+ "fetch_calendar_events_enqueued_#{calendar_name}"
64
+ end
65
+
66
+ # This is the id that we will set when we start performing given job
67
+ def perform_id
68
+ "fetch_calendar_events_processing_#{calendar_name}"
69
+ end
70
+
71
+ # Used when the worker wants to start the job, but there is already one being performed at the moment.
72
+ # At such case we need to reschedule the job to some point in the future.
73
+ # This method defines how far in the future.
74
+ # If not defined it will use the default 30 seconds.
75
+ def duplicate_delay
76
+ # Here if we try to run the job but we are already currently fetching events for given
77
+ # calendar then we want to try running this job again in 5 minutes.
78
+ 5.minutes
79
+ end
80
+ end
81
+ ```
82
+
83
+ If you add those 2 methos then you will be sure that only 1 job for given calendar_name would be enqueued and processed at any given time.
84
+ If you don't care how many same jobs are enqueued then you don't need to specify `enqueue_id` method.
85
+ If you don't care how many same jobs are processed at a time then you don't need to specify `perform_id` method.
86
+ If you don't specify any methods it will just work without any restrictions.
87
+
88
+ ## License
89
+ The gem is available as open source under the terms of the MIT License.
90
+
data/Rakefile ADDED
@@ -0,0 +1,17 @@
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
+ namespace :test do
11
+ namespace :db do
12
+ task :prepare do
13
+ end
14
+ end
15
+ end
16
+
17
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dj_one"
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,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
data/dj_one.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dj_one/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dj_one"
8
+ spec.version = DjOne::VERSION
9
+ spec.authors = ["Piotr Jakubowski"]
10
+ spec.email = ["piotrj@gmail.com"]
11
+
12
+ spec.summary = "DjOne ensures uniqueness of your DelayedJobs"
13
+ spec.description = "With DjOne you can get rid of duplicate schedules of particular DelayedJob so that your workers don't have to do the same work twice and you don't need to ensure synchronization between those jobs"
14
+ spec.homepage = "https://github.com/piotrj/dj_one"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.12"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.0"
25
+ spec.add_dependency 'delayed_job_active_record', "~> 4.1"
26
+ spec.add_dependency 'railties', '>= 4.0'
27
+ end
@@ -0,0 +1,13 @@
1
+ require "rails/generators"
2
+ require "rails/generators/active_record"
3
+
4
+ module DelayedJob
5
+ class AddDjOne < Rails::Generators::Base
6
+ include ActiveRecord::Generators::Migration
7
+ source_root File.expand_path("../templates", __FILE__)
8
+
9
+ def add_migration
10
+ migration_template "install_dj_one_migration.rb", "db/migrate/install_dj_one.rb"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ superclass = if ActiveRecord.version >= Gem::Version.new(5)
2
+ ActiveRecord::Migration[5.0]
3
+ else
4
+ ActiveRecord::Migration
5
+ end
6
+
7
+
8
+ class InstallDjOne < superclass
9
+ def self.up
10
+ add_column :delayed_jobs, :unique_id, :string
11
+ add_index :delayed_jobs, [:unique_id], name: :unique_delayed_jobs, unique: true
12
+ end
13
+
14
+ def self.down
15
+ remove_column :delayed_jobs, :unique_id
16
+ remove_index :delayed_jobs, name: :unique_delayed_jobs
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module DjOne
2
+ VERSION = "0.1.2"
3
+ end
data/lib/dj_one.rb ADDED
@@ -0,0 +1,59 @@
1
+ require "dj_one/version"
2
+
3
+ require "active_record"
4
+ require "delayed_job"
5
+ require "delayed_job_active_record"
6
+
7
+ require "dj_one/generator"
8
+
9
+ module DjOne
10
+ DEFAULT_DUPLICATE_DELAY = 30.seconds
11
+
12
+ class Plugin < Delayed::Plugin
13
+ callbacks do |lifecycle|
14
+ Handler.new(lifecycle)
15
+ end
16
+
17
+ class Handler
18
+ def initialize(lifecycle)
19
+ lifecycle.around(:enqueue, &method(:enqueue))
20
+ lifecycle.around(:perform, &method(:perform))
21
+ lifecycle.around(:failure, &method(:failure))
22
+ end
23
+
24
+ def enqueue(job, &proceed)
25
+ job.unique_id = get_attribute(job, :enqueue_id)
26
+ proceed.call
27
+ rescue ActiveRecord::RecordNotUnique
28
+ end
29
+
30
+ def perform(worker, job, &proceed)
31
+ job.unique_id = get_attribute(job, :perform_id)
32
+ job.save! if job.changed?
33
+
34
+ proceed.call
35
+ rescue ActiveRecord::RecordNotUnique
36
+ job.unique_id = job.unique_id_was
37
+ job.run_at = calculate_run_at(job)
38
+ job.save! if job.changed?
39
+ end
40
+
41
+ def failure(worker, job, &proceed)
42
+ job.unique_id = nil
43
+ proceed.call
44
+ end
45
+
46
+ def get_attribute(job, method_name)
47
+ object = job.payload_object
48
+ object.respond_to?(method_name) ? object.public_send(method_name) : nil
49
+ rescue Delayed::DeserializationError
50
+ nil
51
+ end
52
+
53
+ def calculate_run_at(job)
54
+ delay = get_attribute(job, :duplicate_delay) || DEFAULT_DUPLICATE_DELAY
55
+ Time.now + delay
56
+ end
57
+ end
58
+ end
59
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dj_one
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Piotr Jakubowski
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-02 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: delayed_job_active_record
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.1'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: railties
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '4.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '4.0'
83
+ description: With DjOne you can get rid of duplicate schedules of particular DelayedJob
84
+ so that your workers don't have to do the same work twice and you don't need to
85
+ ensure synchronization between those jobs
86
+ email:
87
+ - piotrj@gmail.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".travis.yml"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - dj_one.gemspec
102
+ - lib/dj_one.rb
103
+ - lib/dj_one/generator.rb
104
+ - lib/dj_one/templates/install_dj_one_migration.rb
105
+ - lib/dj_one/version.rb
106
+ homepage: https://github.com/piotrj/dj_one
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.6.8
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: DjOne ensures uniqueness of your DelayedJobs
130
+ test_files: []