Skip to content

Commit

Permalink
feat: add horizon server details to queue
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasbacsai committed Jan 10, 2025
1 parent b8f1ded commit 765e1ea
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 4 deletions.
16 changes: 14 additions & 2 deletions app/Console/Commands/HorizonManage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
use Illuminate\Support\Facades\Artisan;
use Laravel\Horizon\Contracts\JobRepository;
use Laravel\Horizon\Contracts\MetricsRepository;
use Laravel\Horizon\Repositories\RedisJobRepository;

use function Laravel\Prompts\multiselect;
use function Laravel\Prompts\select;
use function Laravel\Prompts\table;

class HorizonManage extends Command
{
protected $signature = 'horizon:manage ';
protected $signature = 'horizon:manage';

protected $description = 'Manage horizon';

Expand All @@ -28,6 +29,7 @@ public function handle()
'workers' => 'Workers',
'failed' => 'Failed Jobs',
'failed-delete' => 'Failed Jobs - Delete',
'purge-queues' => 'Purge Queues',
]
);

Expand Down Expand Up @@ -102,11 +104,11 @@ public function handle()

return;
}
dump($runningJobs);
foreach ($runningJobs as $runningJob) {
$runningJobsTable[] = [
'id' => $runningJob->id,
'name' => $runningJob->name,
'reserved_at' => $runningJob->reserved_at ? now()->parse($runningJob->reserved_at)->format('Y-m-d H:i:s') : null,
];
}
table($runningJobsTable);
Expand All @@ -123,5 +125,15 @@ public function handle()
}
table($workersTable);
}

if ($action === 'purge-queues') {
$getQueues = app(CustomJobRepository::class)->getQueues();
$queueName = select(
label: 'Which queue to purge?',
options: $getQueues,
);
$redisJobRepository = app(RedisJobRepository::class);
$redisJobRepository->purge($queueName);
}
}
}
5 changes: 4 additions & 1 deletion app/Jobs/ApplicationDeploymentJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ public function handle(): void
{
$this->application_deployment_queue->update([
'status' => ApplicationDeploymentStatus::IN_PROGRESS->value,
'horizon_job_worker' => gethostname(),
]);
if ($this->server->isFunctional() === false) {
$this->application_deployment_queue->addLogEntry('Server is not functional.');
Expand Down Expand Up @@ -2389,10 +2390,12 @@ private function next(string $status)
queue_next_deployment($this->application);
// If the deployment is cancelled by the user, don't update the status
if (
$this->application_deployment_queue->status !== ApplicationDeploymentStatus::CANCELLED_BY_USER->value && $this->application_deployment_queue->status !== ApplicationDeploymentStatus::FAILED->value
$this->application_deployment_queue->status !== ApplicationDeploymentStatus::CANCELLED_BY_USER->value &&
$this->application_deployment_queue->status !== ApplicationDeploymentStatus::FAILED->value
) {
$this->application_deployment_queue->update([
'status' => $status,
'horizon_job_status' => $status,
]);
}
if ($this->application_deployment_queue->status === ApplicationDeploymentStatus::FAILED->value) {
Expand Down
1 change: 1 addition & 0 deletions app/Livewire/Project/Application/DeploymentNavbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function cancel()
$this->application_deployment_queue->update([
'current_process_id' => null,
'status' => ApplicationDeploymentStatus::CANCELLED_BY_USER->value,
'horizon_job_status' => ApplicationDeploymentStatus::CANCELLED_BY_USER->value,
]);
next_after_cancel($server);
}
Expand Down
21 changes: 20 additions & 1 deletion app/Providers/HorizonServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace App\Providers;

use App\Contracts\CustomJobRepositoryInterface;
use App\Models\ApplicationDeploymentQueue;
use App\Repositories\CustomJobRepository;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;
use Laravel\Horizon\Contracts\JobRepository;
use Laravel\Horizon\Events\JobReserved;

class HorizonServiceProvider extends ServiceProvider
{
Expand All @@ -23,6 +26,22 @@ public function register(): void
*/
public function boot(): void
{
//
// Event::listen(function (JobReserved $event) {
// $payload = $event->payload->decoded;
// $jobName = $payload['displayName'];
// if ($jobName === 'App\Jobs\ApplicationDeploymentJob') {
// $tags = $payload['tags'];

// $deploymentQueueId = collect($tags)->first(function ($tag) {
// return str_contains($tag, 'App\Models\ApplicationDeploymentQueue');
// });
// $deploymentQueueId = explode(':', $deploymentQueueId)[1];
// $deploymentQueue = ApplicationDeploymentQueue::find($deploymentQueueId);
// $deploymentQueue->update([
// 'horizon_job_status' => 'reserved',
// ]);
// }
// });

}
}
10 changes: 10 additions & 0 deletions app/Repositories/CustomJobRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,14 @@ public function getLongRunningJobs(int $seconds): Collection

return $jobs;
}

public function getQueues(): array
{
$queues = $this->connection()->keys('queue:*');
$queues = array_map(function ($queue) {
return explode(':', $queue)[2];
}, $queues);

return $queues;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('application_deployment_queues', function (Blueprint $table) {
$table->string('horizon_job_status')->nullable();
$table->string('horizon_job_worker')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('application_deployment_queues', function (Blueprint $table) {
$table->dropColumn('horizon_job_status');
$table->dropColumn('horizon_job_worker');
});
}
};

0 comments on commit 765e1ea

Please sign in to comment.