All Projects → anjlab → triggerable

anjlab / triggerable

Licence: MIT license
Trigger/automation engine for ActiveRecord models

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to triggerable

Forest
分布式任务调度平台,分布式,任务调度,schedule,scheduler
Stars: ✭ 231 (+1183.33%)
Mutual labels:  schedule
genshin-schedule
🕑 Genshin farming scheduler
Stars: ✭ 74 (+311.11%)
Mutual labels:  schedule
DNTScheduler.Core
DNTScheduler.Core is a lightweight ASP.NET Core's background tasks runner and scheduler
Stars: ✭ 44 (+144.44%)
Mutual labels:  schedule
Advanced-xv6
Modern improvements for MIT's xv6 OS
Stars: ✭ 26 (+44.44%)
Mutual labels:  schedule
AirflowETL
Blog post on ETL pipelines with Airflow
Stars: ✭ 20 (+11.11%)
Mutual labels:  schedule
Timespace
A jQuery plugin to handle displaying of time events
Stars: ✭ 27 (+50%)
Mutual labels:  schedule
Stevejobs
A simple jobs queue that just works (for Meteor.js)
Stars: ✭ 195 (+983.33%)
Mutual labels:  schedule
scheduled-pod-autoscaler
Custom Kubernetes controller for GitOps native scheduled scaling
Stars: ✭ 20 (+11.11%)
Mutual labels:  schedule
Torrent-Alert
Torrent Keyword Search, Schedule & Alert #BuildWithMeteorJS
Stars: ✭ 21 (+16.67%)
Mutual labels:  schedule
croner
Trigger functions and/or evaluate cron expressions in JavaScript. No dependencies. Most features. All environments.
Stars: ✭ 169 (+838.89%)
Mutual labels:  schedule
MRTScheduleSwiftUI
MRT Schedule & Locator iOS App built using SwiftUI
Stars: ✭ 32 (+77.78%)
Mutual labels:  schedule
crystime
Advanced time, calendar, schedule, and remind library for Crystal
Stars: ✭ 23 (+27.78%)
Mutual labels:  schedule
Saber-Bot
A discord bot to manage schedules & calendars with discord notifications.
Stars: ✭ 30 (+66.67%)
Mutual labels:  schedule
Pagerbeauty
📟✨ PagerDuty on-call widget for monitoring dashboard. Datadog and Grafana compatible
Stars: ✭ 250 (+1288.89%)
Mutual labels:  schedule
aws-tag-sched-ops
Retired, please see https://github.com/sqlxpert/lights-off-aws
Stars: ✭ 24 (+33.33%)
Mutual labels:  schedule
Schedule Bundle
Schedule Cron jobs (commands/callbacks/bash scripts) within your Symfony application.
Stars: ✭ 216 (+1100%)
Mutual labels:  schedule
legacy-bottlerockets
Node.js high availability queue and scheduler for background job processing
Stars: ✭ 25 (+38.89%)
Mutual labels:  schedule
serverless-local-schedule
⚡️🗺️⏰ Schedule AWS CloudWatch Event based invocations in local time(with DST support!)
Stars: ✭ 68 (+277.78%)
Mutual labels:  schedule
CloudSimPy
CloudSimPy: Datacenter job scheduling simulation framework
Stars: ✭ 144 (+700%)
Mutual labels:  schedule
delay-timer
Time-manager of delayed tasks. Like crontab, but synchronous asynchronous tasks are possible scheduling, and dynamic add/cancel/remove is supported.
Stars: ✭ 257 (+1327.78%)
Mutual labels:  schedule

Triggerable

Code Climate

Triggerable is a powerful engine for adding a conditional behaviour for ActiveRecord models. This logic can be defined in two ways - as triggers and automations. Triggers are called right after model creating, updating or saving, and automations are run on schedule (e.g. 2 hours after update).

Installation

Add this line to your application's Gemfile:

gem 'triggerable'

And then execute:

bundle

Usage

Setup and defining trigger and automation:

class User < ActiveRecord::Base
  trigger on: :after_create, if: { receives_sms: true } do
    user.send_welcome_sms
  end

  automation if: { created_at: { after: 24.hours }, confirmed: false } do
    send_confirmation_email
  end
end

Combining different conditions and predicates:

trigger on: :after_create, if: { field: { is: 1 } }, ...
# short form
trigger on: :after_create, if: { field: 1 }, ...

trigger on: :after_create, if: { field: { is_not: 1 } }, ...

trigger on: :after_create, if: { field: { in: [1, 2, 3] } }, ...
# short form
trigger on: :after_create, if: { field: [1, 2, 3] }, ...

trigger on: :after_create, if: { field: { greater_than: 1 } }, ...
trigger on: :after_create, if: { field: { less_than: 1 } }, ...

trigger on: :after_create, if: { field: { exists: true } }, ...

trigger on: :after_create, if: { and: [{ field1: '1' }, { field2: 1 }] }, ...
trigger on: :after_create, if: { or: [{ field1: '1' }, { field2: 1 }] }, ...

Triggerable does not run automations by itself, you should call Triggerable::Engine.run_automations(interval) using any scheduling script. Interval is a time difference between calling the method (e.g. 1.hour). You should avoid situations when your interval is less then the time your automations need to complete!

Automation calls action block for each found object, but it's possible to pass a relation to action block using pass_relation option:

class User < ActiveRecord::Base
  automation if: {
    created_at: { after: 24.hours }, confirmed: false
  }, pass_relation: true do
    each(&:send_confirmation_email)
  end
end

If you have more complex condition or need to check associations (not supported in DSL now), you should use a lambda condition form:

trigger on: :after_update, if: -> { orders.any? } do
  # ...
end

If you need to share logic between triggers/automations bodies you can move it into separate class. It should be inherited from Triggerable::Actions::Action and implement a single method run_for!(object, rule_name) where trigger_name is a string passed to rule in :name option and obj is a triggered object. Then you can pass a name of your action class instead of do block.

class SendWelcomeSms < Triggerable::Actions::Action
  def run_for! object, trigger_name
    SmsGateway.send_to object.phone, welcome_text
  end
end

class User
  trigger on: :after_create, if: { receives_sms: true }, do: :send_welcome_sms
end

Logging and debugging

You can easily turn on logging and debugging (using puts):

Triggerable::Engine.logger = Logger.new(File.join(Rails.root, 'log', 'triggers.log'))
Triggerable::Engine.debug = true

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request
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].