def create_job(batch_service_client, job_id, pool_id): """ Creates a job with the specified ID, associated with the specified pool. :param batch_service_client: A Batch service client. :type batch_service_client: `azure.batch.BatchServiceClient` :param str job_id: The ID for the job. :param str pool_id: The ID for the pool. """ logging.info('Creating job [{}]...'.format(job_id)) try: job = batch.models.JobAddParameter( id=job_id, constraints=batchmodels.JobConstraints( max_wall_clock_time=datetime.timedelta(minutes=60), max_task_retry_count=1), on_all_tasks_complete=batchmodels.OnAllTasksComplete.terminate_job, pool_info=batch.models.PoolInformation(pool_id=pool_id)) batch_service_client.job.add(job) except: logging.info( 'Job already exists (or error occurred during creating, todo: handle exception apart from exists check).' ) finally: logging.info('Job creating done.')
def test_batch_jobs(self, **kwargs): client = self.create_sharedkey_client(**kwargs) # Test Create Job auto_pool = models.AutoPoolSpecification( pool_lifetime_option=models.PoolLifetimeOption.job, pool=models.PoolSpecification( vm_size='small', cloud_service_configuration=models.CloudServiceConfiguration( os_family='5' ) ) ) job_prep = models.JobPreparationTask(command_line="cmd /c \"echo hello world\"") job_release = models.JobReleaseTask(command_line="cmd /c \"echo goodbye world\"") job_param = models.JobAddParameter( id=self.get_resource_name('batch_job1_'), pool_info=models.PoolInformation( auto_pool_specification=auto_pool ), job_preparation_task=job_prep, job_release_task=job_release ) response = client.job.add(job_param) self.assertIsNone(response) # Test Update Job constraints = models.JobConstraints(max_task_retry_count=3) options = models.JobUpdateParameter( priority=500, constraints=constraints, pool_info=models.PoolInformation( auto_pool_specification=auto_pool ) ) response = client.job.update(job_param.id, options) self.assertIsNone(response) # Test Patch Job options = models.JobPatchParameter(priority=900) response = client.job.patch(job_param.id, options) self.assertIsNone(response) job = client.job.get(job_param.id) self.assertIsInstance(job, models.CloudJob) self.assertEqual(job.id, job_param.id) self.assertEqual(job.constraints.max_task_retry_count, 3) self.assertEqual(job.priority, 900) # Test Create Job with Auto Complete job_auto_param = models.JobAddParameter( id=self.get_resource_name('batch_job2_'), on_all_tasks_complete=models.OnAllTasksComplete.terminate_job, on_task_failure=models.OnTaskFailure.perform_exit_options_job_action, pool_info=models.PoolInformation( auto_pool_specification=auto_pool ) ) response = client.job.add(job_auto_param) self.assertIsNone(response) job = client.job.get(job_auto_param.id) self.assertIsInstance(job, models.CloudJob) self.assertEqual(job.on_all_tasks_complete, models.OnAllTasksComplete.terminate_job) self.assertEqual(job.on_task_failure, models.OnTaskFailure.perform_exit_options_job_action) # Test List Jobs jobs = client.job.list() self.assertIsInstance(jobs, models.CloudJobPaged) self.assertEqual(len(list(jobs)), 2) # Test Disable Job response = client.job.disable(job_param.id, models.DisableJobOption.requeue) self.assertIsNone(response) # Test Enable Job response = client.job.enable(job_param.id) self.assertIsNone(response) # Prep and release task status task_status = client.job.list_preparation_and_release_task_status(job_param.id) self.assertIsInstance(task_status, models.JobPreparationAndReleaseTaskExecutionInformationPaged) self.assertEqual(list(task_status), []) # Test Terminate Job response = client.job.terminate(job_param.id) self.assertIsNone(response) # Test Delete Job response = client.job.delete(job_auto_param.id) self.assertIsNone(response) # Test Job Lifetime Statistics stats = client.job.get_all_lifetime_statistics() self.assertIsInstance(stats, models.JobStatistics) self.assertEqual(stats.num_succeeded_tasks, 0) self.assertEqual(stats.num_failed_tasks, 0)
def test_batch_job_schedules(self, **kwargs): client = self.create_aad_client(**kwargs) # Test Create Job Schedule schedule_id = self.get_resource_name('batch_schedule_') job_spec = models.JobSpecification( pool_info=models.PoolInformation("pool_id"), constraints=models.JobConstraints(max_task_retry_count=2), on_all_tasks_complete=models.OnAllTasksComplete.terminate_job ) schedule = models.Schedule( start_window=datetime.timedelta(hours=1), recurrence_interval=datetime.timedelta(days=1) ) params = models.JobScheduleAddParameter( schedule_id, schedule, job_spec ) response = client.job_schedule.add(params) self.assertIsNone(response) # Test List Job Schedules schedules = list(client.job_schedule.list()) self.assertTrue(len(schedules) > 0) # Test Get Job Schedule schedule = client.job_schedule.get(schedule_id) self.assertIsInstance(schedule, models.CloudJobSchedule) self.assertEqual(schedule.id, schedule_id) self.assertEqual(schedule.state, models.JobScheduleState.active) # Test Job Schedule Exists exists = client.job_schedule.exists(schedule_id) self.assertTrue(exists) # Test List Jobs from Schedule jobs = list(client.job.list_from_job_schedule(schedule_id)) self.assertTrue(len(jobs) > 0) # Test Disable Job Schedule response = client.job_schedule.disable(schedule_id) self.assertIsNone(response) # Test Enable Job Schedule response = client.job_schedule.enable(schedule_id) self.assertIsNone(response) # Test Update Job Schedule job_spec = models.JobSpecification( pool_info=models.PoolInformation('pool_id') ) schedule = models.Schedule( recurrence_interval=datetime.timedelta(hours=10) ) params = models.JobScheduleUpdateParameter(schedule, job_spec) response = client.job_schedule.update(schedule_id, params) self.assertIsNone(response) # Test Patch Job Schedule schedule = models.Schedule( recurrence_interval=datetime.timedelta(hours=5) ) params = models.JobSchedulePatchParameter(schedule) response = client.job_schedule.patch(schedule_id, params) self.assertIsNone(response) # Test Terminate Job Schedule response = client.job_schedule.terminate(schedule_id) self.assertIsNone(response) # Test Delete Job Schedule response = client.job_schedule.delete(schedule_id) self.assertIsNone(response)