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

Schedule Cron Tasks Post-Startup #197

Closed
dradux opened this issue Jul 24, 2020 · 4 comments
Closed

Schedule Cron Tasks Post-Startup #197

dradux opened this issue Jul 24, 2020 · 4 comments

Comments

@dradux
Copy link

dradux commented Jul 24, 2020

Is there a way to add cron tasks post-startup? The docs show how to add a cron job on startup via cron_jobs of WorkerSettings but I dont see a way to add jobs after startup of the worker.

My scenario:

  • fastapi app
  • arq worker
    My app needs to create cron jobs in arq. For example if a new user is created then create a cron job that updates their feeds every 10 minutes.
@dradux
Copy link
Author

dradux commented Aug 5, 2020

For those who have a similar scenario to mine (needing a dynamic scheduler rather than a static scheduler defined on startup of arq) - I solved my issue by using apscheduler. This is a common path to address the issue; however, I have a circumstance which is worth noting. I wanted to leverage the async nature and scalabiilty of arq; however, this is not simple with apscheduler as you need a single apscheduler and apscheduler needs to use its AsyncIOScheduler which is difficult if you need to use apscheduler's RPC (RPyC) server.

My solution was to create a fastapi + apscheduler (using AsyncIOScheduler) - fastapi exposes apscheduler as an API and apscheduler in turn creates arq tasks. Hope the info is useful to others out there looking for a more robust scheduling mechanism to arq.

FWIW I do not think this logic should be in arq, using an external service works well and provides its own benefits. RQ has a similar design with rq-scheduler providing scheduling features in much the same way. If anyone needs code on this let me know as I'd be happy to share.

@dradux dradux closed this as completed Aug 5, 2020
@mtsewrs
Copy link

mtsewrs commented Sep 10, 2020

@dradux Looking to do this as well, would love for you to share the code!

@wackazong
Copy link

@dradux I would be eager to see that code as well if possible.

@dradux
Copy link
Author

dradux commented Mar 17, 2021

A full project using apscheduler + arq + fastapi can be seen here: https://gitlab.com/drad/radicchio. You'll likely be interested in things in the api directory. Of particular interest is _scheduler where apscheduler logic is located and _worker where the arq worker is located. Let me know if there are any questions - be happy to explain further.

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

No branches or pull requests

3 participants