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

Tenant-Aware Job Scheduling with Horizon #498

Open
avt00 opened this issue Nov 18, 2023 · 2 comments
Open

Tenant-Aware Job Scheduling with Horizon #498

avt00 opened this issue Nov 18, 2023 · 2 comments
Assignees

Comments

@avt00
Copy link

avt00 commented Nov 18, 2023

Sorry for opening an issue, but I think I've exhausted the solutions I've found in the docs and here in closed issues.

I aim to:

  • Schedule a job within Kernel.php using the schedule() function.
  • Execute this job via Horizon with tenant awareness.

I've explored two approaches based on the documentation and discussions in this repository.

Approach 1:

protected function schedule(Schedule $schedule)
   {
       Tenant::all()->eachCurrent(function (Tenant $tenant) use ($schedule) {
           Log::info('Schedule for tenant: '.$tenant->id);
           $schedule->job(new FetchItems($tenant->name))
               ->name('fetch-items-'.$tenant->id) // unique name for each tenant
               ->everyMinute();
       });
   }

// FetchItems:

class FetchItems implements ShouldQueue
{
    public function __construct(string $tenantName = null)
    {
        // log random number 
        Log::info('Random number: '.rand(1, 1000));
    }
// rest of the code
}

Result:

php artisan schedule:run
[2023-11-18 19:02:23] local.INFO: Schedule for tenant: 1
[2023-11-18 19:02:23] local.INFO: Random number: 414
[2023-11-18 19:02:23] local.INFO: Schedule for tenant: 2
[2023-11-18 19:02:23] local.INFO: Random number: 197
[2023-11-18 19:02:23] local.INFO: Schedule for tenant: 3
[2023-11-18 19:02:23] local.INFO: Random number: 322

Running php artisan schedule:run logs the expected information. However, when processed by Horizon, an error occurs stating that the current tenant could not be determined, with no tenantId in the payload.
The current tenant could not be determined in a job named Illuminate\Queue\CallQueuedHandler@call. No tenantId was set in the payload.

Approach 2:

I modified the scheduling code as follows:

    protected function schedule(Schedule $schedule)
    {
        $schedule->job(new FetchItems())
            ->name('fetch-items')
            ->withoutOverlapping()
            ->everyMinute();
    }

// FetchItems class as in Approach 1
}

and php artisan tenants:artisan schedule:run

Result:

php artisan tenants:artisan schedule:run

Running command for tenant `first` (id: 1)...
---------------------------------------------------------
[2023-11-18 19:10:24] local.INFO: Random number: 6168

  2023-11-18 19:10:24 Running [fetch-items] ............................................................................................... 8ms DONE


Running command for tenant `health` (id: 2)...
---------------------------------------------------------

  2023-11-18 19:10:24 Running [fetch-items] ............................................................................................... 1ms DONE


Running command for tenant `love` (id: 3)...
---------------------------------------------------------

  2023-11-18 19:10:24 Running [fetch-items] ............................................................................................... 1ms DONE

Running php artisan tenants:artisan schedule:run executes correctly, but with Horizon the job seems to be fixed to the first tenant, with the constructor running only once during scheduling. This behaviour is not what I expected as I need the job to be tenant specific.

I'm looking for insight into what might be missing or overlooked in my implementation. Any advice or suggestions would be greatly appreciated. Thank you very much.

@masterix21 masterix21 self-assigned this Dec 11, 2023
@spatie-bot
Copy link

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.

@avt00
Copy link
Author

avt00 commented Apr 12, 2024

The issue is still present

@masterix21 masterix21 reopened this Apr 17, 2024
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

3 participants