def test_run_job_task1(self): """Exercise basics of run_job_task function: no exceptions.""" job = Job.objects.create() self.assertIsNotNone(job) self.assertEqual(job.status, Job.PENDING) self.assertDictEqual(job.get_task_info(), {}) run_job_task(job.id, lambda ajob: JobTaskResult(status=Job.RUNNING)) self.assertEqual(Job.objects.get(pk=job.id).status, Job.RUNNING) run_job_task(job.id, lambda ajob: JobTaskResult(status=Job.FINISHED)) self.assertEqual(Job.objects.get(pk=job.id).status, Job.FINISHED) job.delete()
def test_run_job_task3(self): """Exercise basics of run_job_task function: exception with handler.""" job = Job.objects.create() self.assertIsNotNone(job) self.assertEqual(job.status, Job.PENDING) run_job_task(job.id, JobsTests._comp_with_ex, self._ex_handler) self.assertEqual(Job.objects.get(pk=job.id).status, Job.RUNNING) run_job_task(job.id, JobsTests._comp_with_ex, lambda j, e: JobTaskResult()) self.assertEqual(Job.objects.get(pk=job.id).status, Job.RUNNING) run_job_task(job.id, JobsTests._comp_with_ex, lambda j, e: JobTaskResult(status=Job.FAILED)) self.assertEqual(Job.objects.get(pk=job.id).status, Job.FAILED) job.delete()
def update_it(job): """Updates the database to reflect the state of the evaluation of the given competition submission.""" logger.debug("Entering update_submission_task::update_it (job_id=%s)", job.id) if job.task_type != 'evaluate_submission': raise ValueError("Job has incorrect task_type (job.task_type=%s)", job.task_type) task_args = job.get_task_args() submission_id = task_args['submission_id'] logger.debug("Looking for submission (job_id=%s, submission_id=%s)", job.id, submission_id) submission = CompetitionSubmission.objects.get(pk=submission_id) status = args['status'] logger.debug( "Ready to update submission status (job_id=%s, submission_id=%s, status=%s)", job.id, submission_id, status) result = None try: traceback = None metadata = None if 'extra' in args: if 'traceback' in args['extra']: traceback = args['extra']['traceback'] if 'metadata' in args['extra']: metadata = args['extra']['metadata'] result = update_submission(submission, status, job.id, traceback, metadata) except Exception as e: logger.exception( "Failed to update submission (job_id=%s, submission_id=%s, status=%s)", job.id, submission_id, status) raise SubmissionUpdateException(submission, e) return JobTaskResult(status=result)
def test_run_job_task4(self): """Exercise basics of run_job_task function: with info provided.""" job = Job.objects.create() self.assertIsNotNone(job) self.assertEqual(job.status, Job.PENDING) info1 = { 'key1': 'value1', 'key2': 2 } info2 = { 'key1': 'value1', 'key2': 2, 'key3': 3.3 } run_job_task(job.id, lambda ajob : JobTaskResult(status=Job.RUNNING, info=info1)) j = Job.objects.get(pk=job.id) self.assertEqual(j.status, Job.RUNNING) self.assertDictEqual(j.get_task_info(), info1) run_job_task(job.id, lambda ajob : JobTaskResult(status=Job.FINISHED, info=info2)) j = Job.objects.get(pk=job.id) self.assertEqual(j.status, Job.FINISHED) self.assertDictEqual(j.get_task_info(), info2) job.delete()
def create_it(job): """Handles the actual creation of the competition""" comp_def_id = args['comp_def_id'] logger.info("Creating competition for competition bundle (bundle_id=%s, job_id=%s)", comp_def_id, job.id) competition_def = CompetitionDefBundle.objects.get(pk=comp_def_id) competition = competition_def.unpack() logger.info("Created competition for competition bundle (bundle_id=%s, job_id=%s, comp_id=%s)", comp_def_id, job.id, competition.pk) return JobTaskResult(status=Job.FINISHED, info={'competition_id': competition.pk})
def handle_update_exception(job, ex): """ Handles exception that occur while attempting to update the status of a submission. job: The running Job instance. ex: The exception. The handler tries to acquire the CompetitionSubmission instance from a submission attribute on the exception. """ try: submission = ex.submission _set_submission_status(submission.id, CompetitionSubmissionStatus.FAILED) except Exception: logger.exception("Unable to set the submission status to Failed (job_id=%s)", job.id) return JobTaskResult(status=Job.FAILED)
def create_competition(job_id, comp_def_id): """ A task to create a competition from a bundle with the competition's definition. job_id: The ID of the job. args: A dictionary with the arguments for the task. Expected items are: args['comp_def_id']: The ID of the bundle holding the competition definition. Once the task succeeds, a new competition will be ready to use in CodaLab. """ logger.info("Creating competition for competition bundle (bundle_id=%s)", comp_def_id) competition_def = CompetitionDefBundle.objects.get(pk=comp_def_id) try: competition = competition_def.unpack() result = JobTaskResult(status=Job.FINISHED, info={'competition_id': competition.pk}) update_job_status_task(job_id, result.get_dict()) logger.info("Created competition for competition bundle (bundle_id=%s, comp_id=%s)", comp_def_id, competition.pk) except Exception as e: result = JobTaskResult(status=Job.FAILED, info={'error': str(e)}) update_job_status_task(job_id, result.get_dict())
def _ex_handler(self, ajob, aex): """Sample exception handler""" self.assertIsNotNone(aex.args[0]) self.assertEquals(ajob, aex.args[0]) return JobTaskResult(status=Job.RUNNING)
def echo_it(job): """Echoes the message specified.""" logger.info("Echoing (job id=%s): %s", job.id, args['message']) return JobTaskResult(status=Job.FINISHED)