Skip to content

Commit 57b15e2

Browse files
Merge pull request #36 from aloware/fix/number-of-processed-jobs
Fix Number of Processed Jobs
2 parents cb1345f + 72ca0a3 commit 57b15e2

File tree

2 files changed

+21
-30
lines changed

2 files changed

+21
-30
lines changed

src/FairQueueServiceProvider.php

-29
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public function boot(): void
5656
$this->registerRoutes();
5757
$this->registerResources();
5858
$this->publishAssets();
59-
$this->registerQueueEvents();
6059

6160
$this->callAfterResolving(Schedule::class, function (Schedule $schedule) {
6261
if(config('fair-queue.recover_lost_jobs.enabled')) {
@@ -90,34 +89,6 @@ protected function registerRoutes(): void
9089
});
9190
}
9291

93-
/**
94-
* Register the FairQueue Queue Events.
95-
*
96-
* @return void
97-
*/
98-
protected function registerQueueEvents(): void
99-
{
100-
Queue::after(function (JobProcessed $event) {
101-
$redis = FairQueue::getConnection();
102-
$payload = $event->job->payload();
103-
if (!isset($payload['data']) || !isset($payload['data']['command'])) {
104-
return;
105-
}
106-
$command = unserialize($payload['data']['command']);
107-
if (!$command instanceof FairSignalJob) {
108-
return;
109-
}
110-
$queue = $command->queue;
111-
$partition = $command->partition;
112-
$past_minute_key = $this->partitionProcessedJobsInPastMinutesKey($queue, $partition, 1);
113-
$past_20minute_key = $this->partitionProcessedJobsInPastMinutesKey($queue, $partition, 20);
114-
$past_60minute_key = $this->partitionProcessedJobsInPastMinutesKey($queue, $partition, 60);
115-
$redis->zadd($past_minute_key, now()->getPreciseTimestamp(3), $payload['id']);
116-
$redis->zadd($past_20minute_key, now()->getPreciseTimestamp(3), $payload['id']);
117-
$redis->zadd($past_60minute_key, now()->getPreciseTimestamp(3), $payload['id']);
118-
});
119-
}
120-
12192
/**
12293
* Register the FairQueue resources.
12394
*

src/FairSignalJob.php

+21-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Aloware\FairQueue;
44

5+
use Aloware\FairQueue\Facades\FairQueue;
56
use Aloware\FairQueue\Interfaces\RepositoryInterface;
7+
use Aloware\FairQueue\Repositories\RedisKeys;
68
use Illuminate\Bus\Queueable;
79
use Illuminate\Contracts\Queue\ShouldQueue;
810
use Illuminate\Foundation\Bus\Dispatchable;
@@ -11,7 +13,7 @@
1113

1214
class FairSignalJob implements ShouldQueue
1315
{
14-
use Dispatchable, InteractsWithQueue, Queueable;
16+
use Dispatchable, InteractsWithQueue, Queueable, RedisKeys;
1517

1618
public $partition;
1719

@@ -63,6 +65,10 @@ public function handle()
6365
}
6466

6567
$job->handle();
68+
69+
// Update Fair Queue Stats
70+
$this->updateStats($job->uuid);
71+
6672
} catch (\Throwable $e) {
6773
printf('[%s] %s' . PHP_EOL, get_class($job), $e->getMessage());
6874

@@ -171,4 +177,18 @@ private function selectPartition($repository, $partitionsMethod = 'partitions')
171177

172178
return $partitions[$partitionIndex];
173179
}
180+
181+
public function updateStats($uuid)
182+
{
183+
$redis = FairQueue::getConnection();
184+
$queue = $this->queue;
185+
$partition = $this->partition;
186+
187+
$past_minute_key = $this->partitionProcessedJobsInPastMinutesKey($queue, $partition, 1);
188+
$past_20minute_key = $this->partitionProcessedJobsInPastMinutesKey($queue, $partition, 20);
189+
$past_60minute_key = $this->partitionProcessedJobsInPastMinutesKey($queue, $partition, 60);
190+
$redis->zadd($past_minute_key, now()->getPreciseTimestamp(3), $uuid);
191+
$redis->zadd($past_20minute_key, now()->getPreciseTimestamp(3), $uuid);
192+
$redis->zadd($past_60minute_key, now()->getPreciseTimestamp(3), $uuid);
193+
}
174194
}

0 commit comments

Comments
 (0)