| 
									
										
										
										
											2024-01-02 15:29:18 +08:00
										 |  |  | from datetime import timedelta | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | from celery import Task, Celery | 
					
						
							|  |  |  | from flask import Flask | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def init_app(app: Flask) -> Celery: | 
					
						
							|  |  |  |     class FlaskTask(Task): | 
					
						
							|  |  |  |         def __call__(self, *args: object, **kwargs: object) -> object: | 
					
						
							|  |  |  |             with app.app_context(): | 
					
						
							|  |  |  |                 return self.run(*args, **kwargs) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     celery_app = Celery( | 
					
						
							|  |  |  |         app.name, | 
					
						
							|  |  |  |         task_cls=FlaskTask, | 
					
						
							|  |  |  |         broker=app.config["CELERY_BROKER_URL"], | 
					
						
							|  |  |  |         backend=app.config["CELERY_BACKEND"], | 
					
						
							|  |  |  |         task_ignore_result=True, | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2023-05-17 15:40:21 +08:00
										 |  |  |      | 
					
						
							|  |  |  |     # Add SSL options to the Celery configuration | 
					
						
							|  |  |  |     ssl_options = { | 
					
						
							|  |  |  |         "ssl_cert_reqs": None, | 
					
						
							|  |  |  |         "ssl_ca_certs": None, | 
					
						
							|  |  |  |         "ssl_certfile": None, | 
					
						
							|  |  |  |         "ssl_keyfile": None, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     celery_app.conf.update( | 
					
						
							|  |  |  |         result_backend=app.config["CELERY_RESULT_BACKEND"], | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2023-05-17 15:40:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if app.config["BROKER_USE_SSL"]: | 
					
						
							|  |  |  |         celery_app.conf.update( | 
					
						
							|  |  |  |             broker_use_ssl=ssl_options,  # Add the SSL options to the broker configuration | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |          | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     celery_app.set_default() | 
					
						
							|  |  |  |     app.extensions["celery"] = celery_app | 
					
						
							| 
									
										
										
										
											2024-01-02 15:29:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     imports = [ | 
					
						
							|  |  |  |         "schedule.clean_embedding_cache_task", | 
					
						
							|  |  |  |         "schedule.clean_unused_datasets_task", | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     beat_schedule = { | 
					
						
							|  |  |  |         'clean_embedding_cache_task': { | 
					
						
							|  |  |  |             'task': 'schedule.clean_embedding_cache_task.clean_embedding_cache_task', | 
					
						
							|  |  |  |             'schedule': timedelta(minutes=1), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         'clean_unused_datasets_task': { | 
					
						
							|  |  |  |             'task': 'schedule.clean_unused_datasets_task.clean_unused_datasets_task', | 
					
						
							|  |  |  |             'schedule': timedelta(minutes=10), | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     celery_app.conf.update( | 
					
						
							|  |  |  |         beat_schedule=beat_schedule, | 
					
						
							|  |  |  |         imports=imports | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     return celery_app |