Susana Santos
Susana Santos

Reputation: 322

Manually call a cron task Symfony 1.4

I have a cron task placed under myProject/lib/task. This cron works daily for over a year.

However now I have to create a button inside my project to do the same process as the cron whenever my client needs. The code is too complex and I can't rewrite everything on a normal action.

Is there a way to call a cron task from a normal action?

Upvotes: 0

Views: 1552

Answers (1)

moote
moote

Reputation: 126

Here's how you can call a task from an action, you just need to add a link to it and you'll be good. I've used the standard Symfony 'Clear Cache' task in the example, but you can change it to yours:

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());
}

You could also use the PHP exec() or shell_exec() functions, but this Symfony solution is probably easier. :)

Upvotes: 3

Related Questions