Esempio n. 1
0
 def test_edit_schedule_of_running_job(self, client, admin_session):
     """You cannot edit job schedule if the job is running."""
     job = Job.get_job_by_key('admin_emails')
     job_history = JobHistory.job_started(job_key=job.key)
     self._api_job_update_schedule(
         client,
         expected_status_code=400,
         job_id=Job.get_job_by_key('admin_emails').id,
         schedule_type='minutes',
         schedule_value=3,
     )
     JobHistory.job_finished(id_=job_history.id, failed=False)
Esempio n. 2
0
 def test_edit_schedule_of_enabled_job(self, client, admin_session):
     """You cannot edit job schedule if the job is enabled."""
     with enabled_job(job_key=AdminEmailsJob.key()):
         self._api_job_update_schedule(
             client,
             expected_status_code=400,
             job_id=Job.get_job_by_key('admin_emails').id,
             schedule_type='minutes',
             schedule_value=3,
         )
Esempio n. 3
0
    def test_authorized(self, client, admin_session):
        """Admin can access available jobs."""
        job = Job.get_job_by_key('admin_emails')
        expected_value = not job.disabled
        api_json = self._api_job_disable(client,
                                         job_id=job.id,
                                         disable=expected_value)
        assert api_json['disabled'] is expected_value
        std_commit(allow_test_environment=True)

        # Reset the value
        expected_value = not expected_value
        api_json = self._api_job_disable(client,
                                         job_id=job.id,
                                         disable=expected_value)
        assert api_json['disabled'] is expected_value
        std_commit(allow_test_environment=True)
Esempio n. 4
0
    def run(self, force_run=False):
        with self.app_context():
            job = Job.get_job_by_key(self.key())
            if job:
                current_instance_id = os.environ.get('EC2_INSTANCE_ID')
                job_runner_id = fetch_job_runner_id()

                if job.disabled and not force_run:
                    app.logger.warn(
                        f'Job {self.key()} is disabled. It will not run.')

                elif current_instance_id and current_instance_id != job_runner_id:
                    app.logger.warn(
                        f'Skipping job because current instance {current_instance_id} is not job runner {job_runner_id}'
                    )

                elif JobHistory.is_job_running(job_key=self.key()):
                    app.logger.warn(
                        f'Skipping job {self.key()} because an older instance is still running'
                    )

                else:
                    app.logger.info(f'Job {self.key()} is starting.')
                    job_tracker = JobHistory.job_started(job_key=self.key())
                    try:
                        self._run()
                        JobHistory.job_finished(id_=job_tracker.id)
                        app.logger.info(
                            f'Job {self.key()} finished successfully.')

                    except Exception as e:
                        JobHistory.job_finished(id_=job_tracker.id,
                                                failed=True)
                        summary = f'Job {self.key()} failed due to {str(e)}'
                        app.logger.error(summary)
                        app.logger.exception(e)
                        send_system_error_email(
                            message=
                            f'{summary}\n\n<pre>{traceback.format_exc()}</pre>',
                            subject=f'{summary[:50]}...'
                            if len(summary) > 50 else summary,
                        )
            else:
                raise BackgroundJobError(
                    f'Job {self.key()} is not registered in the database')
Esempio n. 5
0
 def test_authorized(self, client, admin_session):
     """Admin can edit job schedule."""
     job = Job.get_job_by_key('admin_emails')
     api_json = self._api_job_update_schedule(
         client,
         job_id=job.id,
         schedule_type='minutes',
         schedule_value=3,
     )
     assert api_json['schedule'] == {
         'type': 'minutes',
         'value': 3,
     }
     api_json = self._api_job_update_schedule(
         client,
         job_id=job.id,
         schedule_type='day_at',
         schedule_value='15:30',
     )
     assert api_json['schedule'] == {
         'type': 'day_at',
         'value': '15:30',
     }