Implementing and executing cron jobs

Sometimes, an application requires some background tasks such as re-generating a sitemap or refreshing statistics. A common way to implement this is by using cron jobs. When using Yii, there are two ways to do this, which are as follows:

  • Emulate the browser to call the web application controller action
  • Use the command-line command to run as a job

In this recipe, we will see how to implement both. For our recipe, we will implement writing the current timestamp into a timestamp.txt file under the protected directory.

Getting ready

Create a fresh application by using yiic webapp.

How to do it...

Carry out the following steps:

  1. Create protected/controllers/CronController.php as follows:
    <?php
    class CronController extends CController
    {
      public function actionIndex()
      {
        $filename = Yii::getPathOfAlias("application")."/timestamp.txt";
        file_put_contents($filename, time());
      }
    }
  2. Now we need a way to call it. As it is a web application controller, we need to somehow emulate a browser. In Linux, you can use one of the following commands in your crontab file:
    GET http://example.com/index.php?r=cron
    wget -O - http://example.com/index.php?r=cron
    lynx --dump http://example.com/index.php?r=cron >/dev/null
    

    Note

    When we use a controller in this way, we need to make sure that it is used only as a cron job. For example, we can check for a value of a specific $_GET variable.

  3. Create protected/commands/CronCommand.php as follows:
    <?php
    class CronCommand extends CConsoleCommand
    {
      public function run($args)
      {
        $filename = Yii::getPathOfAlias("application")."/timestamp.txt";
        file_put_contents($filename, time());
      }
    }
  4. We can use the following in the crontab file to execute it:
    /path/to/./yiic cron

    Note

    Note that the full path is required to be specified in cron.

How it works...

GET, wget, and lynx fetch a page through HTTP, so we can use these to trigger the normal web application execution. This method has its pros and cons. The main pro is that we are executing a web application, so the environment is the same as in all other application parts. The main con is that it is not completely secure. Even if we use the $_GET variable as a password, there is no guarantee that it will remain undisclosed forever.

The second method is much more secure because the console command can be executed only if one has SSH access to the server. The con is that the environment is different from the web application.

There's more...

Another way to solve a problem is to use a message queue. Most implementations will deal with concurrency issues for you and will allow you to do the processing almost immediately if there are not too many requests. Implementations to check are:

Further reading

In order to learn more about Yii console applications, refer to the following URL:

http://www.yiiframework.com/doc/guide/en/topics.console

See also

  • The Creating CLI commands recipe in Chapter 8, Extending Yii
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.200.66