Comments (12)
I'm think that in schedule commands that should be tenant aware you could just loop over de tenants yourself. You can use the eachCurrent
method to loop over a tenant collection, it will make each tenant the current one before the callable is executed.
\Spatie\Multitenancy\Models\Tenant::eachCurrent(function(Tenant $tenant) {
// $tenant is now the active one.
});
from laravel-multitenancy.
No, the package does not do this. It allows you to define tasks that should run when switching tenants. If you expect a more batteries included type of package, please check out the alternatives mentioned in the readme.
from laravel-multitenancy.
Thank you for your quick response.
However, As I research the packages DOCs, I got well flexibility. So, I will recommend this package for my project. In this case, I need your help to resolve those issues.
FileSystem: For these issues, I can create a new Task for managing the File System easily.
In the makeCurrent, I can write a few code as follows:
config()->set('filesystems.disks.disk_name.root', storage_path("app/public/tenant_{$tenant->id}" ));
config()->set('filesystems.disks.disk_name.url', "/storage/tenant_{$tenant->id}");
Passport: I saw a reference to resolve this.(Not test in this package).
https://medium.com/@wannymiarelli/laravel-multi-tenancy-with-passport-10ce9b6a1c21
Scheduling: In my project, might have 5000+ Tenants in the future.So, I need the best solution so that It does not affect my project performance. Can you provide any hints for that?
I highly request you to review my above possible solution and suggest me the best way.
Thank you in advance.
Anwar
from laravel-multitenancy.
@anwardote, it's effortless using tasks.
You can add a "tenant" disk in "config/filesystems.php" file:
'disks' => [
'tenant' => [
'visibility' => 'public',
],
]
Next, you can create your switch task "FilesystemTask":
class FilesystemTask implements SwitchTenantTask
{
public function makeCurrent(Tenant $tenant): void
{
$this->setTenantFilesystem($tenant);
}
public function forgetCurrent(): void
{
$this->setTenantFilesystem();
}
protected function setTenantFilesystem(?Tenant $tenant = null): void
{
config()->set('filesystem.tenant', [
// your setup for a public disk
]);
}
}
Hope that will help you :)
from laravel-multitenancy.
@masterix21 , Thank you...
Yea. we can manage Filesystem using Switch Task. But how about Passport and Scheduling?
from laravel-multitenancy.
About Passport, I think you can use one of these two approaches.
- keep the original Models and use a task to switch the default connection
config()->set('database.default',` 'tenant')
- create a task to switch Passport models to custom ones if you're using a tenant
class PassportTask implements SwitchTenantTask
{
public function makeCurrent(Tenant $tenant): void
{
$this->setTenantPassport($tenant);
}
public function forgetCurrent(): void
{
$this->setTenantPassport();
}
protected function setTenantPassport(?Tenant $tenant = null): void
{
Passport::useTokenModel(TokenTenantAware::class);
Passport::useClientModel(ClientTenantAware::class);
Passport::useAuthCodeModel(AuthCodeTenantAware::class);
Passport::usePersonalAccessClientModel(PersonalAccessClientTenantAware::class);
}
}
TokenTenantAware::class extends Token {
UsesTenantConnection;
}
ClientTenantAware::class extends Client {
UsesTenantConnection;
}
AuthCodeTenantAware::class extends AuthCode {
UsesTenantConnection;
}
PersonalAccessClientTenantAware::class extends PersonalAccessClient {
UsesTenantConnection;
}
from laravel-multitenancy.
Great solution!
I hope, FileSystem and Passport issues are solved. I will apply your solutions to my real project very soon. Only the Scheduling issue is left to resolve
Thank you again
from laravel-multitenancy.
Scheduling is a bit different solution, it depends on what you are doing (or you like to do).
If you are meaning about "jobs", "spatie/laravel-multitenancy" already does the dirty work for you, using "queues_are_tenant_aware_by_default" to true in your config file.
If you are meaning about "schedule:run", it's a bit more complicated, because you must understand what you like to do: is it scheduling for all tenants? Or only for one?
from laravel-multitenancy.
I am thinking about "schedule: run". Yea it's a bit complex. but I have both types of scheduling.
01. For all tenants: E.g, an announcement for all Tenants. or some scheduling job which should be run in a specific time according to Tenant's Time Zone.
02. For only specific Tenants: E.g, we may need to fire warning/security/billing notification to any/some Tenants.
Noted: In my project, It might have 5000+ Tenants in the future. So, I am searching for the best solution so that It does not affect my project performance.
from laravel-multitenancy.
So, you can create a command (or more), iterate all tenants and push job tenant-aware on the queue (delayed if needed: https://laravel.com/docs/7.x/queues#delayed-dispatching).
Yes, could be 5000 or more Tenants but if your approach is to use the "schedule:run" there are no other solutions.
from laravel-multitenancy.
Thanks a lots :-)
from laravel-multitenancy.
@freekmurze Sorry for the wired question but is this assuming the jobs/failed_jobs
table are in /landlord
?
Thanks
from laravel-multitenancy.
Related Issues (20)
- Current tenant not determined when dispatching queued closures HOT 6
- When dispatching a Tenant Aware Job in sync mode from a test, any prior/current DB transaction is cleared HOT 2
- Model ignore tenant when add Observer HOT 2
- Tenant password reset token is being stored on landlord password_reset_tokens table HOT 1
- Prefixing cache doesn't work fine with file-store HOT 3
- Conflict with Rabbitmq package from vyuldashev HOT 1
- Scheduler : Large (longer) jobs, multiple tenants, stops.
- Tenant-Aware Job Scheduling with Horizon HOT 4
- MakeQueueTenantAwareAction seems to break active requests when Queue Connection is sync HOT 4
- base table or view not found with AWS SQS queue for job TenantAware HOT 12
- randomly receive Invalid catalog name: 1046 No database selected (Connection: mysql, SQL: insert into failed_jobs (uuid, connection, queue, payload, exception, failed_at) on queue sqs HOT 5
- Tenant Aware Queue Fails No Database Selected when running from supervisor in a multi tenant environment using AWS SQS FIFO HOT 2
- Update documentation at https://spatie.be to `v3` HOT 3
- in_array(): Argument #2 ($haystack) must be of type array, null given {"exception":"[object] (TypeError(code: 0): in_array(): Argument #2 ($haystack) must be of type array, null given at /spatie/laravel-multitenancy/src/Actions/MakeQueueTenantAwareAction.php:79) HOT 1
- Cache issue with spatie/laravel-permission in TenantAware Command HOT 5
- Issue with the has function on different connections HOT 1
- I want to Connect to mongo DB for some tables but it is giving "Database connection [mongodb] not configured.", exception: "InvalidArgumentException",…} error
- Custom cache driver missing when switching Tenant HOT 2
- Cannot schedule jobs in Laravel 11 HOT 3
- Can't make route not tenant aware
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-multitenancy.