BalsamJobStatus.py 1.74 KB
Newer Older
1

jtchilders's avatar
jtchilders committed
2 3
import logging,sys
logger = logging.getLogger(__name__)
4
from common import Serializer
jtchilders's avatar
jtchilders committed
5 6 7 8 9 10

class SerializeFailed(Exception): pass
class DeserializeFailed(Exception): pass

class BalsamJobStatus:
   def __init__(self,job=None,message=None):
Michael Salim's avatar
Michael Salim committed
11 12
      '''Constructed with a BalsamJob, but only contains simple id,
      serialized_job, and message attributes'''
13
      self.job_id          = None
jtchilders's avatar
jtchilders committed
14 15 16 17 18 19
      self.serialized_job  = None
      self.message         = message
      if job is not None:
         self.set_job(job)

   def set_job(self,job):
20
      self.job_id = job.job_id
jtchilders's avatar
jtchilders committed
21
      try:
22
         self.serialized_job = job.serialize()
23
      except Exception as e:
jtchilders's avatar
jtchilders committed
24 25 26
         logger.exception('serialize failed: ' + str(job.__dict__))
         raise SerializeFailed('Received exception while serializing BalsamJob')

27
   def get_job(self,job):
jtchilders's avatar
jtchilders committed
28 29
      if self.serialized_job is not None:
         try:
30 31
            job.deserialize(self.serialized_job)
            return job
32
         except Exception as e:
jtchilders's avatar
jtchilders committed
33 34 35 36 37 38 39
            logger.exception('deserialize failed: ' + str(self.serialized_job))
         raise DeserializeFailed('Received exception while deserializing BalsamJob')
      return None

   def serialize(self):
      try:
         return Serializer.serialize(self.__dict__)
40
      except Exception as e:
jtchilders's avatar
jtchilders committed
41 42 43 44 45 46
         logger.exception('serialize failed: ' + str(self.__dict__))
         raise SerializeFailed('Received exception while serializing BalsamJobStatus: ' + str(e))

   def deserialize(self,text):
      try:
         self.__dict__ = Serializer.deserialize(text)
47
         self.job_id = int(str(self.job_id))
48
      except Exception as e:
jtchilders's avatar
jtchilders committed
49 50
         logger.exception('deserialize failed')
         raise DeserializeFailed('Received exception while deserializing BalsamJobStatus: ' + str(e))