Skip to content

Commit

Permalink
Merge pull request #826 from browner12/carbon
Browse files Browse the repository at this point in the history
[5.x] Switch from Chronos to Carbon
  • Loading branch information
taylorotwell authored Apr 13, 2020
2 parents ac82e97 + b86a210 commit 8dfcb2a
Show file tree
Hide file tree
Showing 23 changed files with 98 additions and 98 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
"ext-json": "*",
"ext-pcntl": "*",
"ext-posix": "*",
"cakephp/chronos": "^2.0",
"illuminate/contracts": "^7.0",
"illuminate/queue": "^7.0",
"illuminate/support": "^7.0",
"nesbot/carbon": "^2.17",
"ramsey/uuid": "^3.5|^4.0",
"symfony/process": "^5.0",
"symfony/error-handler": "^5.0"
Expand Down
4 changes: 2 additions & 2 deletions src/Jobs/RetryFailedJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Laravel\Horizon\Jobs;

use Cake\Chronos\Chronos;
use Carbon\CarbonImmutable;
use Illuminate\Contracts\Queue\Factory as Queue;
use Laravel\Horizon\Contracts\JobRepository;
use Laravel\Horizon\JobId;
Expand Down Expand Up @@ -75,7 +75,7 @@ protected function preparePayload($id, $payload)
protected function prepareNewTimeout($payload)
{
return $payload['timeoutAt']
? Chronos::now()->addSeconds(ceil($payload['timeoutAt'] - $payload['pushedAt']))->getTimestamp()
? CarbonImmutable::now()->addSeconds(ceil($payload['timeoutAt'] - $payload['pushedAt']))->getTimestamp()
: null;
}
}
10 changes: 5 additions & 5 deletions src/Listeners/MonitorWaitTimes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Laravel\Horizon\Listeners;

use Cake\Chronos\Chronos;
use Carbon\CarbonImmutable;
use Laravel\Horizon\Contracts\MetricsRepository;
use Laravel\Horizon\Events\LongWaitDetected;
use Laravel\Horizon\Events\SupervisorLooped;
Expand All @@ -20,7 +20,7 @@ class MonitorWaitTimes
/**
* The time at which we last checked if monitoring was due.
*
* @var \Cake\Chronos\Chronos
* @var \Carbon\CarbonImmutable
*/
public $lastMonitored;

Expand Down Expand Up @@ -77,7 +77,7 @@ protected function dueToMonitor()
// lock to monitor the wait times. We only want a single supervisor to run
// the checks on a given interval so that we don't fire too many events.
if (! $this->lastMonitored) {
$this->lastMonitored = Chronos::now();
$this->lastMonitored = CarbonImmutable::now();
}

if (! $this->timeToMonitor()) {
Expand All @@ -87,7 +87,7 @@ protected function dueToMonitor()
// Next we will update the monitor timestamp and attempt to acquire a lock to
// check the wait times. We use Redis to do it in order to have the atomic
// operation required. This will avoid any deadlocks or race conditions.
$this->lastMonitored = Chronos::now();
$this->lastMonitored = CarbonImmutable::now();

return $this->metrics->acquireWaitTimeMonitorLock();
}
Expand All @@ -99,6 +99,6 @@ protected function dueToMonitor()
*/
protected function timeToMonitor()
{
return Chronos::now()->subMinutes(1)->lte($this->lastMonitored);
return CarbonImmutable::now()->subMinutes(1)->lte($this->lastMonitored);
}
}
10 changes: 5 additions & 5 deletions src/Listeners/TrimFailedJobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Laravel\Horizon\Listeners;

use Cake\Chronos\Chronos;
use Carbon\CarbonImmutable;
use Laravel\Horizon\Contracts\JobRepository;
use Laravel\Horizon\Events\MasterSupervisorLooped;

Expand All @@ -11,7 +11,7 @@ class TrimFailedJobs
/**
* The last time the recent jobs were trimmed.
*
* @var \Cake\Chronos\Chronos
* @var \Carbon\CarbonImmutable
*/
public $lastTrimmed;

Expand All @@ -35,13 +35,13 @@ public function handle(MasterSupervisorLooped $event)
config('horizon.trim.failed', 10080), 12
));

$this->lastTrimmed = Chronos::now()->subMinutes($this->frequency + 1);
$this->lastTrimmed = CarbonImmutable::now()->subMinutes($this->frequency + 1);
}

if ($this->lastTrimmed->lte(Chronos::now()->subMinutes($this->frequency))) {
if ($this->lastTrimmed->lte(CarbonImmutable::now()->subMinutes($this->frequency))) {
app(JobRepository::class)->trimFailedJobs();

$this->lastTrimmed = Chronos::now();
$this->lastTrimmed = CarbonImmutable::now();
}
}
}
10 changes: 5 additions & 5 deletions src/Listeners/TrimMonitoredJobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Laravel\Horizon\Listeners;

use Cake\Chronos\Chronos;
use Carbon\CarbonImmutable;
use Laravel\Horizon\Contracts\JobRepository;
use Laravel\Horizon\Events\MasterSupervisorLooped;

Expand All @@ -11,7 +11,7 @@ class TrimMonitoredJobs
/**
* The last time the monitored jobs were trimmed.
*
* @var \Cake\Chronos\Chronos
* @var \Carbon\CarbonImmutable
*/
public $lastTrimmed;

Expand All @@ -35,13 +35,13 @@ public function handle(MasterSupervisorLooped $event)
config('horizon.trim.monitored', 10080), 12
));

$this->lastTrimmed = Chronos::now()->subMinutes($this->frequency + 1);
$this->lastTrimmed = CarbonImmutable::now()->subMinutes($this->frequency + 1);
}

if ($this->lastTrimmed->lte(Chronos::now()->subMinutes($this->frequency))) {
if ($this->lastTrimmed->lte(CarbonImmutable::now()->subMinutes($this->frequency))) {
app(JobRepository::class)->trimMonitoredJobs();

$this->lastTrimmed = Chronos::now();
$this->lastTrimmed = CarbonImmutable::now();
}
}
}
10 changes: 5 additions & 5 deletions src/Listeners/TrimRecentJobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Laravel\Horizon\Listeners;

use Cake\Chronos\Chronos;
use Carbon\CarbonImmutable;
use Laravel\Horizon\Contracts\JobRepository;
use Laravel\Horizon\Events\MasterSupervisorLooped;

Expand All @@ -11,7 +11,7 @@ class TrimRecentJobs
/**
* The last time the recent jobs were trimmed.
*
* @var \Cake\Chronos\Chronos
* @var \Carbon\CarbonImmutable
*/
public $lastTrimmed;

Expand All @@ -31,13 +31,13 @@ class TrimRecentJobs
public function handle(MasterSupervisorLooped $event)
{
if (! isset($this->lastTrimmed)) {
$this->lastTrimmed = Chronos::now()->subMinutes($this->frequency + 1);
$this->lastTrimmed = CarbonImmutable::now()->subMinutes($this->frequency + 1);
}

if ($this->lastTrimmed->lte(Chronos::now()->subMinutes($this->frequency))) {
if ($this->lastTrimmed->lte(CarbonImmutable::now()->subMinutes($this->frequency))) {
app(JobRepository::class)->trimRecentJobs();

$this->lastTrimmed = Chronos::now();
$this->lastTrimmed = CarbonImmutable::now();
}
}
}
6 changes: 3 additions & 3 deletions src/MasterSupervisor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Laravel\Horizon;

use Cake\Chronos\Chronos;
use Carbon\CarbonImmutable;
use Closure;
use Exception;
use Illuminate\Contracts\Cache\Factory as CacheFactory;
Expand Down Expand Up @@ -172,13 +172,13 @@ public function terminate($status = 0)
app(MasterSupervisorRepository::class)
->forget($this->name);

$startedTerminating = Chronos::now();
$startedTerminating = CarbonImmutable::now();

// Here we will wait until all of the child supervisors finish terminating and
// then exit the process. We will keep track of a timeout value so that the
// process does not get stuck in an infinite loop here waiting for these.
while (count($this->supervisors->filter->isRunning())) {
if (Chronos::now()->subSeconds($longest)
if (CarbonImmutable::now()->subSeconds($longest)
->gte($startedTerminating)) {
break;
}
Expand Down
6 changes: 3 additions & 3 deletions src/ProcessPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Laravel\Horizon;

use Cake\Chronos\Chronos;
use Carbon\CarbonImmutable;
use Closure;
use Countable;
use Symfony\Component\Process\Process;
Expand Down Expand Up @@ -137,7 +137,7 @@ protected function scaleDown($processes)
public function markForTermination(WorkerProcess $process)
{
$this->terminatingProcesses[] = [
'process' => $process, 'terminatedAt' => Chronos::now(),
'process' => $process, 'terminatedAt' => CarbonImmutable::now(),
];
}

Expand Down Expand Up @@ -270,7 +270,7 @@ protected function stopTerminatingProcessesThatAreHanging()
foreach ($this->terminatingProcesses as $process) {
$timeout = $this->options->timeout;

if ($process['terminatedAt']->addSeconds($timeout)->lte(Chronos::now())) {
if ($process['terminatedAt']->addSeconds($timeout)->lte(CarbonImmutable::now())) {
$process['process']->stop();
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/Repositories/RedisJobRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Laravel\Horizon\Repositories;

use Cake\Chronos\Chronos;
use Carbon\CarbonImmutable;
use Illuminate\Contracts\Redis\Factory as RedisFactory;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -238,7 +238,7 @@ protected function countJobsByType($type)
$minutes = $this->minutesForType($type);

return $this->connection()->zcount(
$type, '-inf', Chronos::now()->subMinutes($minutes)->getTimestamp() * -1
$type, '-inf', CarbonImmutable::now()->subMinutes($minutes)->getTimestamp() * -1
);
}

Expand Down Expand Up @@ -334,7 +334,7 @@ public function pushed($connection, $queue, JobPayload $payload)
]);

$pipe->expireat(
$payload->id(), Chronos::now()->addMinutes($this->pendingJobExpires)->getTimestamp()
$payload->id(), CarbonImmutable::now()->addMinutes($this->pendingJobExpires)->getTimestamp()
);
});
}
Expand Down Expand Up @@ -406,7 +406,7 @@ public function remember($connection, $queue, JobPayload $payload)
);

$pipe->expireat(
$payload->id(), Chronos::now()->addMinutes($this->monitoredJobExpires)->getTimestamp()
$payload->id(), CarbonImmutable::now()->addMinutes($this->monitoredJobExpires)->getTimestamp()
);
});
}
Expand Down Expand Up @@ -458,7 +458,7 @@ public function completed(JobPayload $payload, $failed = false)
]
);

$pipe->expireat($payload->id(), Chronos::now()->addMinutes($this->completedJobExpires)->getTimestamp());
$pipe->expireat($payload->id(), CarbonImmutable::now()->addMinutes($this->completedJobExpires)->getTimestamp());
});
}

Expand Down Expand Up @@ -509,7 +509,7 @@ public function deleteMonitored(array $ids)
{
$this->connection()->pipeline(function ($pipe) use ($ids) {
foreach ($ids as $id) {
$pipe->expireat($id, Chronos::now()->addDays(7)->getTimestamp());
$pipe->expireat($id, CarbonImmutable::now()->addDays(7)->getTimestamp());
}
});
}
Expand All @@ -524,25 +524,25 @@ public function trimRecentJobs()
$this->connection()->pipeline(function ($pipe) {
$pipe->zremrangebyscore(
'recent_jobs',
Chronos::now()->subMinutes($this->recentJobExpires)->getTimestamp() * -1,
CarbonImmutable::now()->subMinutes($this->recentJobExpires)->getTimestamp() * -1,
'+inf'
);

$pipe->zremrangebyscore(
'recent_failed_jobs',
Chronos::now()->subMinutes($this->recentFailedJobExpires)->getTimestamp() * -1,
CarbonImmutable::now()->subMinutes($this->recentFailedJobExpires)->getTimestamp() * -1,
'+inf'
);

$pipe->zremrangebyscore(
'pending_jobs',
Chronos::now()->subMinutes($this->pendingJobExpires)->getTimestamp() * -1,
CarbonImmutable::now()->subMinutes($this->pendingJobExpires)->getTimestamp() * -1,
'+inf'
);

$pipe->zremrangebyscore(
'completed_jobs',
Chronos::now()->subMinutes($this->completedJobExpires)->getTimestamp() * -1,
CarbonImmutable::now()->subMinutes($this->completedJobExpires)->getTimestamp() * -1,
'+inf'
);
});
Expand All @@ -556,7 +556,7 @@ public function trimRecentJobs()
public function trimFailedJobs()
{
$this->connection()->zremrangebyscore(
'failed_jobs', Chronos::now()->subMinutes($this->failedJobExpires)->getTimestamp() * -1, '+inf'
'failed_jobs', CarbonImmutable::now()->subMinutes($this->failedJobExpires)->getTimestamp() * -1, '+inf'
);
}

Expand All @@ -568,7 +568,7 @@ public function trimFailedJobs()
public function trimMonitoredJobs()
{
$this->connection()->zremrangebyscore(
'monitored_jobs', Chronos::now()->subMinutes($this->monitoredJobExpires)->getTimestamp() * -1, '+inf'
'monitored_jobs', CarbonImmutable::now()->subMinutes($this->monitoredJobExpires)->getTimestamp() * -1, '+inf'
);
}

Expand Down Expand Up @@ -624,7 +624,7 @@ public function failed($exception, $connection, $queue, JobPayload $payload)
);

$pipe->expireat(
$payload->id(), Chronos::now()->addMinutes($this->failedJobExpires)->getTimestamp()
$payload->id(), CarbonImmutable::now()->addMinutes($this->failedJobExpires)->getTimestamp()
);
});
}
Expand Down Expand Up @@ -669,7 +669,7 @@ public function storeRetryReference($id, $retryId)
$retries[] = [
'id' => $retryId,
'status' => 'pending',
'retried_at' => Chronos::now()->getTimestamp(),
'retried_at' => CarbonImmutable::now()->getTimestamp(),
];

$this->connection()->hmset($id, ['retried_by' => json_encode($retries)]);
Expand Down
8 changes: 4 additions & 4 deletions src/Repositories/RedisMasterSupervisorRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Laravel\Horizon\Repositories;

use Cake\Chronos\Chronos;
use Carbon\CarbonImmutable;
use Illuminate\Contracts\Redis\Factory as RedisFactory;
use Illuminate\Support\Arr;
use Laravel\Horizon\Contracts\MasterSupervisorRepository;
Expand Down Expand Up @@ -37,7 +37,7 @@ public function __construct(RedisFactory $redis)
public function names()
{
return $this->connection()->zrevrangebyscore('masters', '+inf',
Chronos::now()->subSeconds(14)->getTimestamp()
CarbonImmutable::now()->subSeconds(14)->getTimestamp()
);
}

Expand Down Expand Up @@ -109,7 +109,7 @@ public function update(MasterSupervisor $master)
);

$pipe->zadd('masters',
Chronos::now()->getTimestamp(), $master->name
CarbonImmutable::now()->getTimestamp(), $master->name
);

$pipe->expire('master:'.$master->name, 15);
Expand Down Expand Up @@ -145,7 +145,7 @@ public function forget($name)
public function flushExpired()
{
$this->connection()->zremrangebyscore('masters', '-inf',
Chronos::now()->subSeconds(14)->getTimestamp()
CarbonImmutable::now()->subSeconds(14)->getTimestamp()
);
}

Expand Down
Loading

0 comments on commit 8dfcb2a

Please sign in to comment.