Skip to content

Technical Proposal: Support for Inheriting PeriodicTask to Provide More Custom Fields #904

Open
@NeverStopDreamingWang

Description

@NeverStopDreamingWang

Background

If we need to add custom fields, currently we can only associate with the PeriodicTask model manually.

Existing Problems

Data Redundancy: Need to maintain relationships between two models
Complex Queries: Business queries require cross-table joins, affecting performance
Data Consistency: The lifecycle of two models needs to be synchronized manually
Code Complexity: Creating and deleting tasks requires handling operations on multiple models

Proposed Solution

Could you provide an inheritable model that contains the fields required by the Celery Beat scheduler?
For example:

class AbstractPeriodicTask(models.Model):
    task = models.CharField(
        max_length=200,
        verbose_name='Task Name',
        help_text=_('The Name of the Celery Task that Should be Run.  '
                    '(Example: "proj.tasks.import_contacts")'),
    )
    # Other fields...
    class Meta:
        abstract = True

Then provide a method to register my custom model with django_celery_beat.
This approach would not affect the internal implementation of django_celery_beat while enabling more custom fields.

Benefits

  1. Simplified Architecture: Single model inheritance instead of complex relationships
  2. Better Performance: Eliminates the need for JOIN operations
  3. Enhanced Flexibility: Easy to add business-specific fields
  4. Reduced Complexity: Simplified CRUD operations
  5. Maintained Compatibility: Preserves all Celery Beat functionality

Implementation Suggestion

# Abstract base model provided by django_celery_beat
class AbstractPeriodicTask(models.Model):
    # All current PeriodicTask fields
    name = models.CharField(max_length=200, unique=True)
    task = models.CharField(max_length=200)
    interval = models.ForeignKey(IntervalSchedule, ...)
    crontab = models.ForeignKey(CrontabSchedule, ...)
    # ... other fields
    
    class Meta:
        abstract = True

# User's custom implementation
class CustomPeriodicTask(AbstractPeriodicTask):
    network = models.ForeignKey(NetWorkModel, ...)
    node = models.ForeignKey(NodeModel, ...)
    custom_field = models.CharField(max_length=100)
    
    class Meta:
        db_table = "custom_periodic_task"

# Registration method
from django_celery_beat import register_periodic_task_model
register_periodic_task_model(CustomPeriodicTask)

Expected Outcome

This enhancement would provide developers with greater flexibility while maintaining the robustness and functionality of django_celery_beat's scheduling system.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions