Module: Activr::Async

Defined in:
lib/activr/async.rb

Overview

Async hooks module

The async hooks module permits to plug any job system to run some part if Activr code asynchronously.

Possible hooks are:

- :route_activity - An activity must me routed by the Dispatcher
- :timeline_handle - An activity must be handled by a timeline

A hook class:

- must implement a `#enqueue` method, used to enqueue the async job
- must call `Activr::Async.<hook_name>` method in the async job

Hook classes to use are specified thanks to the ‘config.async` hash.

When Resque is detected inside a Rails application then defaults hooks are provided out of the box (see the Resque module).

Examples:

The default :route_activity hook handler when Resque is detected in a Rails application:


# config
Activr.configure do |config|
  config.async[:route_activity] ||= Activr::Async::Resque::RouteActivity
end

class Activr::Async::Resque::RouteActivity
  @queue = 'activr_route_activity'

  class << self
    def enqueue(activity)
      ::Resque.enqueue(self, activity.to_hash)
    end

    def perform(activity_hash)
      # unserialize argument
      activity_hash = Activr::Activity.unserialize_hash(activity_hash)
      activity = Activr::Activity.from_hash(activity_hash)

      # call hook
      Activr::Async.route_activity(activity)
    end
  end # class << self
end # class RouteActivity

Defined Under Namespace

Modules: Resque

Class Method Summary collapse

Class Method Details

.hook(name, *args) ⇒ Object

Run hook

If an async class is defined for that hook name then it is used to process the hook asynchronously, else the hooked code is executed immediately.

Parameters:

  • name (Symbol)

    Hook name to run

  • args (Array)

    Hook parameters



58
59
60
61
62
63
64
65
66
# File 'lib/activr/async.rb', line 58

def hook(name, *args)
  if Activr.config.async[name] && (ENV['ACTIVR_FORCE_SYNC'] != 'true')
    # async
    Activr.config.async[name].enqueue(*args)
  else
    # sync
    self.__send__(name, *args)
  end
end

.route_activity(activity) ⇒ Object

Hook: route an activity

Parameters:

  • activity (Activity)

    Activity to route



76
77
78
# File 'lib/activr/async.rb', line 76

def route_activity(activity)
  Activr.dispatcher.route(activity)
end

.timeline_handle(timeline, activity, route) ⇒ Object

Hook: timeline handles an activity thanks to given route

Parameters:

  • timeline (Timeline)

    Timeline that handles the activity

  • activity (Activity)

    Activity to handle

  • route (Timeline::Route)

    The route causing that activity handling



85
86
87
# File 'lib/activr/async.rb', line 85

def timeline_handle(timeline, activity, route)
  timeline.handle_activity(activity, route)
end