Skip to content

Commit

Permalink
fix: job concurrency (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio authored Nov 24, 2024
1 parent c554e50 commit 23a9e6a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
33 changes: 33 additions & 0 deletions app/Jobs/Middleware/RateLimitSchedulableJobMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace App\Jobs\Middleware;

use Closure;
use Illuminate\Support\Facades\Redis;

class RateLimitSchedulableJobMiddleware
{
public function __construct(
public string $key
) {
//
}

/**
* Process the queued job.
*
* @param \Closure(object): void $next
*/
public function handle(object $job, Closure $next): void
{
Redis::throttle('rate-limit-job:' . $this->key)
->allow(1)
->every(1)
->then(
fn () => $next($job),
fn () => $job->release(2)
);
}
}
8 changes: 8 additions & 0 deletions app/Jobs/PersistTemporaryTableData.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Contracts\ClearsCache;
use App\Contracts\TemporaryTable;
use App\Jobs\Middleware\RateLimitSchedulableJobMiddleware;
use Exception;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
Expand Down Expand Up @@ -69,4 +70,11 @@ public function handle(): void
$model->clearCache($this->electionId);
}
}

public function middleware(): array
{
return [
new RateLimitSchedulableJobMiddleware('persist-temporary-table-data'),
];
}
}
8 changes: 8 additions & 0 deletions app/Jobs/SchedulableJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Jobs;

use App\Jobs\Middleware\RateLimitSchedulableJobMiddleware;
use App\Models\ScheduledJob;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand Down Expand Up @@ -50,6 +51,13 @@ final public function handle(): void
$this->scheduledJob->touch('last_run_at');
}

public function middleware(): array
{
return [
new RateLimitSchedulableJobMiddleware('schedulable-job'),
];
}

public function uniqueId(): string
{
return "scheduled-job:{$this->scheduledJob->id}";
Expand Down
3 changes: 0 additions & 3 deletions app/Models/ScheduledJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,6 @@ public function getPreparedSourceUrl(array $map = []): string

public function fetchSource(array $map = []): Response
{
// Jitter to prevent rate limiting
usleep(1000 * rand(50, 256));

return Http::createPendingRequest()
->when($this->requiresAuthentication(), function (PendingRequest $request) {
return $request->withBasicAuth($this->source_username, $this->source_password);
Expand Down
2 changes: 1 addition & 1 deletion config/horizon.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@
'*' => [
'supervisor-1' => [
'minProcesses' => 1,
'maxProcesses' => 50,
'maxProcesses' => 20,
],
],
],
Expand Down

0 comments on commit 23a9e6a

Please sign in to comment.