Module: Expirable

Extended by:
ActiveSupport::Concern
Defined in:
lib/expirable.rb,
lib/generators/expirable/setup/setup_generator.rb

Overview

Rails model mixin that supports deadlines in workflows.

It requires clients to do the following:

1. Include Expirable
2. define a `deadline_expired` instance method that will be invoked when the
   model's deadline expires
3. define a class method named `newly_expired` that returns a collection of model
   instances that have expired, but haven't yet received a `deadline_expired`
   message.

The mixin relies on an external service to periodically call Expirable.send_expired_events, which polls for models which have expired but have not yet received a deadline_expired message.

Author:

Defined Under Namespace

Modules: Generators

Class Method Summary collapse

Class Method Details

.register(klass) ⇒ Object



23
24
25
# File 'lib/expirable.rb', line 23

def self.register(klass)
  @klasses << klass
end

.send_expired_event(object) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/expirable.rb', line 31

def self.send_expired_event(object)
  Rails.logger.info "Sending deadline expired event to #{object}"
  begin
    object.deadline_expired
  rescue Exception => e
    Rails.logger.error "      Deadline expiration failed with \#{e.message}\n      \#{e.backtrace.join(\"\\n\")}\n    eos\n  end\nend\n".strip_heredoc

.send_expired_eventsObject

Called periodically to send deadline_expired messages. It doesn’t actually send the messages sequentially, but enqueues a call to send_expired_event to execute the message send asynchronously.

Note that this method has no notion of priority, and may enqueue calls on objects that already have method calls enqueued. It is up to the client to make sure that there are enough workers so that the processing rate of the queue exceeds the arrival rate.



51
52
53
54
55
56
57
58
59
# File 'lib/expirable.rb', line 51

def self.send_expired_events
  @klasses.each do |klass|
    klass.newly_expired.each do |object|
      self.delay.send_expired_event(object)
    end
  end

  nil
end