【问题标题】:Symfony 2 CronJobSymfony 2 CronJob
【发布时间】:2015-02-08 17:23:09
【问题描述】:

您好,我在控制器中有一个功能:

if (!function_exists("ssh2_connect"))
die("function ssh2_connect doesn't exist");

if (!($con = ssh2_connect("myipadresherenotshowingtoyouguys", 22))) {
echo "fail: unable to establish connection\n";
} else {
// try to authenticate with username root, password secretpassword
if (!ssh2_auth_password($con, "blablabla", "blablabla!")) {
    echo "fail: unable to authenticate\n";
} else {
    // allright, we're in!
    echo "okay: logged in...\n";

    // execute a command
    $command = 'ssid "Wentzo test2" hide-ssid';
    if (!($stream = ssh2_exec($con, $command))) {
        echo "fail: unable to execute command\n";
    } else {

        $stream2 = ssh2_exec($con, $command_save);
        // collect returning data from command
        stream_set_blocking($stream, true);
        $data = "";
        while ($buf = fread($stream, 4096)) {
            $data .= $buf;
        }

        fclose($stream);
    }
}
}

我想在按下提交按钮时将此脚本作为 cronjob 运行。 但我不知道如何在 symfony 2 中做到这一点。

有人有解决方案吗? 可能是例子?

【问题讨论】:

  • cronjob 是什么意思?您是要运行异步脚本还是要在特定时间连续运行脚本?
  • 假设你想异步运行这个脚本,你应该看看这个symfony.com/doc/current/components/process.html
  • 感谢您的回答。 Process 组件看起来很有趣!我可以为每个执行的命令设置一个计时器吗?

标签: php symfony ssh cron


【解决方案1】:

您正在寻找的概念是排队。

我建议您使用 Beanstalkd、RabbitMQ 或任何其他排队系统来处理此问题。

我们的想法是将处理方法放入 Symfony 命令中: http://symfony.com/doc/current/components/console/introduction.html#creating-a-basic-command

在该命令中,您将从队列中读取,并根据队列中的“作业”运行进程。

在您的控制器中,您唯一要做的就是将新作业推送到该队列中。该作业将包含一些数据,您可以在命令中使用这些数据进行处理。

要进一步了解这个概念,您可以浏览这些幻灯片:http://www.slideshare.net/cakper/2014-0821-symfony-uk-meetup-scaling-symfony2-apps-with-rabbit-mq

您可能还想了解更多有关排队概念的信息。

由于某种原因,我找不到关于 Symfony2 和排队的好教程。

【讨论】:

  • 感谢尼科的回答。会调查一下,结果发现 cronjob 不适合使用它。
猜你喜欢
  • 2015-09-11
  • 2017-07-10
  • 2012-01-17
  • 2015-03-14
  • 2015-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多