简体   繁体   中英

multiple queues on same server with different environment (Laravel Forge)

I am really confused and would be happy if you could shed some light.

What I have:

1) Laravel Forge with single server.

2) Two sites there(staging and production)

3) 1 queue set up for each (staging and production on FORGE)

Problem: I have pdf that gets generated via queue. It looked like that even though queue got executed from staging code, sometimes production site's queue would run. After reading a lot, I summed up that they mess with each other because they are on the same server.. So Here is what I did. This is the config/queue.php.

'database' => [
     'driver' => 'database',
     'table' => 'jobs',
     'queue'  => env('QUEUE_NAME', 'default'), // i made this instead of default as it was
     'retry_after' => 90,
],

After that, as I have different env files for staging and production, I put different QUEUE_NAME values in there. let's say in .staging.env QUEUE_NAME=staging and in .prod.env QUEUE_NAME=production .

Then I moved to My Laravel Forge's staging site, added a queue like this:

Connection: database
Queue: staging

I also moved to My Laravel Forge's prod site, added a queue, but another one differently.

Connection: database
Queue: production

After all this, looks like problem might have disappeared.

Question 1) Do you think What I have done is right and most importantly, Enough so that my problem never arises again?

Question 2) deploy script for both staging and production sites have php artisan queue:restart at the end of it. I think that if I upload something to staging server and that line gets executed, it's also gonna restart production's queue too. which seems really bad. I couldn't find anything to pass a parameter to restart like this:

php artisan queue:restart --queue=staging

I think your solution works, but a better solution would be to make sure they aren't using the same data store.

For each queue driver, Laravel stores pending jobs, completed jobs and failed jobs. The location of the first two depends on the driver, while the last one is always stored in the database. For instance, for the database driver, pending and completed jobs are stored in the database. For the redis driver, they're stored in Redis.

This means that if you can make sure each instance of your application is using a different database (even if they're on the same installation), you should be good: For example (assuming you're using shared instances):

  • if your database instance is MySQL, you can create a different database for each environment and set this as the DB_NAME .
  • if your database instance is SQLite, create a different database file for each and place in the database/ folder, then set the path name as DB_NAME
  • if your queue driver is redis , set the value of REDIS_DB (a number between 0 and 15) to different values for the different environments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM