tenancy / multi-tenant

Run multiple websites using the same Laravel installation while keeping tenant specific data separated for fully independent multi-domain setups, previously github.com/hyn/multi-tenant
https://tenancy.dev
MIT License
2.56k stars 394 forks source link

Tenants not resolved inside of the App\Console\Kernel@schedule method #927

Closed sasokovacic closed 4 years ago

sasokovacic commented 4 years ago

Description

I want to run scheduled tasks for tenants. I've copied code from the documentation (https://tenancy.dev/docs/hyn/5.4/laravel-scheduler) and it's not working. Tenant is not resolved in the schedule() method of the App\Console\Kernel class.


Actual behavior

The if (app(\Hyn\Tenancy\Environment::class)->tenant()) { is never executed because tenant() method returns null. A tenant is only resolved inside of the before() method.

protected function schedule(Schedule $schedule)
{
    $schedule
        ->command(RemindUsersAboutEntityControlDeadlineCommand::class)
        ->everyMinute()
        ->before(function (Schedule $schedule) {
            $tenant = app(\Hyn\Tenancy\Environment::class)->tenant();
            dump($tenant->id); // Here a tenant is resolved
        });

    // app(\Hyn\Tenancy\Environment::class)->tenant() always returns null
    if (app(\Hyn\Tenancy\Environment::class)->tenant())
    {            
        // This is never executed
    } else {
        // This is always executed
    }
}

Expected behavior

Tenants should be resolved inside the App\Console\Kernel@schedule method as shown in the documentation.


Information

Bibendus83 commented 4 years ago

You should post your stack trace or it's difficult to find what's the problem.

fletch3555 commented 4 years ago

You're not identifying a tenant before your if conditional. The before() is only called before the command listed in $schedule->command() is run. They run in different contexts.

bkintanar commented 4 years ago

Closing this issue. If you are still having issues with the package, please create a new issue. Thank you