michelsalib / BCCResqueBundle

The BCC resque bundle provides integration of php-resque to Symfony2. It is inspired from resque, a Redis-backed Ruby library for creating background jobs, placing them on multiple queues, and processing them later.
122 stars 91 forks source link

Job fails to get container if the queue is created from different host and has different kernel root dir. #96

Open cocomode18 opened 10 years ago

cocomode18 commented 10 years ago

I have separate hosts, one that creates the queue, and other that processes the worker and job. When each server has different project root directories, it fails to get the container in ContainerAwareJob class.

I've found out why, but can't get this to work unless I hard code the root_dir in my project that create's the worker (which I don't like to do). Is there any suggestions to fix this??

related codes below

// ContainerAwareJob.php
public function setKernelOptions(array $kernelOptions)
{
    $this->args = \array_merge($this->args, $kernelOptions);
}
// ContainerAwareJob.php
protected function createKernel()
{
    /**/
     $finder->name('*Kernel.php')->depth(0)->in($this->args['kernel.root_dir']);
    /**/
}
cocomode18 commented 10 years ago

update

temporary, I solved it by adding a new config param that defines the worker server app_root_dir.

# app/config/config.yml
bcc_resque:
    worker:
        root_dir: 'path/to/root'
// DependencyInjection/Configuration.php
public function getConfigTreeBuilder()
{
    /**/
    $rootNode
        ->addDefailtsIfNotSet()
        ->children()
                ->arrayNode('worker')
                    ->info('Worker Server configuration')
                    ->addDefaultsIfNotSet()
                    ->children()
                        ->scalarNode('root_dir')
                            ->defaultValue('%kernel.root_dir%')
                            ->cannotBeEmpty()
                            ->info('The root dir of worker registered app')
                        ->end()
                    ->end()
                ->end()
    /**/
}
// DependencyInjection/BCCResqueExtension.php
public function load(array $configs, ContainerBuilder $container {
  /**/
  if (!empty($config['worker']['root_dir'])) {
           $container->setParameter('bcc_resque.worker.root_dir', $config['worker']['root_dir']);
  }
  /**/
# Resources/config/services.xml  line20
<!--<argument key="kernel.root_dir">%kernel.root_dir%</argument>-->
<argument key="kernel.root_dir">%bcc_resque.worker.root_dir%</argument>

It would be better if I can just get the root_dir of the worker server without setting any configurations.

michelsalib commented 10 years ago

Hi @cocomode18, can you create a PR?

calumbrodie commented 10 years ago

I've also ran into this issue, but on a single machine. Because the kernel.root_dir is stored in Redis at the time the job is created, and this is stored as an absolute path, it contains the current release directory of my application.

As such, if I make a release, which changes the current working directory, all jobs on the queue with the previous kernel.root_dir value are executed against the old (out of date) version of the application.

Also - if I make multiple releases in quick succession, it's possible for the kernel to not even exist when the job is due to run.

The above allows my to define the root directory as a symlink instead circumventing the problem. Any chance this can be merged?