Sha256: 0a227ef180d1103af9a8b383c5aac6a3fd9b39c79c4ea63b134a68423d9ad204

Contents?: true

Size: 1.28 KB

Versions: 7

Compression:

Stored size: 1.28 KB

Contents

require 'fasten/std_thread_proxy'

module Fasten
  module Support
    module ThreadWorker
      attr_accessor :thread

      def start
        @queue = Queue.new

        self.thread = Thread.new do
          process_incoming_requests
        end
      end

      def kill
        log_info 'Removing worker'
        thread.exit
      rescue StandardError => e
        log_warn "Ignoring error killing worker #{self}, error: #{e}"
      ensure
        @queue.clear
      end

      def send_request_to_child(task)
        task.state = :RUNNING
        task.worker = self
        self.running_task = task
        self.state = :RUNNING

        @queue.push task
      end

      def receive_request_from_parent
        @queue.pop
      end

      def send_response_to_parent(task)
        log_info "Sending task response back to runner #{task}"

        runner.queue.push task
      end

      def redirect_std(path)
        StdThreadProxy.install

        FileUtils.mkdir_p File.dirname(path)
        @redirect_log = File.new path, 'a'
        @redirect_log.sync = true
        StdThreadProxy.thread_io = @redirect_log
        logger.reopen(@redirect_log)
      end

      def restore_std
        @redirect_log&.close
        StdThreadProxy.thread_io = nil
        logger.reopen(log_file)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
fasten-0.14.2 lib/fasten/support/thread_worker.rb
fasten-0.14.0 lib/fasten/support/thread_worker.rb
fasten-0.12.8 lib/fasten/support/thread_worker.rb
fasten-0.12.6 lib/fasten/support/thread_worker.rb
fasten-0.12.4 lib/fasten/support/thread_worker.rb
fasten-0.12.2 lib/fasten/support/thread_worker.rb
fasten-0.12.0 lib/fasten/support/thread_worker.rb