Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

delayed job with unique key, based on delayed job gem

  • Loading branch information...
commit 33d842d05f7bdb9038c1035064d77d56de4401cb 1 parent 802b7fe
@munya authored
View
32 .gitignore
@@ -1,16 +1,18 @@
+*.gem
*.rbc
-*.sassc
-.sass-cache
-capybara-*.html
-.rspec
-/.bundle
-/vendor/bundle
-/log/*
-/tmp/*
-/db/*.sqlite3
-/public/system/*
-/coverage/
-/spec/tmp/*
-**.orig
-rerun.txt
-pickle-email-*.html
+.bundle
+.config
+.idea
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
View
4 Gemfile
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in delayed_job_unique_key.gemspec
+gemspec
View
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Oleg Muntyan
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
31 README.md
@@ -1,2 +1,29 @@
-delayed_job_unique_key
-======================
+# DelayedJobUniqueKey
+
+TODO: Write a gem description
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'delayed_job_unique_key'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install delayed_job_unique_key
+
+## Usage
+
+TODO: Write usage instructions here
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
View
2  Rakefile
@@ -0,0 +1,2 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
View
20 delayed_job_unique_key.gemspec
@@ -0,0 +1,20 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/delayed_job_unique_key/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ["Oleg Muntyan"]
+ gem.email = ["omuntyan@gmail.com"]
+ gem.description = %q{Gem based on delayed job with active record and allows to prevent creating new job if already exists not running one with unique key}
+ gem.summary = %q{}
+ gem.homepage = "http://github.com/munya/delayed_job_unique_key"
+
+ #gem.files = `git ls-files`.split($\)
+ gem.files = Dir.glob('{lib}/**/*')
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.name = "delayed_job_unique_key"
+ gem.require_paths = ["lib"]
+ gem.version = DelayedJobUniqueKey::VERSION
+
+ gem.add_runtime_dependency 'delayed_job_active_record', '~>0.3.2'
+end
View
8 lib/delayed_job_unique_key.rb
@@ -0,0 +1,8 @@
+require 'active_record'
+require 'delayed_job_active_record'
+require "delayed_job_unique_key/version"
+require 'delayed_job_unique_key/base'
+require 'delayed_job_unique_key/active_record_job'
+
+
+
View
12 lib/delayed_job_unique_key/active_record_job.rb
@@ -0,0 +1,12 @@
+module Delayed
+ module Backend
+ module ActiveRecord
+ class Job
+ include Delayed::Backend::Base
+
+ attr_accessible :priority, :run_at, :queue, :payload_object, :failed_at, :locked_at, :locked_by, :unique_key
+ before_create :check_unique_key
+ end
+ end
+ end
+end
View
52 lib/delayed_job_unique_key/base.rb
@@ -0,0 +1,52 @@
+module Delayed
+ module Backend
+ module Base
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ module ClassMethods
+ # Add a job to the queue
+ def enqueue(*args)
+ options = {
+ :priority => Delayed::Worker.default_priority
+ }.merge!(args.extract_options!)
+
+ options[:payload_object] ||= args.shift
+
+ if args.size > 0
+ warn "[DEPRECATION] Passing multiple arguments to `#enqueue` is deprecated. Pass a hash with :priority and :run_at."
+ options[:priority] = args.first || options[:priority]
+ options[:unique_key] = options[:unique_key]
+ options[:run_at] = args[1]
+ end
+
+ unless options[:payload_object].respond_to?(:perform)
+ raise ArgumentError, 'Cannot enqueue items which do not respond to perform'
+ end
+
+ if Delayed::Worker.delay_jobs
+ self.new(options).tap do |job|
+ Delayed::Worker.lifecycle.run_callbacks(:enqueue, job) do
+ job.hook(:enqueue)
+ job.save
+ end
+ end
+ else
+ Delayed::Job.new(:payload_object => options[:payload_object]).tap do |job|
+ job.invoke_job
+ end
+ end
+ end
+ end
+
+ protected
+
+ def check_unique_key
+ return true if !self.respond_to?(:unique_key) || self.unique_key.blank?
+ self.class.where(:unique_key => self.unique_key, :locked_at => nil).first.nil?
+ end
+
+ end
+ end
+end
View
3  lib/delayed_job_unique_key/version.rb
@@ -0,0 +1,3 @@
+module DelayedJobUniqueKey
+ VERSION = "0.1.0"
+end
View
17 lib/generators/delayed_job_unique_key/install_generator.rb
@@ -0,0 +1,17 @@
+require 'rails/generators'
+require 'rails/generators/migration'
+require 'rails/generators/active_record/migration'
+
+
+module DelayedJobUniqueKey
+ class InstallGenerator < Rails::Generators::Base
+ include Rails::Generators::Migration
+ extend ActiveRecord::Generators::Migration
+
+ self.source_paths << File.join(File.dirname(__FILE__), 'templates')
+
+ def create_migration_file
+ migration_template 'add_unique_key_migration.rb', 'db/migrate/add_unique_key_delayed_jobs.rb'
+ end
+ end
+end
View
1  lib/generators/delayed_job_unique_key/templates/.#upgrade_migration.rb
View
11 lib/generators/delayed_job_unique_key/templates/add_unique_key_migration.rb
@@ -0,0 +1,11 @@
+class AddUniqueKeyDelayedJobs < ActiveRecord::Migration
+ def self.up
+ add_column :delayed_jobs, :unique_key, :string
+ add_index :delayed_jobs, [:unique_key, :locked_at], :name => 'delayed_jobs_unique_key'
+ end
+
+ def self.down
+ remove_index :delayed_jobs, :name => 'delayed_jobs_unique_key'
+ remove_column :delayed_jobs, :unique_key
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.