Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Laravel Scheduler with Multitenancy #103

Closed
LukeAbell opened this issue Aug 4, 2020 · 8 comments
Closed

Using Laravel Scheduler with Multitenancy #103

LukeAbell opened this issue Aug 4, 2020 · 8 comments
Assignees
Labels
bug Something isn't working

Comments

@LukeAbell
Copy link

Running php artisan tenants:artisan schedule:run loops through the tenants and runs the scheduled commands but Tenant::current() is null. I think I've narrowed this down to the scheduler calling an artisan command in a forked process which recalls php artisan XXX with whatever the command is (which bypasses the multi-tenancy call).

Is there a way to make the scheduler tenant aware like the package currently does with queues?

Thanks!

@masterix21
Copy link
Collaborator

Hi @LukeAbell, are you sure that are you doing everything well?

See my example here:
#82 (comment)

Please post your code. Thank you

@LukeAbell
Copy link
Author

@masterix21 @freekmurze The issue only occurs when using $schedule->command() or $schedule->exec(). Running $schedule->job() on the works correctly.

@LukeAbell
Copy link
Author

@freekmurze The issue also affects all of the Mailcoach commands. Turning them into Jobs works.

@masterix21 masterix21 added the bug Something isn't working label Aug 5, 2020
@masterix21 masterix21 self-assigned this Aug 5, 2020
@masterix21
Copy link
Collaborator

@LukeAbell, thanks for your issue.

I'm working over the solution, and in the next days, I will release the PR to fix it.

In the meanwhile, if you like to apply a temporary solution, you can use this trait with your tenant-aware commands:

trait IsTenantAware
{
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $tenantIds = Arr::wrap($this->option('tenant'));

        return Tenant::query()
            ->when(! blank($tenantIds), fn ($query) => $query->whereIn('id', $tenantIds))
            ->cursor()
            ->map(fn ($tenant) => $tenant->execute(fn () => (int) $this->laravel->call([$this, 'handle'])))
            ->sum();
    }
}

Finally, change your command signatures appending --tenant=*.

@LukeAbell
Copy link
Author

@masterix21 thank you!!

@masterix21
Copy link
Collaborator

I forgot to say that you could change your schedule method as follow:

    protected function schedule(Schedule $schedule)
    {
        $schedule->command('tenant:aware:command');
    }

And you could call only

php artisan schedule:run

Thanks

@masterix21
Copy link
Collaborator

@LukeAbell the fix has been released.

@LukeAbell
Copy link
Author

@masterix21 thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants