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

Scheduled Job issue #82

Closed
ChrisCook1906 opened this issue Jul 18, 2020 · 7 comments
Closed

Scheduled Job issue #82

ChrisCook1906 opened this issue Jul 18, 2020 · 7 comments

Comments

@ChrisCook1906
Copy link

ChrisCook1906 commented Jul 18, 2020

Hi --
I have the package set up and things are working as expected....with the exception of my scheduled jobs.

I have queues enabled by default to be tenant aware:
'queues_are_tenant_aware_by_default' => true

I have the middleware added to app/http/kernel:

// middlewaregroups / web:
\Spatie\Multitenancy\Http\Middleware\NeedsTenant::class,
\Spatie\Multitenancy\Http\Middleware\EnsureValidTenantSession::class

In App\Console\Kernel, I have a scheduled job:
$schedule->job(new UpdateMatrix())->everyFiveMinutes();

The job runs twice but only the main database updates.

In my cronjob (which executes the Scheduler) I have applied the tenants:artisan prefix.

For the UpdateMatrix job above, I have added TenantAware
class UpdateMatrix implements ShouldQueue, TenantAware..

What am I missing?

@masterix21
Copy link
Collaborator

Hi @ChrisCook1906, how does scheduled your jobs? All jobs are scheduled using the Laravel default logic (tenant not-aware). Have you implemented a logic that replaces the original one? Please post your code to help me to understand. Thanks

@ChrisCook1906
Copy link
Author

I'm running the standard Scheduler function in App/Console/Kernel:
protected function schedule(Schedule $schedule) { $schedule->job(new UpdateMatrix())->hourly(); }

The job, UpdateMatrix, has the proper TenantAware function implemented. All of my other queued jobs are working correctly, except for those dispatched via the Schedule function in (App/Console/Kernel).

@masterix21
Copy link
Collaborator

Ok, sorry for my questions but I'd like to understand: in your UpdateMatrix do you use Tenant::makeCurrent()?

@ChrisCook1906
Copy link
Author

No. In the UpdateMatrix job, I implement the following:
use Spatie\Multitenancy\Jobs\TenantAware;

class UpdateMatrix implements ShouldQueue, TenantAware {......}

@masterix21
Copy link
Collaborator

@ChrisCook1906, I tried but seems that works like a charm. What am I wrong?

// app/console/Kernel.php
protected function schedule(Schedule $schedule)
{
    if (Tenant::current()) {
        $schedule->job(TenantNoopJob::class)->everyMinute();
        return;
    }

    $schedule->job(LandlordNoopJob::class)->everyMinute();
}
//
// TenantNoopJob
class TenantNoopJob implements ShouldQueue, TenantAware
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function handle()
    {
        Log::info('TenantNoopJob - '. Tenant::current()->domain .' - '. now());
    }
}

// 
// LandlordNoopJob
class LandlordNoopJob implements ShouldQueue, NotTenantAware
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function handle()
    {
        Log::info('LandlordNoopJob - '. now());
    }
}

example

@masterix21
Copy link
Collaborator

@ChrisCook1906, do you have found a solution to the issue?

@ChrisCook1906
Copy link
Author

@masterix21 Yes. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants