Skip to content

Commit

Permalink
Possibility of injecting logger service from Symfony (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
markitosgv authored Dec 15, 2016
1 parent 3a71696 commit 534f0c3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,13 @@ By default `VERBOSE` environment variable is set when calling php-resque

See php-resque logging option : https://github.com/chrisboulton/php-resque#logging

## Use monolog from your kernel

You can pass a monolog channel to write your logs, see example in supervisor config. Note: Pass Kernel ENV to resque is to instanciate logger, no for sharing kernel between jobs.

## Running a worker without forking

Sometimes intensive tasks may lose performance with forking mode. We have added a bin/resque-single binary, you can pass kernel file to be loaded only once at start. Be carefull cause if this Worker fails, then you need to restart manually this worker. We recommend to use supervisor to control this, and superlance with memory plugin to restart worker when hits some memory treshold.
Sometimes intensive tasks may lose performance with forking mode. We have added a bin/resque-single binary, you can pass kernel file to be loaded only once at start. Be carefull cause if this Worker fails, then you need to restart manually this worker. We recommend to use supervisor to control this, and superlance with memory plugin to restart worker when hits some memory treshold. Be careful too with your code, shared variables, services instances... etc.

## Adding a delayed job to a queue

Expand Down Expand Up @@ -258,6 +262,12 @@ user = myusername
environment = APP_INCLUDE='/home/sites/myapp/prod/current/vendor/autoload.php',VERBOSE='1',RESQUE_PHP='/home/sites/myapp/prod/current/vendor/chrisboulton/php-resque/lib/Resque.php'
stopsignal=QUIT
[program:myapp_phpresque_default_single_worker_logger]
command = /usr/bin/php /home/sites/myapp/prod/current/vendor/instasent/resque-bundle/Instasent/ResqueBundle/bin/resque-single
user = myusername
environment = APP_KERNEL='/home/sites/myapp/prod/current/app/AppKernel.php',LOG_CHANNEL='monolog.logger.custom',APP_INCLUDE='/home/sites/myapp/prod/current/vendor/autoload.php',VERBOSE='1',QUEUE='default'
stopsignal=QUIT
[group:myapp]
programs=myapp_phpresque_default,myapp_phpresque_scheduledworker
```
Expand Down
12 changes: 12 additions & 0 deletions bin/resque
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ if (!empty($COUNT) && $COUNT > 1) {
$count = $COUNT;
}

if (getenv('LOG_CHANNEL') && getenv('APP_KERNEL')) {
$file = new SplFileInfo(getenv('APP_KERNEL'));
$class = $file->getBasename('.php');

require $file;

$kernel = new $class(getenv('SYMFONY_ENV'), false);
$kernel->boot();

$logger = $kernel->getContainer()->get(getenv('LOG_CHANNEL'));
}

$PREFIX = getenv('PREFIX');
if (!empty($PREFIX)) {
$logger->log(Psr\Log\LogLevel::INFO, 'Prefix set to {prefix}', array('prefix' => $PREFIX));
Expand Down
4 changes: 4 additions & 0 deletions bin/resque-single
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ if (getenv('APP_KERNEL')) {
$KERNEL = $kernel;
}

if (getenv('LOG_CHANNEL')) {
$logger = $KERNEL->getContainer()->get(getenv('LOG_CHANNEL'));
}

$PREFIX = getenv('PREFIX');
if (!empty($PREFIX)) {
$logger->log(Psr\Log\LogLevel::INFO, 'Prefix set to {prefix}', array('prefix' => $PREFIX));
Expand Down

0 comments on commit 534f0c3

Please sign in to comment.