You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// temporary: get session team_id for restore at end
$session_team_id = getPermissionsTeamId();
// set actual new team_id to package instance
setPermissionsTeamId($model);
// get the admin user and assign roles/permissions on new team model
User::find('your_user_id')->assignRole('Super Admin');
// restore session team_id to package instance using temporary value stored above
setPermissionsTeamId($session_team_id);
So let's assume i have 10 queue workers, each of them is a single PHP process, by the comments on the code, this call 'getPermissionsTeamId()' reads some session data, the session is stored let's assume in the database.
So the question is, I want to do some background jobs, impersonating users and teams its the easy way for me, since the application is 'secure' by default, has global scopes and permissions and so on, impersonating a user and a team inside the job looks like the right choice.
The problem that I see with this approach is that it could lead to race conditions, since the team id looks like it is stored in the session, so if i have two different jobs calling 'setPermissionsTeamId()' i assume the last call wins, since (i don't know).
Job 1 on queue A:
setPermissionsTeamId(1)
// do some pretty long work (e.g. 20 seconds) which relies on getPermissionsTeamId() == 1
Job 2 on queue B:
setPermissionsTeamId(2)
// do some pretty long work (e.g. 20 seconds) which relies on getPermissionsTeamId() == 2
Sorry if this seems stupid :)
Edit: For clarifying it more, I don't know if the current team id is stored in session (hence database, in my case) or just in the instance, but the comments on the example imply that the team id is stored and retrieved from the session
Edit 2: So it seems there is actually a package from Spatie that implements just what I wanted to accomplish, laravel-multitenancy. "Making queues tenant aware", my bad for not searching it better
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello, so I am new to this package (and webdev/Laravel in general [but not new to programming :p]) so I have a question about concurrency.
According to the package example: https://spatie.be/docs/laravel-permission/v6/basic-usage/teams-permissions
This is how you switch teams in the application:
So let's assume i have 10 queue workers, each of them is a single PHP process, by the comments on the code, this call 'getPermissionsTeamId()' reads some session data, the session is stored let's assume in the database.
So the question is, I want to do some background jobs, impersonating users and teams its the easy way for me, since the application is 'secure' by default, has global scopes and permissions and so on, impersonating a user and a team inside the job looks like the right choice.
The problem that I see with this approach is that it could lead to race conditions, since the team id looks like it is stored in the session, so if i have two different jobs calling 'setPermissionsTeamId()' i assume the last call wins, since (i don't know).
Job 1 on queue A:
setPermissionsTeamId(1)
// do some pretty long work (e.g. 20 seconds) which relies on getPermissionsTeamId() == 1
Job 2 on queue B:
setPermissionsTeamId(2)
// do some pretty long work (e.g. 20 seconds) which relies on getPermissionsTeamId() == 2
Sorry if this seems stupid :)
Edit: For clarifying it more, I don't know if the current team id is stored in session (hence database, in my case) or just in the instance, but the comments on the example imply that the team id is stored and retrieved from the session
Edit 2: So it seems there is actually a package from Spatie that implements just what I wanted to accomplish, laravel-multitenancy. "Making queues tenant aware", my bad for not searching it better
Beta Was this translation helpful? Give feedback.
All reactions