How do I setup a cron job a script that is part of a zend framework project

后端 未结 3 1319
难免孤独
难免孤独 2021-02-20 10:58

I have a zendframework project for which i need to run a script periodically to upload the content of a folder and another do download. The script itself is ready but I am strug

3条回答
  •  星月不相逢
    2021-02-20 11:28

    What I did was create a mini app called Scripts here is the structure:

    Scripts
     -- Jobs
     -- Builds
     Abstract.php //abstract each job / build inherit from
     Bootstrap.php
     Index.php
    

    I then use php /path/to/Scripts/Index.php --j=folderScript or /path/to/Scripts/Index.php --b=folderScript for a build script

    I then overload the main zend_application bootstrap run method like so

    public function run()
    {
        $opts = new Zend_Console_Getopt(array(
                    'build|b-s' => 'Cron Build Script to Run',
                    'job|j-s' => 'Cron Job to run',
                    'params|p-s' => 'Parameters to set',
                ));
    
        $opts->parse();
    
        $front = $this->getResource('FrontController');
    
        if (true === isset($opts->params)) {
            parse_str($opts->params, $params);
            $front->setParams($params);
        }
    
        $filter = new Zend_Filter_Word_CamelCaseToUnderscore();
    
        if (true === isset($opts->build)) {
            $build = $filter->filter($opts->build);
            $className = sprintf('Scripts_Build_%s', ucfirst($build));
        } else if (true === isset($opts->job)) {
            $job = $filter->filter($opts->job);
            $className = sprintf('Scripts_Jobs_%s', ucfirst($job));
        } else {
            $className = 'Scripts_Jobs_Dynamic';
        }
    
        if (false === class_exists($className)) {
            throw new Exception('Class "' . $className . '" Does Not Exist');
        } else {
            $front->setParam('bootstrap', $this);
    
            $logger = $this->initLogger();
    
            $fp = fopen(DATA_PATH . '/locks/' . md5($className), 'w+');
    
            if (!flock($fp, LOCK_EX | LOCK_NB)) {
                $logger->info('Already Running');
                exit(0);
            }
    
            $logger->Subject($className);
    
            fwrite($fp, time());
    
            try {
                $class = new $className();
    
                $class->setLogger($logger)
                        ->init()
                        ->run();
    
                $logger->done('Operation Completed');
            } catch (Exception $e) {
                $logger->err($e->getMessage() . ' ' . $e->getTraceAsString());
                $logger->done('done with error');
            }
    
            flock($fp, LOCK_UN);
        }
    }
    

    Hope that gets you started, I looked at using a controller / module within ZF but seemed like too much overhead

    Symfony uses a good approach with their app/console as well, could be worth looking into their setup

提交回复
热议问题