I'm using Laravel 5.2. I currently have a cron job where it fetches many thousands of rows from my database and performs computations on each of those rows. The cron job currently takes roughly an hour to run, but the database continues to get larger every day.
One option would be to manually add more cron jobs using crontab -e
(i.e. 5 cron jobs that split the job into 5 smaller parts). However, is it also possible to do something like the following:
- Set up a single cron job using
crontab -e
. - The cron job script gets all the rows from the database and, depending on how many rows are returned, it determines how many cron jobs it should further be split into. For example, if the database returns 1000 rows, it should split it into 10 cron jobs, but if the database returns 10000 rows, it should split it into 100 cron jobs.
So my question is, is it possible to have only a single cron job listed in crontab -e
, where the PHP script then decides how many more cron jobs it should be split into (essentially calling more cron jobs from a PHP script)?
Is it safe to do this? Is there a better way to approach this?
Here is my current script:
class PostCron extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'post_cron';
/**
* The console command description.
*
* @var string
*/
protected $description = '';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$posts = Post::all(); // currently returns ~1000 rows, but grows in size daily
foreach ($posts as $post) {
// computations on each separate post
}
}
}