|
|
@ -7,6 +7,7 @@ import times
|
|
|
|
import procname
|
|
|
|
import procname
|
|
|
|
import socket
|
|
|
|
import socket
|
|
|
|
import signal
|
|
|
|
import signal
|
|
|
|
|
|
|
|
import traceback
|
|
|
|
from pickle import dumps
|
|
|
|
from pickle import dumps
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
from logbook import Logger
|
|
|
|
from logbook import Logger
|
|
|
@ -284,19 +285,9 @@ class Worker(object):
|
|
|
|
self._is_horse = True
|
|
|
|
self._is_horse = True
|
|
|
|
random.seed()
|
|
|
|
random.seed()
|
|
|
|
self.log = Logger('horse')
|
|
|
|
self.log = Logger('horse')
|
|
|
|
try:
|
|
|
|
|
|
|
|
self.perform_job(job)
|
|
|
|
success = self.perform_job(job)
|
|
|
|
except Exception as e:
|
|
|
|
sys.exit(int(not success))
|
|
|
|
self.log.exception(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Store the exception information...
|
|
|
|
|
|
|
|
job.exc_info = e
|
|
|
|
|
|
|
|
job.save()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ...and put the job on the failure queue
|
|
|
|
|
|
|
|
self.failure_queue.push_job_id(job.id)
|
|
|
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
self._horse_pid = child_pid
|
|
|
|
self._horse_pid = child_pid
|
|
|
|
self.procline('Forked %d at %d' % (child_pid, time.time()))
|
|
|
|
self.procline('Forked %d at %d' % (child_pid, time.time()))
|
|
|
@ -317,29 +308,35 @@ class Worker(object):
|
|
|
|
def perform_job(self, job):
|
|
|
|
def perform_job(self, job):
|
|
|
|
self.procline('Processing %s from %s since %s' % (
|
|
|
|
self.procline('Processing %s from %s since %s' % (
|
|
|
|
job.func.__name__,
|
|
|
|
job.func.__name__,
|
|
|
|
job.origin.name, time.time()))
|
|
|
|
job.origin, time.time()))
|
|
|
|
msg = 'Got job %s from %s' % (
|
|
|
|
msg = 'Got job %s from %s' % (
|
|
|
|
job.description,
|
|
|
|
job.description,
|
|
|
|
job.origin.name)
|
|
|
|
job.origin)
|
|
|
|
self.log.info(msg)
|
|
|
|
self.log.info(msg)
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
rv = job.perform()
|
|
|
|
rv = job.perform()
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
rv = e
|
|
|
|
|
|
|
|
self.log.exception(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fq = self.failure_queue
|
|
|
|
fq = self.failure_queue
|
|
|
|
|
|
|
|
self.log.exception(e)
|
|
|
|
self.log.warning('Moving job to %s queue.' % (fq.name,))
|
|
|
|
self.log.warning('Moving job to %s queue.' % (fq.name,))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Store the exception information...
|
|
|
|
job.ended_at = times.now()
|
|
|
|
job.ended_at = times.now()
|
|
|
|
job.exc_info = e
|
|
|
|
job.exc_info = traceback.format_exc()
|
|
|
|
fq._push(job.pickle())
|
|
|
|
|
|
|
|
|
|
|
|
# ------ REFACTOR THIS -------------------------
|
|
|
|
|
|
|
|
job.save()
|
|
|
|
|
|
|
|
# ...and put the job on the failure queue
|
|
|
|
|
|
|
|
fq.push_job_id(job.id)
|
|
|
|
|
|
|
|
# ------ UNTIL HERE ----------------------------
|
|
|
|
|
|
|
|
# (should be as easy as fq.enqueue(job) or so)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
if rv is not None:
|
|
|
|
self.log.info('Job OK, result = %s' % (rv,))
|
|
|
|
self.log.info('Job result = %s' % (rv,))
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
self.log.info('Job ended normally without result')
|
|
|
|
|
|
|
|
if rv is not None:
|
|
|
|
if rv is not None:
|
|
|
|
p = conn.pipeline()
|
|
|
|
p = conn.pipeline()
|
|
|
|
p.set(job.rv_key, dumps(rv))
|
|
|
|
p.set(job.result, dumps(rv))
|
|
|
|
p.expire(job.rv_key, self.rv_ttl)
|
|
|
|
p.expire(job.result, self.rv_ttl)
|
|
|
|
p.execute()
|
|
|
|
p.execute()
|
|
|
|
|
|
|
|
return True
|
|
|
|