def register_schedule_task_restart_database(hostnames): today = date.today() try: databases = Database.objects.filter( databaseinfra__instances__hostname__hostname__in=hostnames ).distinct() for database in databases: print("Checking database {}".format(database.name)) scheudled_tasks = TaskSchedule.objects.filter( status=TaskSchedule.SCHEDULED, database=database, method_path='restart_database') if scheudled_tasks: print("Already scheduled for database {}!".format( database.name)) else: task = TaskSchedule.objects.create( method_path='restart_database', scheduled_for=TaskSchedule.next_maintenance_window( today + timedelta(days=2), database.databaseinfra.maintenance_window, database.databaseinfra.maintenance_day), database=database) task.send_mail(is_new=True) print("Done") except Exception as err: print("Error: {}".format(err))
def test_already_have_task_scheduled(self, create_mock): task_schedule = TaskSchedule() task_schedule.database = self.database task_schedule.scheduled_for = self.one_month_later task_schedule.status = TaskSchedule.SCHEDULED task_schedule.save() check_ssl_expire_at() self.assertFalse(create_mock.called)