Create cronjob with Zend Framework

前端 未结 13 2193
广开言路
广开言路 2020-12-13 14:32

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

13条回答
  •  情书的邮戳
    2020-12-13 15:05

    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
    

提交回复
热议问题