Working with custom Task types #163
-
|
Hello, I'm wondering if there's a "correct" way of creating your own Task types that add additional functionalities. I've created my own @dataclass(frozen=True)
class Task(BaseTask[P, T]):
retry: Optional[bool] = DEFAULT_RETRY
max_retries: Optional[int] = DEFAULT_MAX_RETRIES
retry_after: Optional[timedelta] = DEFAULT_RETRY_AFTER
unique: Optional[bool] = FalseBut it's not as easy as it seemed when I read the DEP. I need to create a custom decorator, because it only accepts the I then wrote some tests to check whether my decorator worked, and added override_settings above to TestClass to use my custom backend but because the task decorator is executed immediately this would get the wrong Task class (because the import is above the TestCase). So I had to run the tests with a pre-defined TASK setting, which is fine but I wonder if this is intentional? Shouldn't the task class be created after django is fully started? I wonder if there has been any thoughts about having Task types that add extra functionality and if there are best practices for this. Maybe the decorator could take args & kwargs so that potential Task types could actually handle this. Nevertheless, I got it working fine but was just wondering if there is a "correct" way of doing so. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Hey @RealOrangeOne, Now that it's merged into Django, I want to add it to our roadmap to implement our backend in this. However, I was wondering if there's now a way to "extend" Task types or to achieve what I want to do above. Eg, I want to have task like this; But previously when I tried (as described above), I had to create custom types for all these things: And even with that I had to make more changes I think. |
Beta Was this translation helpful? Give feedback.
Each backend will use the
Tasktype which is defined on it:django-tasks/django_tasks/backends/base.py
Line 32 in 9259c2c
Everything should be pulling it from there. If you need a custom class, you'll need to subclass the backend you're using and override that. A custom task type is only useful if there's backend functionality to go along with it, hence the coupling.