0

我在 myProject/lib/task 下放置了一个 cron 任务。这个 cron 每天工作一年多。

但是现在我必须在我的项目中创建一个按钮,以便在我的客户需要时执行与 cron 相同的过程。代码太复杂了,我无法在正常操作上重写所有内容。

有没有办法从正常操作中调用 cron 任务?

4

1 回答 1

3

以下是如何从操作中调用任务的方法,您只需要添加一个链接就可以了。我在示例中使用了标准的 Symfony 'Clear Cache' 任务,但您可以将其更改为您的:

public function executeRunTaskTest(sfWebRequest $request)
{
  // need to be working in the project root
  chdir(sfConfig::get('sf_root_dir'));

  // init the task we want to run
  $task = new sfCacheClearTask($this->dispatcher, new sfFormatter());

  // run the task
  $task->run(
    array(), // array of arguments
    array(
      'app' => 'frontend',
      'env' => 'prod',
      'type' => 'all',
    ) // array of options
  );

  // back to where we came from
  $this->redirect($request->getReferer());
}

您也可以使用 PHPexec()shell_exec()函数,但这个 Symfony 解决方案可能更简单。:)

于 2013-06-17T19:19:03.963 回答