Skip to content

Instantly share code, notes, and snippets.

@viezel
Last active January 8, 2020 13:30
Show Gist options
  • Save viezel/733b426f64884a95df5e9f85ef065075 to your computer and use it in GitHub Desktop.
Save viezel/733b426f64884a95df5e9f85ef065075 to your computer and use it in GitHub Desktop.
Possible bug: Config persists between queue jobs ?

Possible bug: Config persists between queue jobs ?

I think i found a bug in handling of queue jobs in Laravel. If I run laravel queue jobs with config cached in laravel, then runtime configs persists between jobs.

Expected result: Second job does not know of runtime config.

How to reproduce

Job1: WriteToConfig

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class WriteToConfig implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function handle()
    {
        //write to config
        config(['services.foobar' => 'hello']);
        info('writing config value: ' . config('services.foobar') );
    }
}

Job2: ReadRuntimeConfig

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class ReadRuntimeConfig implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
    public function handle()
    {
        info('reading config value: ' . config('services.foobar') );
    }
}
Route::get("test", function(){
    dispatch(new \App\Jobs\WriteToConfig());
    dispatch(new \App\Jobs\ReadRuntimeConfig());
});

Test

Test 1 (without no cached config)

  1. run command php artisan config:clear
  2. start worker php artisan queue:work --tries=1
  3. run jobs, by visiting /test route

Result of Test 1

[2020-01-08 13:03:40][13] Processing: App\Jobs\WriteToConfig
[2020-01-08 13:03:40][13] Processed:  App\Jobs\WriteToConfig
[2020-01-08 13:03:58][14] Processing: App\Jobs\ReadRuntimeConfig
[2020-01-08 13:03:58][14] Processed:  App\Jobs\ReadRuntimeConfig

[2020-01-08 13:03:42] local.INFO: writing config value: hello  
[2020-01-08 13:04:00] local.INFO: reading config value:   

Test 2 (with cached config)

  1. run command php artisan config:cache
  2. start worker php artisan queue:work --tries=1
  3. run jobs, by visiting /test route

Result of Test 2

[2020-01-08 13:03:40][13] Processing: App\Jobs\WriteToConfig
[2020-01-08 13:03:40][13] Processed:  App\Jobs\WriteToConfig
[2020-01-08 13:03:58][14] Processing: App\Jobs\ReadRuntimeConfig
[2020-01-08 13:03:58][14] Processed:  App\Jobs\ReadRuntimeConfig

[2020-01-08 13:03:42] local.INFO: writing config value: hello  
[2020-01-08 13:04:00] local.INFO: reading config value: hello 

As you see in Test2, the ReadRuntimeConfig knows of the values "hello" which I think it should not. This is the potential bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment