iro/model/job.py
branchdevel
changeset 245 4526747a42ca
parent 218 91f87ad13540
child 258 0a5eb5aac0be
equal deleted inserted replaced
244:48fba7b4afe9 245:4526747a42ca
     1 from twisted.python import log
     1 from twisted.python import log, threadable
     2 from twisted.python import threadable
       
     3 
     2 
     4 from datetime import datetime
     3 from datetime import datetime
     5 from collections import MutableMapping
     4 from collections import MutableMapping
     6 
     5 
     7 import schema
     6 import schema
    19         self.recipients = recipients
    18         self.recipients = recipients
    20         self.offers = offers
    19         self.offers = offers
    21         self.tasks={}
    20         self.tasks={}
    22         self.c = 0
    21         self.c = 0
    23         self.status = "started"
    22         self.status = "started"
       
    23         log.msg("Job(%s) created."%(self.dbjob))
    24 
    24 
    25     def addTask(self,task):
    25     def addTask(self,task):
    26         self.tasks[task.recipient] = task
    26         self.tasks[task.recipient] = task
    27 
    27 
    28     def incStatus(self):
    28     def incStatus(self):
    35             return
    35             return
    36         elif self.status == "sended" and status != "error":
    36         elif self.status == "sended" and status != "error":
    37             return
    37             return
    38         job.status = status
    38         job.status = status
    39         self.status = status
    39         self.status = status
       
    40         log.msg("Job(%s) status changed to: %s."%(self.dbjob, status))
    40         session.commit()
    41         session.commit()
    41 
    42 
    42     @dbdefer
    43     @dbdefer
    43     def setStatus(self, session, task, status):
    44     def setStatus(self, session, task, status):
    44         c = self.incStatus()
    45         c = self.incStatus()
    45         job = schema.Job.get(session, self.dbjob)
    46         job = schema.Job.get(session, self.dbjob)
    46         
    47         
    47         if job.status in ["started","init"]:
       
    48             self._status(session,"sending")
       
    49         if c == len(self.recipients):
    48         if c == len(self.recipients):
    50             self._status(session,"sended") 
    49             self._status(session,"sended") 
       
    50         elif job.status in ["started","init"]:
       
    51             self._status(session,"sending")
    51 
    52 
    52         if status.costs > 0:
    53         if status.costs > 0:
    53             o = schema.Offer.get(session, status.provider.name, status.route, self.message.typ)
    54             o = schema.Offer.get(session, status.provider.name, status.route, self.message.typ)
    54             job.messages.append(schema.Message(price=status.costs, isBilled=False, recipient=str(task.recipient), count=status.count, exID=status.exID, date=datetime.today(), offer=o))
    55             job.messages.append(schema.Message(price=status.costs, isBilled=False, recipient=str(task.recipient), count=status.count, exID=status.exID, date=datetime.today(), offer=o))
    55         session.commit()
    56         session.commit()