-2

I am facing an issue in my Laravel project. I have created multiple databases. When a user logs in through the frontend, they have to select a company code. With this code we decide which database has to be used from the list in config/companyDb.php. This file contains all the database details for the companies. For logging in a user this all works fine, but when this is used in a job that is ran from a queue, it doesn't work.

If I run php artisan queue:all Xcompany it works for a single company, but if I run php artisan queue:all it does not work. I have to run it in the background through a custom artisan command.

2
  • We need more information to be able to answer this question. How are you fetching the database details? What logic is the job executing? How are you running the queue exactly? Please read how do I ask a good question and edit your question to include as much information as possible. Commented Mar 24 at 14:31
  • It will be much easier to store all jobs in a single database and switch the db connection for each job. If you use multiple databases for jobs, you would need nX number of queue workers for X databases ( n = number of queue workers per database, X = number of databases ). For example, if you have 15 databases and you need 4 workers each database, that will be 60 workers.... not exactly very resource efficient Commented Mar 25 at 8:48

1 Answer 1

0

You shared to few details, so the answer would be general:

When you login, you rely on web or api guard to retrieve user details, including database in your case.

At the same time jobs are running without authentication in background. Job just don't have authenticated user and his details.

How to solve?

I don't know your business logic, so hard to say which to choose

At glance, I would pass database name directly to job:

class MyCustomJob 
{
public function __construct(..., protected string $dbName = 'defaultDB') {}

/** other logic */

PS:

php artisan queue:all Xcompany is something custom I guess, so there could be also some problems

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.