All Projects → veeqo → activejob-uniqueness

veeqo / activejob-uniqueness

Licence: MIT license
Unique jobs for ActiveJob. Ensure the uniqueness of jobs in the queue.

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to activejob-uniqueness

Redisson
Redisson - Redis Java client with features of In-Memory Data Grid. Over 50 Redis based Java objects and services: Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Publish / Subscribe, Bloom filter, Spring Cache, Tomcat, Scheduler, JCache API, Hibernate, MyBatis, RPC, local cache ...
Stars: ✭ 17,972 (+9163.92%)
Mutual labels:  lock, distributed-locks
LockView
一个自定义的多功能的锁屏页解锁控件
Stars: ✭ 17 (-91.24%)
Mutual labels:  lock
Lock Threads
GitHub Action that locks closed issues and pull requests after a period of inactivity
Stars: ✭ 156 (-19.59%)
Mutual labels:  lock
Pottery
Redis for humans. 🌎🌍🌏
Stars: ✭ 204 (+5.15%)
Mutual labels:  lock
Jxpatternlock
An easy-to-use, powerful, customizable pattern lock view in swift. 图形解锁/手势解锁 / 手势密码 / 图案密码 / 九宫格密码
Stars: ✭ 165 (-14.95%)
Mutual labels:  lock
Javainterview
java中高级基础指南
Stars: ✭ 222 (+14.43%)
Mutual labels:  lock
React Locky
"🔒-y" – Asgardian God of Event Scoping 📦, Scroll Locking 📜, Silence Casting 🙊
Stars: ✭ 141 (-27.32%)
Mutual labels:  lock
async
Synchronization and asynchronous computation package for Go
Stars: ✭ 104 (-46.39%)
Mutual labels:  lock
maintenance job
Mechanism to run testable one-off jobs in Rails at deploy time to manipulate data
Stars: ✭ 27 (-86.08%)
Mutual labels:  activejob
Concurrent
Functional Concurrency Primitives
Stars: ✭ 206 (+6.19%)
Mutual labels:  lock
Ninja Mutex
Mutex implementation for PHP
Stars: ✭ 180 (-7.22%)
Mutual labels:  lock
Aioredlock
🔒 The asyncio implemetation of Redis distributed locks
Stars: ✭ 171 (-11.86%)
Mutual labels:  lock
php-fslock
A simple lock implementation using flock.
Stars: ✭ 18 (-90.72%)
Mutual labels:  lock
Easy
开源的Java开发脚手架,工作经验总结,springboot,springcloud,基于tk-mybatis代码反向生成,基于redis(redisson)注解形式加分布式锁等,计划将用该脚手架抄袭jeesite和ruoyi还有基于vue的后台权限管理系统做一套开源的后台管理和cms系统,域名服务器已买好,脚手架还在继续更新中,更新完毕开始更新easysite
Stars: ✭ 160 (-17.53%)
Mutual labels:  lock
ES-Timer
A USB timer powered by Digispark ATtiny85 according to 🍅 pomodoro time management technique
Stars: ✭ 45 (-76.8%)
Mutual labels:  lock
Composer Lock Diff
See what has changed after a composer update
Stars: ✭ 154 (-20.62%)
Mutual labels:  lock
React Focus On
🎯 Solution for WAI ARIA compatible modal dialogs or full-screen tasks, you were looking for
Stars: ✭ 180 (-7.22%)
Mutual labels:  lock
Lock.swift
A Swift & iOS framework to authenticate using Auth0 and with a Native Look & Feel
Stars: ✭ 215 (+10.82%)
Mutual labels:  lock
KittyEvents
Super simple event system on top of ActiveJob
Stars: ✭ 20 (-89.69%)
Mutual labels:  activejob
django-concurrency-talk
🎭 Database Integrity in Django: Safely Handling Critical Data in Distributed Systems
Stars: ✭ 49 (-74.74%)
Mutual labels:  lock

Job uniqueness for ActiveJob

Build Status Gem Version

The gem allows to protect job uniqueness with next strategies:

Strategy The job is locked The job is unlocked
until_executing when pushed to the queue when processing starts
until_executed when pushed to the queue when the job is processed successfully
until_expired when pushed to the queue when the lock is expired
until_and_while_executing when pushed to the queue when processing starts
a runtime lock is acquired to prevent simultaneous jobs
has extra options: runtime_lock_ttl, on_runtime_conflict
while_executing when processing starts when the job is processed
with any result including an error

Inspired by SidekiqUniqueJobs, uses Redlock under the hood.

Installation

Add the activejob-uniqueness gem to your Gemfile.

gem 'activejob-uniqueness'

If you want jobs unlocking for Sidekiq Web UI, require the patch explicitly. Queues cleanup becomes slower!

gem 'activejob-uniqueness', require: 'active_job/uniqueness/sidekiq_patch'

And run bundle install command.

Configuration

ActiveJob::Uniqueness is ready to work without any configuration. It will use REDIS_URL to connect to Redis instance. To override the defaults, create an initializer config/initializers/active_job_uniqueness.rb using the following command:

rails generate active_job:uniqueness:install

Usage

Make the job to be unique

class MyJob < ActiveJob::Base
  # new jobs with the same args will raise error until existing one is executed
  unique :until_executed

  def perform(args)
    # work
  end
end

Tune uniqueness settings per job

class MyJob < ActiveJob::Base
  # new jobs with the same args will be logged within 3 hours or until existing one is being executing
  unique :until_executing, lock_ttl: 3.hours, on_conflict: :log

  def perform(args)
    # work
  end
end

You can set defaults globally with the configuration

Control lock conflicts

class MyJob < ActiveJob::Base
  # Proc gets the job instance including its arguments
  unique :until_executing, on_conflict: ->(job) { job.logger.info "Oops: #{job.arguments}" }

  def perform(args)
    # work
  end
end

Control lock key arguments

class MyJob < ActiveJob::Base
  unique :until_executed

  def perform(foo, bar, baz)
    # work
  end

  def lock_key_arguments
    arguments.first(2) # baz is ignored
  end
end

Control the lock key

class MyJob < ActiveJob::Base
  unique :until_executed

  def perform(foo, bar, baz)
    # work
  end

  def lock_key
    'qux' # completely custom lock key
  end

  def runtime_lock_key
    'quux' # completely custom runtime lock key for :until_and_while_executing
  end
end

Unlock jobs manually

The selected strategy automatically unlocks jobs, but in some cases (e.g. the queue is purged) it is handy to unlock jobs manually.

# Remove the lock for particular arguments:
MyJob.unlock!(foo: 'bar')
# or
ActiveJob::Uniqueness.unlock!(job_class_name: 'MyJob', arguments: [{foo: 'bar'}])

# Remove all locks of MyJob
MyJob.unlock!
# or
ActiveJob::Uniqueness.unlock!(job_class_name: 'MyJob')

# Remove all locks
ActiveJob::Uniqueness.unlock!

Test mode

Most probably you don't want jobs to be locked in tests. Add this line to your test suite (rails_helper.rb):

ActiveJob::Uniqueness.test_mode!

Logging

ActiveJob::Uniqueness instruments ActiveSupport::Notifications with next events:

  • lock.active_job_uniqueness
  • runtime_lock.active_job_uniqueness
  • unlock.active_job_uniqueness
  • runtime_unlock.active_job_uniqueness
  • conflict.active_job_uniqueness
  • runtime_conflict.active_job_uniqueness

And then writes to ActiveJob::Base.logger.

ActiveJob prior to version 6.1 will always log Enqueued MyJob (Job ID) ... even if the callback chain is halted. Details

Testing

Run redis server (in separate console):

docker run --rm -p 6379:6379 redis

Run tests with:

bundle
rake

Sidekiq API support

ActiveJob::Uniqueness supports Sidekiq API to unset job locks on queues cleanup (e.g. via Sidekiq Web UI). Starting Sidekiq 5.1 job death also triggers locks cleanup. Take into account that big queues cleanup becomes much slower because each job is being unlocked individually. In order to activate Sidekiq API patch require it explicitly in your Gemfile:

gem 'activejob-uniqueness', require: 'active_job/uniqueness/sidekiq_patch'

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/veeqo/activejob-uniqueness.

License

The gem is available as open source under the terms of the MIT License.

About Veeqo

At Veeqo, our team of Engineers is on a mission to create a world-class Inventory and Shipping platform, built to the highest standards in best coding practices. We are a growing team, looking for other passionate developers to join us on our journey. If you're looking for a career working for one of the most exciting tech companies in ecommerce, we want to hear from you.

Veeqo developers blog

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].