Jump to content

Avoiding Cron Jobs


Recommended Posts

What does everyone think about cron jobs? When should you use them? When should you NOT use them? Is it better to avoid them when alternatives are possible? How can they be avoided?

When I started my site, animals were aged up with a cron job. I found this to cause many problems because it was a large amount of data to update and, when it failed, this was a huge deal for players. I recoded the site to handle aging with dates. That eliminated the problems we used to have with aging. 

I'm now wondering what else would be better off without cron jobs? I have to recode our shops (for other reasons) and wondered if I should recode it in a way that doesn't use crons. Though, it's not such a big deal when the shop cron job gets skipped. 

Has anyone else had experiences with dealing with alternatives to cron jobs? What things should and should you not use crons for?

Edited by Hare
  • Like 1
Link to comment
Share on other sites

I think Cron jobs are fab provided they are utilised correctly.

For example with NPC Shops:
A good way to save on resources would be to utilise the users pageview to restock the shop.

You could add a column for each NPC shop with a timestamp of when a shop should restock next. When loading the shop title etc check this value to see if this timestamp has passed. If it has, perform a restock of that particular shop and then update that timestamp for the next time the shop should update.

The benefit of this is that if there is a shop that gets visited only a few times a day, it wont be using resources every x minutes when the cron would run. It also means no additional resources when checking, because when a user views the shop, you will be retrieving the NPC Shop title name anyway, so it wouldnt be any additional drain on the system to get the timestamp too :)

You could even then use that timestamp to show the user: "Next Restock:" - "in 5 minutes", "in 3 minutes" "in less than 1 minute!"

  • Like 2
Link to comment
Share on other sites

When you're working with large data sets it might be a good option to run a queue worker.

You could set up a cron job that creates jobs for a queue worker.

An example case would be sending over X amount of animals to be processed at a time.

So lets say you have 3000 animals that need to age, you could paginate that (let's say into 300 animals), pass the IDs to a Job that would do the aging and just repeat until you hit the 3000.

This is actually quite common when using modern frameworks, take a look at https://github.com/chrisboulton/php-resque if you're not using one :)

Having said that, how are your crons being called actually? I've seen a lot of 'wget' tutorials floating around the web, it might be handier to run them through the command line, or increase the script timeout limit.

Edited by HappyDemon
  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...
18 hours ago, Hare said:

Thanks for your input! I just finished a recode of our shops using Nate's timestamp suggestion. Much more resource friendly than a cron. And that is good to know, HappyDemon!

Glad I could help :) Anything else you are planning to de-cron?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...