Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I report status from a worker? #87

Open
codecowboy opened this issue Mar 13, 2014 · 5 comments
Open

How do I report status from a worker? #87

codecowboy opened this issue Mar 13, 2014 · 5 comments
Labels

Comments

@codecowboy
Copy link
Contributor

I've written a worker and can fire this from a dummy gearman client. Which methods allow me to report job status? I want to report status as the job progresses.

I see that status callback is provided as a kernel event but am not sure how to implement this. Do I need to write an Event listener? Does it matter if my worker is not defined as a service? Is there an example anywhere?

@mmoreram mmoreram added the RFC label Mar 13, 2014
@mmoreram
Copy link
Owner

Maybe this process could be documented.

@codecowboy
Copy link
Contributor Author

Definitely and I am happy to do it once I understand the process better. Please can you provide the necessary steps for implementing and using an status callback event listener e.g.

  1. Create an event listener class as follows:
<?php
namespace My\Bundle\AdminBundle\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\DependencyInjection\ContainerAware;

class UpdateImageSizeEventListener extends ContainerAware


{
    public function onStatus(\Mmoreram\GearmanBundle\Event\GearmanClientCallbackStatusEvent $event)
    {
        $logger = $this->container->get('logger');

        $response = new Response();
        $task = $event->getGearmanTask();

        $logger->info('this ran');

        $response->setContent($task->taskDenominator(), $task->taskNumerator());
        // Send the modified response object to the event
        $event->setResponse($response);

    }
}
  1. In your worker class, send status periodically with $job->sendStatus(($i*1000),$denominator);
  2. in your worker set Status callback to ????? //please fill this in for me Marc

@mmoreram
Copy link
Owner

Ok, good first aprox. Some hints :)

  • Never, I repeat, Never, inject all the container if you don't really need to. In this case, you need to inject logger in the constructor.
  • To ensure the code lines are not longer than 80 chars, set Event namespace with a use statement.
  • Following the Symfony2 Coding Standards, code should be documented as needed, to beauty the code and to make it understandable to others.
  • Because logger must be injected, just Cast it with its interface and remove ContainerAware extends.
<?php

/**
 * My project 2014
 */

namespace My\Bundle\AdminBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\DependencyInjection\ContainerAware;
use Psr\Log\LoggerInterface;
use Mmoreram\GearmanBundle\Event\GearmanClientCallbackStatusEvent;

/**
 * EventListener for UpdateImageSize
 */
class UpdateImageSizeEventListener
{
    /**
     * @var LoggerInterface
     * 
     * Logger
     */
    protected $logger;

    /**
     * Construct method
     * 
     * @param LoggerInterface $logger Logger
     */
    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /**
     * Subscriber to onStatus event
     * 
     * @param GearmanClientCallbackStatusEvent $event Event
     *                                                
     * @return UpdateImageSizeEventListener self Object
     */
    public function onStatus(GearmanClientCallbackStatusEvent $event)
    {
        $this->logger->info('this ran');        
        $task = $event->getGearmanTask();

        $response = new Response();
        $response->setContent($task->taskDenominator(), $task->taskNumerator());
        $event->setResponse($response);
    }
}

@mmoreram
Copy link
Owner

BTW, I'll se what are you asking me to fill ...

@codecowboy
Copy link
Contributor Author

@mmoreram any update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants