i'm using laravel 5.2. have cron job fetches many thousands of rows database , performs computations on each of rows. cron job takes hour run, database continues larger every day.
one option manually add more cron jobs using crontab -e
(i.e. 5 cron jobs split job 5 smaller parts). however, possible following:
- set single cron job using
crontab -e
. - the cron job script gets rows database and, depending on how many rows returned, determines how many cron jobs should further split into. example, if database returns 1000 rows, should split 10 cron jobs, if database returns 10000 rows, should split 100 cron jobs.
so question is, possible have single cron job listed in crontab -e
, php script decides how many more cron jobs should split (essentially calling more cron jobs php script)?
is safe this? there better way approach this?
here current script:
class postcron extends command { /** * name , signature of console command. * * @var string */ protected $signature = 'post_cron'; /** * console command description. * * @var string */ protected $description = ''; /** * execute console command. * * @return mixed */ public function handle() { $posts = post::all(); // returns ~1000 rows, grows in size daily foreach ($posts $post) { // computations on each separate post } } }
you can create job , run each post queue. refer here laravel queue
so code below
protected $bus; /** * create new command instance. * * @return void */ public function __construct(\illuminate\contracts\bus\dispatcher $bus) { parent::__construct(); $this->bus = $bus; } public function handle() { $posts = post::all(); // returns ~1000 rows, grows in size daily foreach ($posts $post) { // computations on each separate post $this->bus->dispatch(new yourjobclassname($post)); } }