def main(): configure_logging() while True: try: rb = TaskScheduler() logger.info('Worker initialized, waiting for jobs') rb.consume_jobs(worker_cb) except pika.exceptions.ConnectionClosed: logger.warning('RabbitMQ not present, retrying') time.sleep(1) continue
def _start(self): id = str(uuid.uuid4()) sched = TaskScheduler() sched.post_task(id) return '''<html> <body> <h1>Task created</h1> <p><a href="/?id=%s">Check task status.</a></p> <script> setTimeout(function() { location.href = "/?id=%s"; }, 4000) </script> </body> </html>''' % (id, id)
def worker_cb(job_id): logger.info('Received task id %s', job_id) r = TaskScheduler() for t in range(100): status = r.update_task(job_id, random.randint(0, 10)) logger.debug('Processing task id %s, total work %d', job_id, status) if status >= 1000: logger.debug('Task %s cancelled', job_id) break if status > 100: logger.debug('Task %s completed', job_id) break time.sleep(1)
def _check(self, id): r = TaskScheduler() status = r.check_task(id) return '''<html> <body> <h1>Task %s</h1> <p>%s</p> <p><a href="/cancel?id=%s">Cancel task</a></p> <script> setTimeout(function() { location.reload(); }, 5000); </script> </body> </html>''' % (id, status, id)
def cancel(self, id): r = TaskScheduler() r.cancel_task(id) return '''<html><body><script>location.href = "/?id=%s";</script> ''' % (id)