1

I have a "Contest" model that a user creates and one of the fields is

(endTime = models.DateField(default=datetime.now()+timedelta(days=7)).

I need a method to run a function when their contest has expired. The function would be to notify users, update objects, etc.

What would be the best method to achieve this?

manchakowski
  • 127
  • 1
  • 1
  • 11
  • You just want to notify the creator that their contest has expired? Do you want to send them an email or just a notification when they are on your site? – rrao Jun 13 '16 at 17:09
  • Well there will be a big chunk of code that will need to be ran that will update many objects. Basically just looking for the best procedure to identify that the datetime field has passed. – manchakowski Jun 13 '16 at 17:13
  • And this can't be based on a user logging in or any user behaviour. This has to run in the background somehow. – manchakowski Jun 13 '16 at 17:14
  • For example, how do sites like eBay run all of the "ending auction" code when the auction ends? Schedule a task everytime auction is created I suppose. What would be the easiest way to do this? – manchakowski Jun 13 '16 at 17:28

1 Answers1

1

To schedule a task you can look here

as for a this question,

procedure to identify that the datetime field has passed

This would work:

Contest.objects.filter(endTime__lte=timezone.now())

So you would have a task running continuously that would call a django command every X hours, that would use the above search to find any expired contests.

Community
  • 1
  • 1
rrao
  • 629
  • 4
  • 13
  • That method would work for scheduling it based on a predetermined date rather than a DateField for each particular "contest". Not looking to "schedule" a task, looking for a task to run based on a "DateField". – manchakowski Jun 13 '16 at 17:26
  • I suppose you could schedule a task everytime a contest is created...hmm. – manchakowski Jun 13 '16 at 17:29
  • You would have a task that runs continuously in the background, that would call a django command that searches the database using: `Contest.objects.filter(endTime__lte=timezone.now())` and it would call this command every _X_ hours. – rrao Jun 13 '16 at 17:32
  • Do you think the continuous task in the background would be superior to scheduling a task everytime a contest is created? – manchakowski Jun 13 '16 at 17:53
  • That's a separate question, but in short, yes that would be better. – rrao Jun 13 '16 at 17:57
  • The main question is the best way to do this! But ok thanks! – manchakowski Jun 13 '16 at 18:08