delayed_job_prevent_duplicate 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
+ SHA256:
3
+ metadata.gz: '0239b2cca535272939571db36a7694c86008c0fc57dd1fd946c10c35b30f9160'
4
+ data.tar.gz: f69616afd47037e85e3f248f9f4a398a00c99b91e3f484c547de82f1782de933
5
+ SHA512:
6
+ metadata.gz: d0a0587190b5e6c93f51c8bbb4e31424173ebe8b31e33dfcbc070bda3bcc9daf557b0c39b7d9434e7d24fd4c8a0e68dda036f0113174b83b4f0e3ffc0b214188
7
+ data.tar.gz: abb1515c6a31575ddea9b6e1b1f87ef386d0f5768f743a2c5d5218e563cef630522d9390ed7705c8bea5d9afa603f04128934fdc84ece66ec1c0b42f8d8f198c
data/.DS_Store ADDED
Binary file
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-09-07
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in delayed_job_prevent_duplicate.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 noesya
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,46 @@
1
+ # DelayedJobPreventDuplicate
2
+
3
+ The purpose of this gem is to prevent to re-enqueue on DelayedJob a task already enqueued.
4
+ So we set a "signature" attached to every task enqueued which is a composite from the class and the id of the object, and the method called.
5
+ And then when creating a new job we look in the "pending" jobs if there is another one with the same signature (not in the "working" one because a task can be executed and yet you want to re-excute it because of any change).
6
+
7
+ ## Note
8
+
9
+ This gem is based based on the [synth](https://github.com/synth) work: https://gist.github.com/synth/fba7baeffd083a931184
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'delayed_job_prevent_duplicate'
17
+ ```
18
+
19
+ This line should be added after including the gem 'delayed_job'
20
+
21
+ And then execute:
22
+
23
+ $ bundle install
24
+
25
+ Next, you need to run the generator:
26
+
27
+ ```ruby
28
+ rails g delayed_job_prevent_duplicate
29
+ ```
30
+
31
+ All should be fine now!
32
+
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/noesya/delayed_job_prevent_duplicate.
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
41
+
42
+ ## Bibliography
43
+
44
+ https://groups.google.com/g/delayed_job/c/gZ9bFCdZrsk#2a05c39a192e630c
45
+ https://github.com/collectiveidea/delayed_job/blob/master/lib/delayed/backend/base.rb
46
+ https://github.com/ignatiusreza/activejob-trackable
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "delayed_job_prevent_duplicate"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ 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,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/delayed_job_prevent_duplicate/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "delayed_job_prevent_duplicate"
7
+ spec.version = DelayedJobPreventDuplicate::VERSION
8
+ spec.authors = ["pabois"]
9
+ spec.email = ["pierreandre.boissinot@noesya.coop"]
10
+
11
+ spec.summary = "Prevent delayed_job to enqueue a task already enqueued"
12
+ spec.description = "Improve Delayed Job to prevent a task already enqueued to be enqueue a second time."
13
+ spec.homepage = "https://github.com/noesya/delayed_job_prevent_duplicate"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = "https://github.com/noesya/delayed_job_prevent_duplicate/CHANGELOG.md"
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 do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ spec.add_dependency "delayed_job", [">= 3.0", "< 5"]
34
+
35
+ # For more information and examples about making a new gem, checkout our
36
+ # guide at: https://bundler.io/guides/creating_gem.html
37
+ end
data/lib/.DS_Store ADDED
Binary file
@@ -0,0 +1,76 @@
1
+ # based on https://gist.github.com/synth/fba7baeffd083a931184
2
+
3
+ require 'delayed_job'
4
+
5
+ class DelayedDuplicatePreventionPlugin < Delayed::Plugin
6
+
7
+ module SignatureConcern
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ before_validation :add_signature
12
+ validate :prevent_duplicate
13
+ end
14
+
15
+ private
16
+
17
+ def add_signature
18
+ self.signature = generate_signature
19
+ self.args = self.payload_object.args
20
+ end
21
+
22
+ def generate_signature
23
+ pobj = payload_object
24
+ if pobj.object.respond_to?(:id) and pobj.object.id.present?
25
+ sig = "#{pobj.object.class}"
26
+ sig += ":#{pobj.object.id}"
27
+ else
28
+ sig = "#{pobj.object}"
29
+ end
30
+
31
+ sig += "##{pobj.method_name}"
32
+ return sig
33
+ end
34
+
35
+ def prevent_duplicate
36
+ if DuplicateChecker.duplicate?(self)
37
+ Rails.logger.warn "Found duplicate job(#{self.signature}), ignoring..."
38
+ errors.add(:base, "This is a duplicate")
39
+ end
40
+ end
41
+ end
42
+
43
+ class DuplicateChecker
44
+ attr_reader :job
45
+
46
+ def self.duplicate?(job)
47
+ new(job).duplicate?
48
+ end
49
+
50
+ def initialize(job)
51
+ @job = job
52
+ end
53
+
54
+ def duplicate?
55
+ possible_dupes.any? { |possible_dupe| args_match?(possible_dupe, job) }
56
+ end
57
+
58
+ private
59
+
60
+ def possible_dupes
61
+ possible_dupes = Delayed::Job.where(attempts: 0, locked_at: nil) # Only jobs not started, otherwise it would never compute a real change if the job is currently running
62
+ .where(signature: job.signature) # Same signature
63
+ possible_dupes = possible_dupes.where.not(id: job.id) if job.id.present?
64
+ possible_dupes
65
+ end
66
+
67
+ def args_match?(job1, job2)
68
+ # TODO: make this logic robust
69
+ normalize_args(job1.args) == normalize_args(job2.args)
70
+ end
71
+
72
+ def normalize_args(args)
73
+ args.kind_of?(String) ? YAML.load(args) : args
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DelayedJobPreventDuplicate
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "delayed_job_prevent_duplicate/version"
4
+
5
+ module DelayedJobPreventDuplicate
6
+ class Error < StandardError; end
7
+
8
+ require 'delayed_duplicate_prevention_plugin'
9
+
10
+ Delayed::Backend::ActiveRecord::Job.send(:include, DelayedDuplicatePreventionPlugin::SignatureConcern)
11
+ Delayed::Worker.plugins << DelayedDuplicatePreventionPlugin
12
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails'
4
+
5
+ module DelayedJobPreventDuplicate
6
+ class DelayedJobPreventDuplicateGenerator < ::Rails::Generators::Base
7
+
8
+ include Rails::Generators::Migration
9
+ source_root File.expand_path("../templates", __FILE__)
10
+
11
+ def self.next_migration_number(path)
12
+ unless @prev_migration_nr
13
+ @prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
14
+ else
15
+ @prev_migration_nr += 1
16
+ end
17
+ @prev_migration_nr.to_s
18
+ end
19
+
20
+ def copy_migration
21
+ migration_template "migration.rb", "db/migrate/add_signature_to_delayed_job.rb", migration_version: migration_version
22
+ end
23
+
24
+ def migration_version
25
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddSignatureToDelayedJob < ActiveRecord::Migration<%= migration_version %>
4
+ def change
5
+ add_column :delayed_jobs, :signature, :string
6
+ add_column :delayed_jobs, :args, :text
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delayed_job_prevent_duplicate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - pabois
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-09-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: delayed_job
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5'
33
+ description: Improve Delayed Job to prevent a task already enqueued to be enqueue
34
+ a second time.
35
+ email:
36
+ - pierreandre.boissinot@noesya.coop
37
+ executables: []
38
+ extensions: []
39
+ extra_rdoc_files: []
40
+ files:
41
+ - ".DS_Store"
42
+ - CHANGELOG.md
43
+ - Gemfile
44
+ - LICENSE.txt
45
+ - README.md
46
+ - Rakefile
47
+ - bin/console
48
+ - bin/setup
49
+ - delayed_job_prevent_duplicate.gemspec
50
+ - lib/.DS_Store
51
+ - lib/delayed_duplicate_prevention_plugin.rb
52
+ - lib/delayed_job_prevent_duplicate.rb
53
+ - lib/delayed_job_prevent_duplicate/version.rb
54
+ - lib/generators/delayed_job_prevent_duplicate_generator.rb
55
+ - lib/generators/templates/migration.rb
56
+ homepage: https://github.com/noesya/delayed_job_prevent_duplicate
57
+ licenses:
58
+ - MIT
59
+ metadata:
60
+ homepage_uri: https://github.com/noesya/delayed_job_prevent_duplicate
61
+ source_code_uri: https://github.com/noesya/delayed_job_prevent_duplicate
62
+ changelog_uri: https://github.com/noesya/delayed_job_prevent_duplicate/CHANGELOG.md
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 2.6.0
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubygems_version: 3.1.4
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Prevent delayed_job to enqueue a task already enqueued
82
+ test_files: []