delayed_job_loner 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -16,6 +16,15 @@ Or install it yourself as:
16
16
 
17
17
  $ gem install delayed_job_loner
18
18
 
19
+ ### Migration
20
+
21
+ The jobs table requires a migration to add the loner_hash string field.
22
+
23
+ <pre>
24
+ $ script/rails generate delayed_job_loner
25
+ $ rake db:migrate
26
+ </pre>
27
+
19
28
  ## Usage
20
29
 
21
30
  Pass the option `:unique_on` to any method that you would provide `:priority` or `:run_at`. `:unique_on` should be an array of attributes that you want to check the uniqueness of the job against.
@@ -18,7 +18,7 @@ Gem::Specification.new do |gem|
18
18
  gem.add_dependency 'delayed_job'
19
19
 
20
20
  gem.add_development_dependency 'rspec'
21
- gem.add_development_dependency 'activerecord'
21
+ gem.add_development_dependency 'rails'
22
22
  gem.add_development_dependency 'sqlite3-ruby'
23
23
  gem.add_development_dependency 'sqlite3_ar_regexp'
24
24
  gem.add_development_dependency 'delayed_job_active_record'
@@ -1,2 +1,2 @@
1
1
  require "delayed_job_loner/version"
2
- require "delayed_job_loner/init"
2
+ require "delayed_job_loner/init"
@@ -1,22 +1,33 @@
1
+ require 'digest/md5'
2
+
1
3
  module Delayed
2
4
  module Backend
3
5
  module ActiveRecord
4
6
  class Job < ::ActiveRecord::Base
7
+ attr_accessor :loner
5
8
  attr_accessor :unique_on
6
- # attr_accessible :unique_on
7
9
 
8
10
  validate :check_uniqueness
9
11
 
10
12
  def check_uniqueness
13
+ self.loner_hash = generate_loner_hash
14
+ if loner || unique_on
15
+ self.errors.add(:base, "Job already exists") unless self.class.where(loner_hash: self.loner_hash, locked_by: nil).first.nil?
16
+ else
17
+ true
18
+ end
19
+ end
20
+
21
+ def generate_loner_hash
11
22
  if unique_on
12
- scope = self.class.where("handler REGEXP '\\nmethod_name: :#{payload_object.method_name}\\n'").where(:locked_by => nil)
23
+ hashable_string = "#{payload_object.method_name}"
13
24
  unique_on.each do |attribute_name|
14
- scope = scope.where("handler REGEXP ' +#{attribute_name}: #{payload_object.send(attribute_name)}\\n'")
25
+ hashable_string += "::#{attribute_name}:#{payload_object.send(attribute_name)}"
15
26
  end
16
- self.errors.add(:base, "Job already exists") unless scope.first.nil?
17
27
  else
18
- true
28
+ hashable_string = "#{payload_object.method_name}::id:#{payload_object.id}"
19
29
  end
30
+ Digest::MD5.base64digest(hashable_string)
20
31
  end
21
32
 
22
33
  end
@@ -1,3 +1,3 @@
1
1
  module DelayedJobLoner
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,29 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class DelayedJobLonerGenerator < Rails::Generators::Base
5
+
6
+ include Rails::Generators::Migration
7
+
8
+ def self.source_root
9
+ @source_root ||= File.join(File.dirname(__FILE__), 'templates')
10
+ end
11
+
12
+ # Implement the required interface for Rails::Generators::Migration.
13
+ #
14
+ def self.next_migration_number(dirname) #:nodoc:
15
+ next_migration_number = current_migration_number(dirname) + 1
16
+ if ActiveRecord::Base.timestamped_migrations
17
+ [Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
18
+ else
19
+ "%.3d" % next_migration_number
20
+ end
21
+ end
22
+
23
+ def create_migration_file
24
+ if defined?(ActiveRecord)
25
+ migration_template 'migration.rb', 'db/migrate/add_loner_hash_column_to_delayed_jobs.rb'
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,11 @@
1
+ class AddLonerHashColumnToDelayedJobs < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :delayed_jobs, :loner_hash, :string
4
+ add_index :delayed_jobs, :loner_hash
5
+ end
6
+
7
+ def self.down
8
+ remove_index :delayed_jobs, :loner_hash
9
+ remove_column :delayed_jobs, :loner_hash
10
+ end
11
+ end
@@ -21,15 +21,28 @@ describe DelayedJobLoner do
21
21
 
22
22
  # This test is broken right now because of an issue with attr_accessor not being able to be set by mass assignment in my tests.
23
23
  # It works in real life...
24
- it "should not create a job if :unique_on is specified and a similar job exists" do
24
+ it "should not create a job if :loner is specified and a similar job exists" do
25
25
  story = Story.create(:text => "foo")
26
26
  story.reload
27
27
 
28
28
  story.whatever.persisted?.should eql true
29
29
  story.whatever.persisted?.should eql false
30
30
 
31
- story.delay(:unique_on => [:id]).tell.persisted?.should eql true
32
- story.delay(:unique_on => [:id]).tell.persisted?.should eql false
31
+ story.delay(:loner => true).tell.persisted?.should eql true
32
+ story.delay(:loner => true).tell.persisted?.should eql false
33
+ end
34
+
35
+ it "should return a unique md5 hash" do
36
+ story = Story.create(:text => "foo")
37
+ story2 = Story.create(:text => "foo")
38
+ story.reload
39
+ story2.reload
40
+
41
+ dj_object = story.whatever
42
+ dj_object2 = story2.whatever
43
+
44
+ dj_object.generate_loner_hash.should_not eql dj_object2.generate_loner_hash
45
+ dj_object.loner_hash.should eql dj_object.generate_loner_hash
33
46
  end
34
47
 
35
48
  end
@@ -1,4 +1,4 @@
1
- require 'active_record'
1
+ require 'rails'
2
2
  require 'delayed_job'
3
3
  require 'delayed_job_active_record'
4
4
  require 'delayed_job_loner'
@@ -20,7 +20,9 @@ ActiveRecord::Schema.define do
20
20
  table.datetime :locked_at
21
21
  table.string :locked_by
22
22
  table.datetime :failed_at
23
+ table.string :loner_hash
23
24
  table.timestamps
25
+
24
26
  end
25
27
 
26
28
  create_table :stories, :force => true do |table|
@@ -40,6 +42,6 @@ class Story < ActiveRecord::Base
40
42
  def whatever
41
43
  tell
42
44
  end
43
- handle_asynchronously :whatever, :unique_on => [:id], :priority => 10
45
+ handle_asynchronously :whatever, :loner => true, :priority => 10
44
46
 
45
47
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delayed_job_loner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-08 00:00:00.000000000 Z
12
+ date: 2012-09-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: delayed_job
@@ -44,7 +44,7 @@ dependencies:
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
46
  - !ruby/object:Gem::Dependency
47
- name: activerecord
47
+ name: rails
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
@@ -125,6 +125,8 @@ files:
125
125
  - lib/delayed_job_loner.rb
126
126
  - lib/delayed_job_loner/init.rb
127
127
  - lib/delayed_job_loner/version.rb
128
+ - lib/generators/delayed_job_loner/delayed_job_loner_generator.rb
129
+ - lib/generators/delayed_job_loner/templates/migration.rb
128
130
  - spec/delayed_job_loner_spec.rb
129
131
  - spec/spec_helper.rb
130
132
  - spec/story.rb