Sha256: 29bc8a83842de23d34a2c9c8500e5cbffc2f09fef455926b23cb9fbf9bb21d32

Contents?: true

Size: 1.29 KB

Versions: 10

Compression:

Stored size: 1.29 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 => error
        log_warn "Ignoring error killing worker #{self}, error: #{error}"
      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

10 entries across 10 versions & 1 rubygems

Version Path
fasten-0.10.0 lib/fasten/support/thread_worker.rb
fasten-0.8.8 lib/fasten/support/thread_worker.rb
fasten-0.8.6 lib/fasten/support/thread_worker.rb
fasten-0.8.4 lib/fasten/support/thread_worker.rb
fasten-0.8.0 lib/fasten/support/thread_worker.rb
fasten-0.7.6 lib/fasten/support/thread_worker.rb
fasten-0.7.4 lib/fasten/support/thread_worker.rb
fasten-0.7.2 lib/fasten/support/thread_worker.rb
fasten-0.7.0 lib/fasten/support/thread_worker.rb
fasten-0.6.0 lib/fasten/support/thread_worker.rb