Add support to celery beat through custom Scheduler
s
#65
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
As suggested in the documentation and in #9, one can set a dispatching task as a proxy to run tasks within all tenant contexts in order to work with
beat
and periodic tasks. This is a simple solution which might be applicable in smaller or new projects, but might be something you want to avoid in bigger projects.We did not want to rewrite all the scheduled tasks to depend on a dispatching task, so we looked into how we could change the behaviour of beat without touching the configuration or the tasks and came up with the idea of subclassing
Scheduler
to control just that.This PR adds a
TenantAwareSchedulerMixin
that is used to createTenantAwareScheduler
andTenantAwarePersistentScheduler
(celery
's built-in schedulers), which are responsible for dispatching the task to all tenants. Now, switching to a tenant-aware dispatching mechanism only involves selecting the specific scheduler in the invocation tocelery beat
with the--scheduler
flag (seeREADME.md
).In addition, we also wanted to enable some tasks only for some tenants - though our use case is a bit different -, which comes in handy when you want to run a task on the
public
schema. For that matter, we also added aTenantAwareSchedulerEntry
class that allows users to set a new configtenant_schemas
in theirapp.conf.beat_schedule
or similar.