delayed_job_prevent_duplicate 0.1.0 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0239b2cca535272939571db36a7694c86008c0fc57dd1fd946c10c35b30f9160'
4
- data.tar.gz: f69616afd47037e85e3f248f9f4a398a00c99b91e3f484c547de82f1782de933
3
+ metadata.gz: f8668bbcfafe20c2670059fcb0ff0a53d6d75ce1987fd0ce5246bed70660b8ef
4
+ data.tar.gz: 39369122bb75be1eaa05f6a21ac4ecc557be61c39cdf03d86c7b1b10dc1f925c
5
5
  SHA512:
6
- metadata.gz: d0a0587190b5e6c93f51c8bbb4e31424173ebe8b31e33dfcbc070bda3bcc9daf557b0c39b7d9434e7d24fd4c8a0e68dda036f0113174b83b4f0e3ffc0b214188
7
- data.tar.gz: abb1515c6a31575ddea9b6e1b1f87ef386d0f5768f743a2c5d5218e563cef630522d9390ed7705c8bea5d9afa603f04128934fdc84ece66ec1c0b42f8d8f198c
6
+ metadata.gz: 0a3865e266d9d65a17194d486aa550ab378aa2a659e2419008fb3168e7928e125b1ea6d07e8edae311da6b252c10d1a35b794caf612abcb777aa5398b2d5876b
7
+ data.tar.gz: d1359a9ac3416fb4aa628d2cb2b923508911951ccd2eda52cd5821127c060b49ad6f8b168d2f204c15eebc0995fd3ae510945050dcac31a528343d1a8f599895
data/CHANGELOG.md CHANGED
@@ -1,5 +1,3 @@
1
- ## [Unreleased]
2
-
3
1
  ## [0.1.0] - 2023-09-07
4
2
 
5
3
  - Initial release
data/README.md CHANGED
@@ -6,7 +6,7 @@ And then when creating a new job we look in the "pending" jobs if there is anoth
6
6
 
7
7
  ## Note
8
8
 
9
- This gem is based based on the [synth](https://github.com/synth) work: https://gist.github.com/synth/fba7baeffd083a931184
9
+ This gem is based on the [synth](https://github.com/synth) work: https://gist.github.com/synth/fba7baeffd083a931184
10
10
 
11
11
  ## Installation
12
12
 
@@ -43,4 +43,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
43
43
 
44
44
  https://groups.google.com/g/delayed_job/c/gZ9bFCdZrsk#2a05c39a192e630c
45
45
  https://github.com/collectiveidea/delayed_job/blob/master/lib/delayed/backend/base.rb
46
- https://github.com/ignatiusreza/activejob-trackable
46
+ https://github.com/ignatiusreza/activejob-trackable
@@ -15,27 +15,60 @@ class DelayedDuplicatePreventionPlugin < Delayed::Plugin
15
15
  private
16
16
 
17
17
  def add_signature
18
- self.signature = generate_signature
19
- self.args = self.payload_object.args
18
+ # If signature fails, id will keep everything working (though deduplication will not work)
19
+ self.signature = generate_signature || generate_signature_random
20
+ self.args = get_args
20
21
  end
21
22
 
22
23
  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}"
24
+ begin
25
+ if payload_object.is_a?(Delayed::PerformableMethod)
26
+ generate_signature_for_performable_method
27
+ else
28
+ generate_signature_random
29
+ end
30
+ rescue
31
+ generate_signature_failed
32
+ end
33
+ end
34
+
35
+ # Methods tagged with handle_asynchronously
36
+ def generate_signature_for_performable_method
37
+ if payload_object.object.respond_to?(:id) and payload_object.object.id.present?
38
+ sig = "#{payload_object.object.class}:#{payload_object.object.id}"
27
39
  else
28
- sig = "#{pobj.object}"
40
+ sig = "#{payload_object.object}"
29
41
  end
42
+ sig += "##{payload_object.method_name}"
43
+ sig
44
+ end
30
45
 
31
- sig += "##{pobj.method_name}"
32
- return sig
33
- end
46
+ # # Regular Job
47
+ # def generate_signature_for_job_wrapper
48
+ # sig = "#{payload_object.job_data["job_class"]}"
49
+ # payload_object.job_data["arguments"].each do |job_arg|
50
+ # string_job_arg = job_arg.is_a?(String) ? job_arg : job_arg.to_json
51
+ # end
52
+ # sig += "#{payload_object.job_data["job_class"]}"
53
+ # sig
54
+ # end
55
+
56
+ def generate_signature_random
57
+ SecureRandom.uuid
58
+ end
59
+
60
+ def generate_signature_failed
61
+ puts "DelayedDuplicatePreventionPlugin could not generate the signature correctly."
62
+ end
63
+
64
+ def get_args
65
+ self.payload_object.respond_to?(:args) ? self.payload_object.args : []
66
+ end
34
67
 
35
68
  def prevent_duplicate
36
69
  if DuplicateChecker.duplicate?(self)
37
70
  Rails.logger.warn "Found duplicate job(#{self.signature}), ignoring..."
38
- errors.add(:base, "This is a duplicate")
71
+ errors.add(:base, "This is a duplicate")
39
72
  end
40
73
  end
41
74
  end
@@ -61,16 +94,14 @@ class DelayedDuplicatePreventionPlugin < Delayed::Plugin
61
94
  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
95
  .where(signature: job.signature) # Same signature
63
96
  possible_dupes = possible_dupes.where.not(id: job.id) if job.id.present?
97
+ byebug
64
98
  possible_dupes
65
99
  end
66
100
 
67
101
  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
102
+ job1.payload_object.args == job2.payload_object.args
103
+ rescue
104
+ false
74
105
  end
75
106
  end
76
107
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DelayedJobPreventDuplicate
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delayed_job_prevent_duplicate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - pabois
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-09-07 00:00:00.000000000 Z
11
+ date: 2023-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: delayed_job
@@ -38,7 +38,6 @@ executables: []
38
38
  extensions: []
39
39
  extra_rdoc_files: []
40
40
  files:
41
- - ".DS_Store"
42
41
  - CHANGELOG.md
43
42
  - Gemfile
44
43
  - LICENSE.txt
@@ -60,7 +59,7 @@ metadata:
60
59
  homepage_uri: https://github.com/noesya/delayed_job_prevent_duplicate
61
60
  source_code_uri: https://github.com/noesya/delayed_job_prevent_duplicate
62
61
  changelog_uri: https://github.com/noesya/delayed_job_prevent_duplicate/CHANGELOG.md
63
- post_install_message:
62
+ post_install_message:
64
63
  rdoc_options: []
65
64
  require_paths:
66
65
  - lib
@@ -75,8 +74,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
74
  - !ruby/object:Gem::Version
76
75
  version: '0'
77
76
  requirements: []
78
- rubygems_version: 3.1.4
79
- signing_key:
77
+ rubygems_version: 3.4.13
78
+ signing_key:
80
79
  specification_version: 4
81
80
  summary: Prevent delayed_job to enqueue a task already enqueued
82
81
  test_files: []
data/.DS_Store DELETED
Binary file