Sha256: 290a86a26897cc9483c5ec6010e4ce4c3e41b5341ba6a00fc83ef292d5f8b439

Contents?: true

Size: 1.99 KB

Versions: 4

Compression:

Stored size: 1.99 KB

Contents

require_relative './input_stream'
require_relative './output_stream'
require_relative './request_worker'
module CapistranoSentinel
  # class used to handle the rake worker and sets all the hooks before and after running the worker
  class RequestHooks

    ENV_KEY_JOB_ID = 'multi_cap_job_id'
    SUBSCRIPTION_PREFIX ="rake_worker_"
    PUBLISHER_PREFIX ="celluloid_worker_"

    attr_accessor :job_id, :task

    def initialize(task = nil)
      @job_id  = ENV[CapistranoSentinel::RequestHooks::ENV_KEY_JOB_ID]
      @task = task.respond_to?(:fully_qualified_name) ? task.fully_qualified_name : task
    end

    def automatic_hooks(&block)
      if job_id.present? && @task.present?
        actor_start_working(action: 'invoke')
        actor.wait_execution until actor.task_approved
        actor_execute_block(&block)
      else
        block.call
      end
    end

    def print_question?(question)
      if job_id.present?
        actor.user_prompt_needed?(question)
      else
        yield if block_given?
      end
    end


    def show_bundler_progress
      actor_start_working({action: "bundle_install"}) if @task.present? && @task.to_s.size > 2
      yield if block_given?
    end

  private

    def actor
      @actor ||= CapistranoSentinel::RequestWorker.new
      @actor
    end

    def output_stream
      CapistranoSentinel::OutputStream
    end

    def input_stream
      CapistranoSentinel::InputStream
    end

    def before_hooks
      stringio = StringIO.new
      output = output_stream.hook(stringio)
      input = input_stream.hook(actor, stringio)
      [input, output]
    end

    def after_hooks
      input_stream.unhook
      output_stream.unhook
    end

    def actor_execute_block(&block)
      before_hooks
      block.call
      after_hooks
    end

    def actor_start_working(additionals = {})
      additionals = additionals.present? ? additionals : {}
      data = {job_id: job_id, task: @task }.merge(additionals)
      data = data.stringify_keys
      actor.work(data)
    end


  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
capistrano_sentinel-0.0.5 lib/capistrano_sentinel/classes/request_hooks.rb
capistrano_sentinel-0.0.3 lib/capistrano_sentinel/classes/request_hooks.rb
capistrano_sentinel-0.0.2 lib/capistrano_sentinel/classes/request_hooks.rb
capistrano_sentinel-0.0.1 lib/capistrano_sentinel/classes/request_hooks.rb