异步php功能备份数据库

时间:2018-03-23 10:17:48

标签: php asynchronous server cron

我正在使用PHP页面来备份我的数据库,它运行得非常完美。

我的问题(我未来的问题,我更好地说)是我的托管中的cron job仅在响应返回5秒时允许工作功能,否则会失败。

到目前为止,我的数据库非常小,我不确定它需要多长时间,但是自工作以来肯定不到5秒。

但是当我的数据库长大后,我认为问题将会开始。

如何在5秒后让我的功能正常工作?

我会解决异步功能吗?

1 个答案:

答案 0 :(得分:0)

hm,您具有shell访问权限吗? 我做了我的index.php及其称为master类以检测cli模式。像这样:

if( php_sapi_name() === 'fpm-fcgi') {
    define( 'WEB_MODE', true );
    define( 'CLI_MODE', false );
} else if (php_sapi_name() === 'cli') {
    define( 'WEB_MODE', false );
    define( 'CLI_MODE', true );
} else {
    define( 'WEB_MODE', true );
    define( 'CLI_MODE', false );
}

然后在我的由http请求执行的脚本中,我这样做:

$this->asyncShellExecution('/usr/bin/php ' . ROOT_PATH .'/php/index.php -m houseKeeping' );

private function asyncShellExecution(string $str)
{
    exec($str .  " > /dev/null 2>/dev/null &");
}

在cli模式下,我调用新的asyncHouseKeeping()

class asyncHouseKeeping
{
    function __construct()
    {
        $args=getopt("m:t:i:o:l:s:f:");

        if($args['m'] == 'houseKeeping') {
            $this->doHouseKeeping();
        } else {
            exit;
        }
    }
}

那样,在通过xhr从阻止代码中上传图像后,我得到了对CPU和时间要求严格的图像优化过程,之后它又开始运行。

相关问题