+from sercom.model import InstanciaDeEntrega, hub
+from sercom.finalizer import Finalizer
+from threading import Thread
+from datetime import datetime
+import time
+import logging
+
+log = logging.getLogger('sercom.tester')
+
+class Queue(object): #{{{
+ def __init__(self):
+ self.go_on = True
+ def get(self):
+ while self.go_on:
+ try:
+ hub.begin()
+ try:
+ select = InstanciaDeEntrega.selectBy(inicio_proceso=None)
+ instancia = select.orderBy(InstanciaDeEntrega.q.fin)[0]
+ instancia.inicio_proceso = datetime.now()
+ finally:
+ hub.commit()
+ return instancia.id
+ except IndexError:
+ log.debug(_(u'No hay instancias de entrega sin finalizar'))
+ time.sleep(30) # TODO config?
+ except Exception, e:
+ if isinstance(e, SystemExit):
+ raise
+ log.exception('Queue: ')
+ time.sleep(30) # TODO config?
+ return None
+#}}}
+
+q = Queue()
+finalizer = Finalizer(name='juanca', queue=q)
+t = Thread(name='juanca', target=finalizer.run)
+t.start()
+