procrastinate: App.import_paths not working
I would like to invoke a worker process(es) using the CLI invocation, a la:
procrastinate --app=app.procrastinate.app worker
However, we also would like to “hook in” to the app initialization in order to do some setup work (e.g. DB connection pool, etc.).
That setup work is illustrated in this example procrastinate.py module:
import asyncio
from procrastinate import AiopgConnector, App
from app.settings import database, DATABASE_URL
app = App(
connector=AiopgConnector(dsn=DATABASE_URL),
import_paths=['app.reconciler'],
)
async def main():
await app.open_async()
await database.connect()
await app.run_worker_async(
delete_jobs='successful',
)
if __name__ == "__main__":
print('SCRIPT MODE')
print('__name__ == "__main__"')
asyncio.run(main())
This allows me to run the main() function which does my setup work before initializing the worker. However this requires me to execute this process as a Python script - python -m app.procrastinate - which is fine, except my reconcile task no longer seems to be imported/loadable by my worker:
(⎈ minikube:cloud)➜ dev git:(mc/cloud-worker) ✗ k logs -n cloud deployments/cloud-app -c cloud-app-worker -f
SCRIPT MODE
__name__ == "__main__"
Task was not found: Task at reconcile cannot be imported: reconcile is not a valid path
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/procrastinate/tasks.py", line 14, in load_task
task = utils.load_from_path(path, Task)
File "/usr/local/lib/python3.9/site-packages/procrastinate/utils.py", line 27, in load_from_path
raise exceptions.LoadFromPathError(f"{path} is not a valid path")
procrastinate.exceptions.LoadFromPathError: reconcile is not a valid path
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/procrastinate/worker.py", line 175, in process_job
await self.run_job(job=job, worker_id=worker_id)
File "/usr/local/lib/python3.9/site-packages/procrastinate/worker.py", line 240, in run_job
task = self.load_task(task_name=task_name, worker_id=worker_id)
File "/usr/local/lib/python3.9/site-packages/procrastinate/worker.py", line 222, in load_task
task = tasks.load_task(task_name)
File "/usr/local/lib/python3.9/site-packages/procrastinate/tasks.py", line 16, in load_task
raise exceptions.TaskNotFound(f"Task at {path} cannot be imported: {str(exc)}")
procrastinate.exceptions.TaskNotFound: Task at reconcile cannot be imported: reconcile is not a valid path
When I run the process via CLI, then App.import_paths are honored.
When I run the process as a Python script, App.import_paths does not seem to be honored.
Any suggestions? Thank you! 😄
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 22 (22 by maintainers)
If you have the opportunity to test the PR on #463 and confirm it solves the issue for you, that would be great 😃