Listens to completed job

def job_listener(event):
    '''Listens to completed job'''
    job_id = event.job.args[0]

    if event.code == events.EVENT_JOB_MISSED:
        db.mark_job_as_missed(job_id)
    elif event.exception:
        if isinstance(event.exception, util.JobError):
            error_object = event.exception.as_dict()
        else:
            error_object = "\n".join(traceback.format_tb(event.traceback) +
                    [repr(event.exception)])
        db.mark_job_as_errored(job_id, error_object)
    else:
        db.mark_job_as_completed(job_id, event.retval)

    api_key = db.get_job(job_id)["api_key"]
    result_ok = send_result(job_id, api_key)

    if not result_ok:
        db.mark_job_as_failed_to_post_result(job_id)

    # Optionally notify tests that job_listener() has finished.
    if "_TEST_CALLBACK_URL" in app.config:
        requests.get(app.config["_TEST_CALLBACK_URL"])