delayed_job_unique 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: e6e42c56a5be6f3253194021d173b2eb33375c6aea688af9654df4f4a52356a6
4
+ data.tar.gz: f88071747f8751079a83013dafeced59ea05afc84fa10bff9ce4fb4106893123
5
+ SHA512:
6
+ metadata.gz: 1248f523d05a4208eab06f974845055dd7895593069a6967c6ef10f42306e778e44589608c0428f3b4c0707369be847d04b83f8d9d1c6a9ff4e91c4e64ec4790
7
+ data.tar.gz: 6898a0f19a650239ac7f7f55219155f228b5f0b3efae99427dd54af68ada1b5b53b29944bb6841b3727474344905c7e0030b22ebc2defa812935269c131adc10
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 1.17.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in delayed_job_unique.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 SINAPTIA
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,64 @@
1
+ # Delayed Job Unique
2
+
3
+ Delayed Job Unique is a Ruby gem that provides an extension to `delayed_job_active_record` for handling unique jobs in the queue.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ $ bundle add delayed_job_unique
9
+ ```
10
+ Or add the following line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'delayed_job_unique'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ ```bash
19
+ $ bundle install
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ The gem provides a `Delayed::Job::enqueue_once` class method which will use the `unique_key` of your job to check for other **not failed** jobs enqueued under the same key.
25
+ This provides a simple yet flexible mechanism to finely control the concurrency on each Job class.
26
+
27
+ If the job was already enqueued it will return `false`, otherwise it will return the enqueued job instance.
28
+
29
+ ## Example
30
+
31
+ ```ruby
32
+ class MyUniqueJob
33
+ def perform
34
+ # Your job logic here
35
+ end
36
+
37
+ def unique_key
38
+ # Return a unique key for the job (String)
39
+ end
40
+ end
41
+
42
+ # Enqueue the job only if it's not already in the queue
43
+ if Delayed::Job.enqueue_once(MyUniqueJob.new)
44
+ # job enqueued
45
+ else
46
+ # job was already enqueued
47
+ end
48
+ ```
49
+
50
+ ## Development
51
+
52
+ ```bash
53
+ git clone https://github.com/sinaptia/delayed_job_unique.git
54
+ cd delayed_job_uniq
55
+ bundle install
56
+ ```
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it
61
+ 2. Create your feature branch (`git checkout -b feature/my-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin feature/my-feature`)
64
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "delayed_job_unique"
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__)
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
@@ -0,0 +1,41 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "delayed_job_unique/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "delayed_job_unique"
8
+ spec.version = DelayedJobUnique::VERSION
9
+ spec.authors = ["Esteban Debole"]
10
+ spec.email = ["estebandebole@gmail.com"]
11
+
12
+ spec.summary = "Delayed Job Unique"
13
+ spec.description = "Enqueue just one job with the same key"
14
+ spec.homepage = "https://sinaptia.dev"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
21
+
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ spec.metadata["source_code_uri"] = "https://github.com/sinaptia/delayed_job_unique"
24
+ spec.metadata["changelog_uri"] = "https://github.com/sinaptia/delayed_job_unique"
25
+ else
26
+ raise "RubyGems 2.0 or newer is required to protect against " \
27
+ "public gem pushes."
28
+ end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/|\.gem$}) }
34
+ end
35
+ spec.bindir = "exe"
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ["lib"]
38
+
39
+ spec.add_dependency "delayed_job_active_record", "~> 4.1"
40
+ spec.add_development_dependency "rspec", "~> 3.0"
41
+ end
@@ -0,0 +1,7 @@
1
+ require "delayed_job_unique/unique_job"
2
+
3
+ module Delayed
4
+ class Job < ActiveRecord::Base
5
+ extend Delayed::UniqueJob
6
+ end
7
+ end
@@ -0,0 +1,26 @@
1
+ require "delayed_job_active_record"
2
+
3
+ module Delayed
4
+ module UniqueJob
5
+ def enqueue_once(*args)
6
+ job = args.first
7
+ raise "Job `#{job.class}` must respond to `#unique_key`" unless job.respond_to?(:unique_key)
8
+ Delayed::Job.transaction do
9
+ if job_already_enqueued?(job)
10
+ Delayed::Worker.logger.error "Job with key: #{job.unique_key} it's already on queue"
11
+ false
12
+ else
13
+ Delayed::Job.enqueue(job, key: job.unique_key)
14
+ end
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def job_already_enqueued?(job)
21
+ # failed_at not nil means DJ doesnt retry the job again
22
+ # https://github.com/collectiveidea/delayed_job_active_record/blob/c7e7ceb10b7a50cf4de4e485fdef4581ad2c37e1/lib/delayed/backend/active_record.rb#L57
23
+ Delayed::Job.where(failed_at: nil, key: job.unique_key).exists?
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module DelayedJobUnique
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "delayed_job_unique/version"
2
+ require "delayed_job_unique/unique_job"
3
+
4
+ ActiveSupport.on_load(:active_record) do
5
+ require "delayed_job_unique/active_record_extension"
6
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delayed_job_unique
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Esteban Debole
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-11-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: delayed_job_active_record
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ description: Enqueue just one job with the same key
42
+ email:
43
+ - estebandebole@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".travis.yml"
51
+ - Gemfile
52
+ - LICENSE
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - delayed_job_unique.gemspec
58
+ - lib/delayed_job_unique.rb
59
+ - lib/delayed_job_unique/active_record_extension.rb
60
+ - lib/delayed_job_unique/unique_job.rb
61
+ - lib/delayed_job_unique/version.rb
62
+ homepage: https://sinaptia.dev
63
+ licenses:
64
+ - MIT
65
+ metadata:
66
+ allowed_push_host: https://rubygems.org/
67
+ homepage_uri: https://sinaptia.dev
68
+ source_code_uri: https://github.com/sinaptia/delayed_job_unique
69
+ changelog_uri: https://github.com/sinaptia/delayed_job_unique
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.4.22
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Delayed Job Unique
89
+ test_files: []