mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 05:05:42 +00:00 
			
		
		
		
	try/catch for operational error
- Database might not yet be ready to load models
This commit is contained in:
		| @@ -135,41 +135,49 @@ class ScheduleMixin: | |||||||
|         Register the tasks with the database |         Register the tasks with the database | ||||||
|         """ |         """ | ||||||
|  |  | ||||||
|         from django_q.models import Schedule |         try: | ||||||
|  |             from django_q.models import Schedule | ||||||
|  |  | ||||||
|         for key, task in self.scheduled_tasks.items(): |             for key, task in self.scheduled_tasks.items(): | ||||||
|  |  | ||||||
|             task_name = self.get_task_name(key) |                 task_name = self.get_task_name(key) | ||||||
|  |  | ||||||
|             # If a matching scheduled task does not exist, create it! |                 # If a matching scheduled task does not exist, create it! | ||||||
|             if not Schedule.objects.filter(name=task_name).exists(): |                 if not Schedule.objects.filter(name=task_name).exists(): | ||||||
|  |  | ||||||
|                 logger.info(f"Adding scheduled task '{task_name}'") |                     logger.info(f"Adding scheduled task '{task_name}'") | ||||||
|  |  | ||||||
|                 Schedule.objects.create( |                     Schedule.objects.create( | ||||||
|                     name=task_name, |                         name=task_name, | ||||||
|                     func=task['func'], |                         func=task['func'], | ||||||
|                     schedule_type=task['schedule'], |                         schedule_type=task['schedule'], | ||||||
|                     minutes=task.get('minutes', None), |                         minutes=task.get('minutes', None), | ||||||
|                     repeats=task.get('repeats', -1), |                         repeats=task.get('repeats', -1), | ||||||
|                 ) |                     ) | ||||||
|  |         except OperationalError: | ||||||
|  |             # Database might not yet be ready | ||||||
|  |             pass | ||||||
|  |  | ||||||
|     def unregister_tasks(self): |     def unregister_tasks(self): | ||||||
|         """ |         """ | ||||||
|         Deregister the tasks with the database |         Deregister the tasks with the database | ||||||
|         """ |         """ | ||||||
|  |  | ||||||
|         from django_q.models import Schedule |         try: | ||||||
|  |             from django_q.models import Schedule | ||||||
|  |  | ||||||
|         for key, task in self.scheduled_tasks.items(): |             for key, task in self.scheduled_tasks.items(): | ||||||
|  |  | ||||||
|             task_name = self.get_task_name(key) |                 task_name = self.get_task_name(key) | ||||||
|  |  | ||||||
|             try: |                 try: | ||||||
|                 scheduled_task = Schedule.objects.get(name=task_name) |                     scheduled_task = Schedule.objects.get(name=task_name) | ||||||
|                 scheduled_task.delete() |                     scheduled_task.delete() | ||||||
|             except Schedule.DoesNotExist: |                 except Schedule.DoesNotExist: | ||||||
|                 pass |                     pass | ||||||
|  |         except OperationalError: | ||||||
|  |             # Database might not yet be ready | ||||||
|  |             pass | ||||||
|  |  | ||||||
|  |  | ||||||
| class UrlsMixin: | class UrlsMixin: | ||||||
|   | |||||||
| @@ -301,7 +301,6 @@ class PluginsRegistry: | |||||||
|         logger.info('Activating plugin tasks') |         logger.info('Activating plugin tasks') | ||||||
|  |  | ||||||
|         from common.models import InvenTreeSetting |         from common.models import InvenTreeSetting | ||||||
|         from django_q.models import Schedule |  | ||||||
|  |  | ||||||
|         # List of tasks we have activated |         # List of tasks we have activated | ||||||
|         task_keys = [] |         task_keys = [] | ||||||
| @@ -323,17 +322,23 @@ class PluginsRegistry: | |||||||
|  |  | ||||||
|         # Remove any scheduled tasks which do not match |         # Remove any scheduled tasks which do not match | ||||||
|         # This stops 'old' plugin tasks from accumulating |         # This stops 'old' plugin tasks from accumulating | ||||||
|         scheduled_plugin_tasks = Schedule.objects.filter(name__istartswith="plugin.") |         try: | ||||||
|  |             from django_q.models import Schedule | ||||||
|  |  | ||||||
|         deleted_count = 0 |             scheduled_plugin_tasks = Schedule.objects.filter(name__istartswith="plugin.") | ||||||
|  |  | ||||||
|         for task in scheduled_plugin_tasks: |             deleted_count = 0 | ||||||
|             if task.name not in task_keys: |  | ||||||
|                 task.delete() |  | ||||||
|                 deleted_count += 1 |  | ||||||
|  |  | ||||||
|         if deleted_count > 0: |             for task in scheduled_plugin_tasks: | ||||||
|             logger.info(f"Removed {deleted_count} old scheduled tasks") |                 if task.name not in task_keys: | ||||||
|  |                     task.delete() | ||||||
|  |                     deleted_count += 1 | ||||||
|  |  | ||||||
|  |             if deleted_count > 0: | ||||||
|  |                 logger.info(f"Removed {deleted_count} old scheduled tasks") | ||||||
|  |         except OperationalError: | ||||||
|  |             # Database might not yet be ready | ||||||
|  |             pass | ||||||
|  |  | ||||||
|     def deactivate_integration_schedule(self): |     def deactivate_integration_schedule(self): | ||||||
|         pass |         pass | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user