Sha256: fc18294f5c304602b71f81839846e3009a84571287e3a45f615615317fd58462

Contents?: true

Size: 1.24 KB

Versions: 7

Compression:

Stored size: 1.24 KB

Contents

# encoding: UTF-8

module Izanami

  # Inject in a class or module all the {Izanami::Worker} modules.
  #
  # @param [Module, Class] target where to inject (include and extend) the modules.
  def self.Worker(target)
    target.send :extend,  Worker::ClassMethods
    target.send :include, Worker::InstanceMethods
  end

  # Basic interface to create {Izanami::Workers}
  module Worker
    module ClassMethods

      # @see {#initialize} and {#defer}
      def defer(*args)
        new(*args).defer
      end

      # @see {#initialize} and {#call}
      def call(*args)
        new(*args).call
      end

      # @see {.call}
      def run(*args)
        new(*args).run
      end
    end

    module InstanceMethods

      # Executes a {#call} method in a different process, forking the
      # current one.
      #
      # @note the process forked is detached.
      #
      # @return [String, Fixnum] the new process pid.
      def defer
        pid = Process.fork { call }
        Process.detach(pid)

        pid
      end

      # Main worker action. Must be defined in the concrete class.
      def call
        raise 'The method #call should be defined in the worker class'
      end

      # @see {#call}
      def run
        call
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
izanami-0.20.0 lib/izanami/worker.rb
izanami-0.19.0 lib/izanami/worker.rb
izanami-0.18.0 lib/izanami/worker.rb
izanami-0.17.0 lib/izanami/worker.rb
izanami-0.16.0 lib/izanami/worker.rb
izanami-0.15.0 lib/izanami/worker.rb
izanami-0.14.0 lib/izanami/worker.rb