I would wrap your streaming logic into Symfony console command and launch the daemon with upstart or bash script. You can interact with daemon using tasks in Redis or Mysql.
UPD: Create Task entity in AppBundle/Entity folder:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table
*/
class Task
{
/**
* @ORM\Id
* @ORM\Column(type="bigint")
* @ORM\GeneratedValue(strategy="AUTO")
* @var int
*/
private $id;
/**
* @ORM\Column(type="text")
* @var string
*/
private $fileName;
/**
* @var boolean
* @ORM\Column(type="string", length=50)
*/
protected $status = 'new';
/**
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @param string $status
*/
public function setStatus($status)
{
$this->status = $status;
}
/**
* @return string
*/
public function setStatus($status)
{
return $this->status;
}
}
Update database schema and ensure that table has been created:
app/console doctrine:schema:update --force
Create Symfony Command.
namespace AppBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class VideoDaemonCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->setName('app:video-daemon');
}
/** @var EntityManager */
private $em;
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
while (true) {
$task = $this->em->getRepository('AppBundle:Task')->findOneBy(['status' => 'new']);
if (is_null($task)) {
sleep(5);
continue;
}
$this->setTaskStatus('processing', $task);
$this->processTask($task);
}
}
private function processTask($task) {
//here do streaming logic
$this->setTaskStatus('completed', $task);
}
private function setTaskStatus($status, $task) {
$task->setStatus($status);
$this->em->persist($task);
$this->em->flush();
}
}
Lets create a daemon with upstart(debian/ubuntu). Place the following content to into /etc/init/video-daemon.conf and restart computer.
start on filesystem or runlevel [2345]
stop on runlevel [!2345]
### Keep the process alive, limit to 5 restarts in 60s
respawn
respawn limit 5 60
### Start daemon
script
exec /path/to/your/project/app/console app:video-daemon
end script
Check if daemon works:
ps -aux | grep video-daemon
Now if everything gone well you can create tasks in Symfony controllers/services by:
$task = new \AppBundle\Entity\Task();
$em->persist($task);
$em->flush();