您可以创建 symfony2 控制台命令“myapp:my-command-name”,它将运行您的 c++ 命令
class MyStandaloneCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->setName('myapp:my-command-name')
->setDescription('Will run standalone c++')
->addArgument('arg1', InputArgument::REQUIRED, 'Some arg');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$arg1 = $input->getArgument('arg1');
$result = shell_exec('nohup my/program/written/in/c++.out '.$arg1.' 2>&1');
$output->writeln('My cool command is started');
return true;
}
}
然后使用 JMSJobBundle
http://jmsyst.com/bundles/JMSJobQueueBundle/master/installation
您可以在其中创建控制台命令队列,例如:
class HomeController ... {
// inject service here
private $cronJobHelper;
// inject EM here
private $em;
public function indexAction(){
$job = $this->cronJobHelper->createConsoleJob('myapp:my-command-name', $event->getId(), 10);
$this->em->persist($job);
$this->em->persist($job);
$this->em->flush();
}
use JMS\JobQueueBundle\Entity\Job;
class CronJobHelper{
public function createConsoleJob($consoleFunction, $params, $delayToRunInSeconds, $priority = Job::PRIORITY_DEFAULT, $queue = Job::DEFAULT_QUEUE){
if(!is_array($params)){
$params = [$params];
}
$job = new Job($consoleFunction, $params, 1, $queue, $priority);
$date = $job->getExecuteAfter();
$date = new \DateTime('now');
$date->setTimezone(new \DateTimeZone('UTC')); //just in case
$date->add(new \DateInterval('PT'.$delayToRunInSeconds.'S'));
$job->setExecuteAfter($date);
return $job;
}
}