Sha256: 0675efa0efef7f301bc4b2680a74cdf1ab38cb332a844f558eae813ea2691975

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

module RestFtpDaemon
  class Worker
    include LoggerHelper
    attr_reader :logger

    if Settings.newrelic_enabled?
      include ::NewRelic::Agent::Instrumentation::ControllerInstrumentation
    end

    def initialize wid, pool = nil
      # Logger
      @logger = RestFtpDaemon::LoggerPool.instance.get :workers
      @log_worker_status_changes = true

      # Worker name
      #@wid = wid
      @pool = pool

      # Set thread context
      Thread.current.thread_variable_set :pool, pool
      Thread.current.thread_variable_set :wid, wid
      Thread.current.thread_variable_set :started_at, Time.now
      worker_status WORKER_STATUS_STARTING
    end

  protected

    def log_context
      {
        wid: Thread.current.thread_variable_get(:wid),
        jid: Thread.current.thread_variable_get(:jid),
      }
    end

    def start
      loop do
        begin
          work
        rescue StandardError => e
          log_error "WORKER EXCEPTION: #{e.inspect}"
          sleep 1
        end
      end
    end

    def worker_status status, job = nil
      # Update thread variables
      Thread.current.thread_variable_set :status, status
      Thread.current.thread_variable_set :updted_at, Time.now

      # Nothin' to log if "silent"
      return unless @log_worker_status_changes

      # Log this status change
      if job.is_a?(Job)
        log_info "#{status} - job[#{job.id}] status[#{job.status}] error[#{job.error}]"
      else
        log_info "#{status}"
      end
    end

    def worker_jid jid
      Thread.current.thread_variable_set :jid, jid
      Thread.current.thread_variable_set :updted_at, Time.now
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rest-ftp-daemon-0.245.1 lib/rest-ftp-daemon/worker.rb
rest-ftp-daemon-0.245 lib/rest-ftp-daemon/worker.rb