Sonata – Intergrate Supervisor in Dashboard

Step 1: install Supervisor bundle

Add in composer.json:

    "require": {
        "yzalis/supervisor-bundle": "1.0.*@dev"

Update composer:

# composer update

Add Bundle in AppKernel.php:

    $bundles = array(
        // ...
        new YZ\SupervisorBundle\YZSupervisorBundle(),
        // ...

Step 2: configure Supervisor bundle

Add the bundle config in config.yml:

                host: preprod.mediabot.cppg.dom
                username: default
                password: default
                port: 9000

To be redirected to the dashboard without overloading methods (start button and stop button), add in the routing:

    resource: "@YZSupervisorBundle/Resources/config/routing.xml"
    prefix: /admin

Here we declare a new service with a certain id associated with a class and one injects the methods supervisor.manager service.
Add in admin.xml file:

    <service id="sonata.block.service.supervisor" class="Acme\AdminBundle\Service\SupervisorBlockService">
        <tag name="sonata.block"/>
        <argument type="service" id="templating" />
        <call method="setSupervisorManager">
            <argument type="service" id="supervisor.manager" />

To create supervisor service, create SupervisorBlockService.php file, and add:

# src/Acme/AdminBundle/Service/SupervisorBlockService.php

namespace Acme\AdminBundle\Service;

use Symfony\Component\HttpFoundation\Response;

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Validator\ErrorElement;

use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\BlockBundle\Block\BaseBlockService;
use YZ\SupervisorBundle\Manager\SupervisorManager;

class SupervisorBlockService extends BaseBlockService
    protected $supervisorManager;
    public function getName()
        return 'Supervisor';
    public function setSupervisorManager($supervisorManager)
        $this->supervisorManager = $supervisorManager;

    public function execute(BlockContextInterface  $blockContext, Response $response = null)
        $settings = $blockContext->getSettings();

        return $this->renderResponse('AcmeAdminBundle:Supervisor:list.html.twig', array(
            'supervisors' => $this->supervisorManager->getSupervisors(),
            'block'     => $blockContext->getBlock(),
            'settings'  => $settings
            ), $response);

It remains for us to create the associated view. Create src/Acme/AdminBundle/Resources/Supervisor/view/list.html.twig, and add:

# src/Acme/AdminBundle/Resources/Supervisor/list.html.twig
{% block yz_supervisor_content %}
    <div class="box" style="padding:2%;">
        {% include "YZSupervisorBundle:Supervisor:list_content.html.twig" %}
{% endblock yz_supervisor_content %}

Finally we need to incorporate this view in the dashboard, for this, add the config.yml file:

    // ...
	// ...
              contexts: [admin]
	// ...
            - { position: left, type: sonata.admin.block.admin_list }
            - { position: right, type: sonata.block.service.supervisor }

If we update the dashboard, you should normally see the supervisor alongside the admin list.

Step 3: Solve timeout problem

Now we need to solve the problem of timeout (error raised when starting an order program), for this we will overload the supervisor.manager service
Create SupervisorManager.php file, and copy the contents of the original service in vendor/yzalis/supervisor-bundle/YZ/SupervisorBundle/Manager/

use Supervisor/Supervisor


use  Acme\AdminBundle\Extension\Supervisor

Create Supervisor.php file in Acme\AdminBundle\Extension, and copy the contents of the original service in vendor/yzalis/supervisor/src/Supervisor/Supervisor.php

use Supervisor/Process

Change the __construct method of the class, adding the bold line:

public function __construct($name, $ipAddress, $username = null, $password = null, $port = null)
    $this->name = $name;

    $this->rpcClient = new RpcClient('http://'.$ipAddress.':'.$port.'/RPC2/');

    if ($username != null and $password != null) {
        $this->rpcClient->getHttpClient()->setAuth($username, $password, HttpClient::AUTH_BASIC);
        'timeout'      => 30
    $this->createKey($ipAddress, $username, $password, $port);

This last line change the timeout to 30 seconds, which allows time for supervisor to restart programs.
Finaly in services.yml add:

    // ...
        class: Acme\AdminBundle\Extension\SupervisorManager
        arguments: ["%supervisor.servers%"]

Raphaël has written 45 articles

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>