0

I'm using Django 1.5.5.

Say I have an object as such:

class Encounter(model.Models):
    date = models.DateTimeField(blank=True, null=True)

How can I detect when a given Encounter has reached current time ? I don't see how signals can help me.

Mathieu Marques
  • 1,678
  • 15
  • 33

2 Answers2

2

You can't detect it using just Django.

You need some scheduler, that will check every Encounters date (for example, by using corresponding filter query), and do needed actions.

It can be a simple cron script. You can write it as django custom management command. And cron must call it every 5 minute, for example.

Or, you can use Celery. With it, you can see worker status from admin and do some other things.

stalk
  • 11,934
  • 4
  • 36
  • 58
  • I didn't think this was possible. If I'm not mistaken I could create a custom management command such as `validatebets` (the user bets on encounters and the earnings should be awarded when the encounter has been played) and then run this command every hours with Celery. Am I right ? – Mathieu Marques Sep 18 '14 at 13:17
  • Not exactly. If you'll choose celery, you don't need to create management command, as you just create a [periodic task](http://celery.readthedocs.org/en/latest/userguide/periodic-tasks.html). Management command will be useful, if you choose simple cron. All cron can do is just call repeatedly your command. And in that command you'll have to write all logic. – stalk Sep 18 '14 at 13:24
  • Alright I didn't know what was Cron as first. In a bit of a hurry, this feature (amongst others) is due in 24 hours, I will - for now - write this as a Cron script as I believe it to be the easiest/fastest way for a local environment. But, why should I prefer using Celery (often heard of it, but never heard of using Cron) ? Is it all about needing or not a task queueing system ? – Mathieu Marques Sep 18 '14 at 13:51
1

What you could do is use Celery. When you save an object of Encounter this would then get into the task queue and execute only once it has reached current time.

There is one caveat though, it might execute a bit later depending on how busy the celery workers are.

Jonathan
  • 8,453
  • 9
  • 51
  • 74