Sha256: 45aa02631a50246e7d57ee8bf38c6db94073e574e0c4814e288f8150e0c5cd74

Contents?: true

Size: 1.79 KB

Versions: 4

Compression:

Stored size: 1.79 KB

Contents

require_relative '../celluloid/rake_worker'
require_relative './input_stream'
require_relative './output_stream'
module CapistranoMulticonfigParallel
  # class used to handle the rake worker and sets all the hooks before and after running the worker
  class RakeHookActor
    attr_accessor :task, :env
    def initialize(env, task)
      @env = env
      @task = task
    end

    def work(&block)
      if job_id.present?
        actor_start_working
        actor.wait_execution until actor.task_approved
        return unless actor.task_approved
        actor_execute_block(&block)
      else
        block.call
      end
    end

  private

    def actor_execute_block(&block)
      stringio = StringIO.new
      CapistranoMulticonfigParallel::OutputStream.hook(stringio)
      CapistranoMulticonfigParallel::InputStream.hook(actor, stringio)
      block.call
      CapistranoMulticonfigParallel::InputStream.unhook
      CapistranoMulticonfigParallel::OutputStream.unhook
    end

    def actor_start_working
      if actor.blank?
        CapistranoMulticonfigParallel::RakeWorker.supervise_as rake_actor_id
        actor.work(@env, actor_id: rake_actor_id, job_id: job_id, task: @task)
      else
        actor.publish_new_work(@env, task: @task)
      end
    end

    def supervise_actor
      CapistranoMulticonfigParallel::RakeWorker.supervise_as(rake_actor_id,
                                                             actor_id: rake_actor_id,
                                                             job_id: job_id) if actor.blank?
    end

    def actor
      Celluloid::Actor[rake_actor_id]
    end

    def job_id
      @env[CapistranoMulticonfigParallel::ENV_KEY_JOB_ID]
    end

    def rake_actor_id
      @env['count_rake'].present? ? "rake_worker_#{job_id}_count" : "rake_worker_#{job_id}"
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
capistrano_multiconfig_parallel-0.17.0 lib/capistrano_multiconfig_parallel/classes/rake_hook_actor.rb
capistrano_multiconfig_parallel-0.16.2 lib/capistrano_multiconfig_parallel/helpers/rake_hook_actor.rb
capistrano_multiconfig_parallel-0.16.1 lib/capistrano_multiconfig_parallel/helpers/rake_hook_actor.rb
capistrano_multiconfig_parallel-0.16.0 lib/capistrano_multiconfig_parallel/helpers/rake_hook_actor.rb