pythondjangodjango-cron

Strange line in django_cron


I'm using this instruction http://django-cron.readthedocs.org/en/latest/installation.html and can't understand meaning of "a unique code" line.

from django_cron import CronJobBase, Schedule

class MyCronJob(CronJobBase):
    RUN_EVERY_MINS = 120 # every 2 hours

    schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
    code = 'my_app.my_cron_job'    # a unique code

    def do(self):
        pass    # do your thing here

Can anyone explain me what this line do?

code = 'my_app.my_cron_job'    # a unique code

Solution

  • Looking at code here:

    def make_log(self, *messages, **kwargs):
        cron_log = self.cron_log
    
        cron_job      = getattr(self, 'cron_job', self.cron_job_class)
        cron_log.code = cron_job.code
    

    we can understand, that this "unique code" denote particular cron task. Every time your cron task is executed, CronJobLog instance is created with cron_log.code = cron_job.code.

    So, it is possible to filter the logs, that belongs to particular task:

    last_job = CronJobLog.objects.filter(code=cron_job.code).latest('start_time')
    

    That is why it must be unique, to not mix logs from one cron task with another. I suppose it has the same purpose as id, but this code has meaningful value.