This question already has an answer here:
-
How to run cron job with zend framework 2
2 answers
I have two functions in zend framework 2, which fetch me the details of apartments from another site and save them in my data base. (BUT THEY DO SO ONLY WHEN I RUN THE FUNCTIONS).
say the controller name is 'FetchFromSourceController' and the functions are 'index' and 'ical'. those are the names I used.
But my requirement is those two functions should run automatically one after the other when the day begins, with a gap of 10 minutes. i.e. index action should run at 12:00 AM and ical Action should run at 12:10 AM everyday.
I heard that I could use 'cron job' for that purpose, but never used that before. Please tell me where to write the cron job code in ZF2 to run those functions in those particular times.
or atleast give me some tutorial for using cron job in zendframework2. I searched for it.but not find any. Thanks in advance.
ZF2 doesn't have a "cron job" service to automatically run scripts at a specific time.
A Cron Job is a task performed at a certain time and is typically setup at the operating system level. In unix, it's accomplished via the crontab, in windows you can use the task scheduler. There is nothing in ZF2 which will run your scripts at exactly 12:00 and 12:10.
Option #1: Cron Job via Admin Panel
For most hosting environments, you can setup a "cron job" via your website hosting admin panel (such as Plesk or cPanel). There you'll be able to schedule a job by entering in the time and the command to be run. When you create a cron job this way, keep in mind that you'll be entering a command to be run via the command line. Usually this is directly calling php to execute a specific script, but you could also use wget to fetch any URL from the internet. If you directly call php, you'll need to setup a console route as per Zdenek's answer.
/usr/bin/php /path/to/zf2/public/index.php myIndexConsoleRoute
or
wget http://yoursite.com/fetch-from-source/index
Option #2: Setup Crontab via command line
Alternatively, if you have command line access to your server and the proper user permissions, you can setup a crontab via the command line per Zdenek's answer. There is also relevant info here. This is typically the preferred method, but in my opinion it's a bit more involved and can be a challenge to setup and debug, especially if you're not experienced with Unix.
Option #3: Cron Job service
If you're looking for a simpler solution, you might consider one of the cron services out there which will load a specific URL at a given time.
For example, I've used this service in the past:
https://www.setcronjob.com
After you create your account, you simply enter a URL and select the time you'd like the URL to be called. You could easily accomplish your requirements to call the indexAction at 12:00 and icalAction at 12:10 by setting up two jobs, one to run at 12:00 and the other to run at 12:10. SetCronjob has a free plan which might fit your needs, but be aware that they require you to enter in a captcha every 30 days to renew your free account (which is fine, but becomes annoying after awhile).
URL vs Command Line
With "wget" in Option #1 and a Cron Service in Option #3, you'll need to make your indexAction and icalAction available via http. This could be a significant consideration. Even if you add some sort of validation (by checking for a code you send via query string or post), it's still possible that anyone on the internet could trigger these functions if they know the correct 'secret' URL. Having your ical and index methods accessed via http has the additional benefit of you being able to easily call them if you want to manually run these scripts to force an update.
With the CLI approach mentioned by Zdenek, you create a console route in ZF2 and the only way to trigger that route is via the command line. This is typically preferred because your index and ical methods won't be exposed to http, but it's not always an option depending on your hosting setup, and it might not be a significant concern depending on what these functions do to your data and public website.
With Zf2 you can access Controller Actions from command line through CLI, it works really well.
All what you have to do is to add routing to module.config.php
return array(
......
'console' => array(
'router' => array(
'routes' => array(
'synch' => array(
'options' => array(
'route' => 'synch (sites|items):task',
'defaults' => array(
'controller' => 'Application\Controller\Scheduled',
'action' => 'synch'
)
)
),
),
As you can see it's calling Application\Controller\Scheduled controller, synch action, there
public function synchAction()
{
$task = $this->params('task');
try{
switch($task){
case 'sites':
.......
break;
case 'items':
.......
break;
}
Then you can create simple shell script zf2.sh
php /var/www/vhosts/mysite/httpdocs/public/index.php $1 $2
and to crontab add
0 6 * * * /home/admin/scripts/zf2.sh synch sites
or just run from command line
./zf2.sh synch items
It depends your server and his cron system installed.
For example on Debian/Ubuntu it's very easy http://www.pantz.org/software/cron/croninfo.html
You can use this useful module to create cron: https://github.com/heartsentwined/zf2-cron