lib/patriot/worker/base.rb in patriot-workflow-scheduler-0.7.0 vs lib/patriot/worker/base.rb in patriot-workflow-scheduler-0.7.1
- old
+ new
@@ -2,10 +2,31 @@
require 'patriot/command'
module Patriot
module Worker
+ def get_pid(config)
+ pid_file = get_pid_file(config)
+ return nil unless File.exists?(pid_file)
+ pid = nil
+ File.open(pid_file,'r'){|f| pid = f.read.strip.to_i }
+ begin
+ Process.getpgid(pid)
+ rescue Errno::ESRCH
+ @logger.warn("process #{pid} not exist but pid file remains") if @logger
+ return nil
+ end
+ return pid
+ end
+ module_function :get_pid
+
+ def get_pid_file(config)
+ worker_name = config.get('worker_name', Patriot::Worker::DEFAULT_WORKER_NAME)
+ return File.join($home, 'run', "patriot-worker_#{worker_name}.pid")
+ end
+ module_function :get_pid_file
+
# @abstract
# base class for worker implementations
class Base
include Patriot::Util::Logger
@@ -22,11 +43,10 @@
@job_store = create_jobstore(Patriot::JobStore::ROOT_STORE_ID, @config)
@host = `hostname`.chomp
@cycle = config.get('fetch_cycle', Patriot::Worker::DEFAULT_FETCH_CYCLE).to_i
@fetch_limit = config.get('fetch_limit', Patriot::Worker::DEFAULT_FETCH_LIMIT).to_i
@worker_name = config.get('worker_name', Patriot::Worker::DEFAULT_WORKER_NAME)
- @pid_file = File.join($home, 'run', "patriot-worker_#{@worker_name}.pid")
@info_server = Patriot::Worker::InfoServer.new(self,@config)
end
# execute a job
# @param [Patriot::JobStore::JobTicket] job_ticket a ticket of job to be executed
@@ -77,20 +97,11 @@
return job_ticket.exit_code
end
# @return [Integer] pid if the worker is running, otherwise nil
def get_pid
- return nil unless File.exists?(@pid_file)
- pid = nil
- File.open(@pid_file,'r'){|f| pid = f.read.strip.to_i }
- begin
- Process.getpgid(pid)
- rescue Errno::ESRCH
- @logger.warn("process #{pid} not exist but pid file remains")
- return nil
- end
- return pid
+ return Patriot::Worker.get_pid(@config)
end
# send a request graceful shutdown to a running worker
# @return [Boolean] true worker is running and request is sent, otherwise false
def request_shutdown
@@ -104,12 +115,12 @@
end
# main entry point of worker processing
def start_worker
return unless get_pid.nil?
-
@logger.info "starting worker #{@node}@#{@host}"
- File.open(@pid_file, 'w') {|f| f.write($$)} # save pid for shutdown
+ pid_file = Patriot::Worker.get_pid_file(@config)
+ File.open(pid_file, 'w') {|f| f.write($$)} # save pid for shutdown
set_traps
@info_server.start_server
@logger.info "initiating worker #{@node}@#{@host}"
init_worker
@status = Patriot::Worker::Status::ACTIVE