-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 2-step publication of models to Pub/Sub
**Context** ModelPublisherTask takes a django instance as one of its inputs. However, django models are mutable, so the task payload must be constructed immediately, even if we want to defer publishing. This is actually already the case: the sync() method does this (even though it is actually not affected by this) and the asap() method does this too. **Issues** The payload construction code is not isolated (sync() and asap() duplicate code as of now) and not uniform (asap() and push() have different APIs in a ModelPublisherTask, which violates a principle that applies to most, if not all, other Tasks). Also, in case we want to defer the sync() method to the end of the current django transaction, we have no way of doing so as there is no way to capture the payload and later send it using `transaction.on_commit` **Solution** - We unified the `push` and `asap` method signatures in ModelPublisherTask. That means you can now do `PublisherTask.asap(obj=model)` and also `PublisherTask.push(task_kwargs={'obj': model}, queue='queue-override'}`, for example. This task no longer differs from regular tasks on this regard. - Two-step publication (which is required for Pub/Sub to work inside transactions, by deferring actual calls until the transaction commits) is now possible using the `ModelPublisherTask.prepare()` API. It captures the payload that needs to be sent at the call site, and the process can be finalized by calling either `asap()`, `sync()` or `push()` on the result of this API call. For example: ```python def post_save(sender, instance): publication = ModelPublisherTask.prepare(obj=instance, event="created") transaction.on_commit(lambda: publication.sync()) ```
- Loading branch information
1 parent
1798fed
commit 5b45488
Showing
2 changed files
with
149 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters