Create cronjob with Zend Framework

2020-02-08 02:47发布

I am trying to write a cronjob controller, so I can call one website and have all modules cronjob.php executed. Now my problem is how do I do that?

Would curl be an option, so I also can count the errors and successes?

[Update]

I guess I have not explained it enough.

What I want to do is have one file which I can call like from http://server/cronjob and then make it execute every /application/modules/*/controller/CronjobController.php or have another way of doing it so all the cronjobs aren't at one place but at the same place the module is located. This would offer me the advantage, that if a module does not exist it does not try to run its cronjob.

Now my question is how would you execute all the modules CronjobController or would you do it a completly different way so it still stays modular?

And I want to be able to giveout how many cronjobs ran successfully and how many didn't

13条回答
何必那么认真
2楼-- · 2020-02-08 03:20

You could set up a database table to hold references to the cronjob scripts (in your modules), then use a exec command with a return value on pass/fail.

查看更多
叼着烟拽天下
3楼-- · 2020-02-08 03:21

My solution:

  • curl /cron
  • Global cron method will include_once all controllers
  • Check whether each of the controllors has ->cron method
  • If they have, run those.

Public cron url (for curl) is not a problem, there are many ways to avoid abuse. As said, checking remote IP is the easiest.

查看更多
Root(大扎)
4楼-- · 2020-02-08 03:23

Take a look at zf-cli:

This handles well all cron jobs.

查看更多
等我变得足够好
5楼-- · 2020-02-08 03:23

It doesn't make sense to run the bootstrap in the same directory or in cron job folder. I've created a better and easy way to implement the cron job work. Please follow the below things to make your work easy and smart:

  1. Create a cron job folder such as "cron" or "crobjob" etc. whatever you want.

  2. Sometimes we need the cron job to run on a server with different interval like for 1 hr interval or 1-day interval that we can setup on the server.

  3. Create a file in cron job folder like I created an "init.php", Now let's say you want to send a newsletter to users in once per day. You don't need to do the zend code in init.php.

  4. So just set up the curl function in init.php and add the URL of your controller action in that curl function. Because our main purpose is that an action should be called on every day. for example, the URL should be like this:

https://www.example.com/cron/newsletters

So set up this URL in curl function and call this function in init.php in the same file.

In the above link, you can see "cron" is the controller and newsletters is the action where you can do your work, in the same way, don't need to run the bootstrap file etc.

查看更多
Luminary・发光体
6楼-- · 2020-02-08 03:25

This is my way to run Cron Jobs with Zend Framework

In Bootstrap I will keep environment setup as it is minus MVC:

public static function setupEnvironment()
{
     ...
     self::setupFrontController();
     self::setupDatabase();
     self::setupRoutes();
     ...
     if (PHP_SAPI !== 'cli') { 
          self::setupView();
          self::setupDbCaches();
     }
     ...
}

Also in Bootstrap, I will modify setupRoutes and add a custom route:

public function setupRoutes()
{   
    ...
    if (PHP_SAPI == 'cli') { 
        self::$frontController->setRouter(new App_Router_Cli());
        self::$frontController->setRequest(new Zend_Controller_Request_Http());        
    }
}

App_Router_Cli is a new router type which determines the controller, action, and optional parameters based on this type of request: script.php controller=mail action=send. I found this new router here: Setting up Cron with Zend Framework 1.11 :

class App_Router_Cli extends Zend_Controller_Router_Abstract 
{
    public function route (Zend_Controller_Request_Abstract $dispatcher) 
    {
        $getopt = new Zend_Console_Getopt (array());
        $arguments = $getopt->getRemainingArgs();
        $controller = "";
        $action = "";
        $params = array();

        if ($arguments) {

            foreach($arguments as $index => $command) {

                $details = explode("=", $command);

                if($details[0] == "controller") {
                    $controller = $details[1];
                } else if($details[0] == "action") {
                    $action = $details[1];
                } else {
                    $params[$details[0]] = $details[1];
                }
            }

            if($action == "" || $controller == "") {
                die("Missing Controller and Action Arguments == You should have: 
                     php script.php controller=[controllername] action=[action]");
            }
            $dispatcher->setControllerName($controller);
            $dispatcher->setActionName($action);
            $dispatcher->setParams($params);

            return $dispatcher;
        }
        echo "Invalid command.\n", exit;
        echo "No command given.\n", exit;
    }

    public function assemble ($userParams, $name = null, $reset = false, $encode = true)
    {
        throw new Exception("Assemble isnt implemented ", print_r($userParams, true));
    }
}

In CronController I do a simple check:

public function sendEmailCliAction()
{   
    if (PHP_SAPI != 'cli' || !empty($_SERVER['REMOTE_ADDR'])) { 
        echo "Program cannot be run manually\n";
        exit(1);
    } 
    // Each email sent has its status set to 0;

Crontab runs a command of this kind:

    * * * * * php /var/www/projectname/public/index.php controller=name action=send-email-cli >> /var/www/projectname/application/data/logs/cron.log
查看更多
乱世女痞
7楼-- · 2020-02-08 03:32

Do you have filesystem access to the modules' directories? You could iterate over the directories and determine where a CronjobController.php is available. Then you could either use Zend_Http_Client to access the controller via HTTP or use an approach like Zend_Test_PHPUnit: simulate the actual dispatch process locally.

查看更多
登录 后发表回答