def __init__(self, job_id, sqs_queue_name): """Initialize new JobMonitor :param job_id: string; Elastic Transcoder job ID to monitor :param sqs_queue_name: string; Name of SQS queue subscribed to receive notifications """ # Each JobMonitor has its own SqsWorker to monitor notifications self._sqs_worker = SqsWorker(job_id, sqs_queue_name) self._job_id = job_id
class JobMonitor: """Monitors the SQS notifications received for an Elastic Transcoder job""" def __init__(self, job_id, sqs_queue_name): """Initialize new JobMonitor :param job_id: string; Elastic Transcoder job ID to monitor :param sqs_queue_name: string; Name of SQS queue subscribed to receive notifications """ # Each JobMonitor has its own SqsWorker to monitor notifications self._sqs_worker = SqsWorker(job_id, sqs_queue_name) self._job_id = job_id def start(self): """Have the SqsWorker start monitoring notifications""" self._sqs_worker.start() def stop(self): """Instruct the SqsWorker to stop monitoring notifications If this occurs before the job has finished, the monitoring of notifications is aborted, but the Elastic Transcoder job itself continues. """ self._sqs_worker.stop() def finished(self): return self._sqs_worker.finished() def status(self): return self._sqs_worker.job_status() def wait_for_completion(self): """Block until the job finishes""" while not self.finished(): time.sleep(5) # Stop the SqsWorker self.stop() def __repr__(self): return f'JobMonitor(Job ID: {self._job_id}, Status: {self.status().name})'