Sha256: b3b1ddf545806b916060ebe3564affcaac50cba8bef4dea46af88cc971c18a10

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

module Tennis
  module Worker
    module Deferable

      autoload :Action, "tennis/worker/deferable/action"

      def self.included(base)
        base.extend DSL
        base.include Worker::Generic
        base.serialize Serializer::Generic.new
        define_work_broker(base)
      end

      def self.define_work_broker(base)
        base.work do |message|
          begin
            receiver, method, arguments = message
            result = receiver.__send__(method, *arguments)
            instance_exec(result, &base._on_success)
          rescue => exception
            instance_exec(exception, message, &base._on_error)
          end
        end
      end

      def defer
        Action.new(self.class, self)
      end

      module DSL

        def defer
          Action.new(self, self)
        end

        def on_error(&block)
          @_on_error = block
        end

        def on_success(&block)
          @_on_success = block
        end

        def _on_error
          @_on_error || ->(_, _){ reject! }
        end

        def _on_success
          @_on_success || ->(_){ ack! }
        end

      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tennis-jobs-0.3.1 lib/tennis/worker/deferable.rb
tennis-jobs-0.3.0 lib/tennis/worker/deferable.rb
tennis-jobs-0.2.0 lib/tennis/worker/deferable.rb
tennis-jobs-0.1.0 lib/tennis/worker/deferable.rb