Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execute a one-time job, manually #197

Open
kevinrenskers opened this issue Jan 23, 2025 · 3 comments
Open

Execute a one-time job, manually #197

kevinrenskers opened this issue Jan 23, 2025 · 3 comments
Labels

Comments

@kevinrenskers
Copy link

I use django-apscheduler with a runapscheduler.py manage.py command script, where I have a bunch of scheduled jobs defined that run periodically. All works perfectly fine.

Now I have a task that should only ever be executed manually, never periodically. The idea is that I add a button somewhere in the Django Admin that our marketing manager can press, which will then add a job to the queue, to be executed once, as soon as possible.

Can I use django-apscheduler to run one-off jobs like this?

I know there’s an example in the README of triggering a job via the DjangoJob admin page, but we don’t include this page in our admin site. I want to add a button somewhere else that should add this job to the queue.

Do I have to create my own DjangoMemoryJobStore and add the job to it without a trigger? Does the job already have to exist in my main runapscheduler.py scheduler? But how can I create a job there that never runs? How do I shutdown the scheduler when the single job completes?

Or rather than creating a separate BackgroundScheduler instance, can I not add this job to the already running BlockingScheduler, part of runapscheduler.py manage command?

Is there an example of executing one single piece of code in the background, one time? Can I even use django-apscheduler for this? I'd rather not add another background task runner into the mix just for this one use case 😬

@kevinrenskers
Copy link
Author

Or rather than creating a separate BackgroundScheduler instance, can I not add this job to the already running BlockingScheduler, part of runapscheduler.py manage command?

Ah, I guess this is not really possible due to what #155 also ran into. So then I do have to create a brand new scheduler and start it from within my view, and make sure it shuts down properly again when the job finishes. It seems quite complex just to run a single task?

@jcass77
Copy link
Owner

jcass77 commented Jan 24, 2025

It seems quite complex just to run a single task?

Yes.

And then you also have to contend with users clicking the same button multiple times (or multiple users in multiple browsers doing the same) without being able to force single-threaded execution.

Assuming the job can be run in less than 30 seconds it might be best to just run the logic in your view directly.

@kevinrenskers
Copy link
Author

I thought that the button getting clicked multiple times wouldn't be a big problem since jobs have an ID and I can check if such a job is already in the queue.

But it's also not really a problem if the job does end up in the queue multiple times.

Sadly the job takes about a minute to run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants