0

I want to check an item in my database every x minutes for y minutes after its creation.

I've come up with two ways to do this and I'm not sure which will result in better efficiency/speed.

The first is to store a Date field in the model and do something like

Model.find({time_created > current_time - y})

inside of a cron job every x minutes.

The second is the keep a times_to_check field that keeps track of how many more times, based on x and y, the object should be checked.

Model.find({times_to_check> 0})

My thought on why these two might be comparable is because the first comparison of Dates would take longer, but the second one requires a write to the database after the object had been checked.

db2791
  • 1,040
  • 6
  • 17
  • 30

1 Answers1

0

So either way you are going have to check the database continuously to see if it is time to query your collection. In your "second solution" you do not have a way to run your background process as you are only referencing how you are determining your collection delta.

Stick with running you unix Cron job but make sure it is fault tolerant an have controls ensuring it is actually running when you application is up. Below is a pretty good answer for how to handle that.

How do I write a bash script to restart a process if it dies?

Based on that i would ask how does your application react if your Cron job has not run for x number of minutes, hours or days. How will your application recover if this does happen?

Scott
  • 174
  • 1
  • 13