I switched prefork pool to eventlet. In my case my termination method not more working and rising this error:
django.db.utils.DatabaseError: DatabaseWrapper objects created in a thread can only be used in that same thread. The object with alias ‘default’ was created in thread id 139661231139648 and this is thread id 139661089517376.
Using django-db as result backend. Termination Method:
def terminate(self):
"""
Terminate a service with bot_code
"""
task_name = f"{self.bot_code}_instance"
try:
task = PeriodicTask.objects.get(name=task_name)
task.enabled = False
task.save()
celery_task_id = TaskController.get_celery_task_id_by(search_field="task_name",
search_value=task.task, status="STARTED")
if celery_task_id:
current_app.control.revoke(celery_task_id, terminate=True)
return Response({'message': 'Task terminated successfully.'}, status=status.HTTP_200_OK)
except PeriodicTask.DoesNotExist:
return Response({'error': 'Task does not exist.'}, status=status.HTTP_404_NOT_FOUND)