refactor scheduled task job (and related stuffs)

This commit is contained in:
Andras Bacsai
2024-11-07 11:09:38 +01:00
parent 8e3469bdff
commit 376a2341af
9 changed files with 220 additions and 87 deletions

View File

@@ -2,23 +2,60 @@
namespace App\Livewire\Project\Shared\ScheduledTask;
use App\Models\ScheduledTask;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Livewire\Attributes\Locked;
use Livewire\Component;
class Executions extends Component
{
public $executions = [];
public ScheduledTask $task;
public $selectedKey;
#[Locked]
public int $taskId;
public $task;
#[Locked]
public Collection $executions;
#[Locked]
public ?int $selectedKey = null;
#[Locked]
public ?string $serverTimezone = null;
public function getListeners()
{
$teamId = Auth::user()->currentTeam()->id;
return [
'selectTask',
"echo-private:team.{$teamId},ScheduledTaskDone" => 'refreshExecutions',
];
}
public function mount($taskId)
{
try {
$this->taskId = $taskId;
$this->task = ScheduledTask::findOrFail($taskId);
$this->executions = $this->task->executions()->take(20)->get();
$this->serverTimezone = data_get($this->task, 'application.destination.server.settings.server_timezone');
if (! $this->serverTimezone) {
$this->serverTimezone = data_get($this->task, 'service.destination.server.settings.server_timezone');
}
if (! $this->serverTimezone) {
$this->serverTimezone = 'UTC';
}
} catch (\Exception $e) {
return handleError($e);
}
}
public function refreshExecutions(): void
{
$this->executions = $this->task->executions()->take(20)->get();
}
public function selectTask($key): void
{
if ($key == $this->selectedKey) {
@@ -29,38 +66,9 @@ class Executions extends Component
$this->selectedKey = $key;
}
public function server()
{
if (! $this->task) {
return null;
}
if ($this->task->application) {
if ($this->task->application->destination && $this->task->application->destination->server) {
return $this->task->application->destination->server;
}
} elseif ($this->task->service) {
if ($this->task->service->destination && $this->task->service->destination->server) {
return $this->task->service->destination->server;
}
}
return null;
}
public function getServerTimezone()
{
$server = $this->server();
if (! $server) {
return 'UTC';
}
return $server->settings->server_timezone;
}
public function formatDateInServerTimezone($date)
{
$serverTimezone = $this->getServerTimezone();
$serverTimezone = $this->serverTimezone;
$dateObj = new \DateTime($date);
try {
$dateObj->setTimezone(new \DateTimeZone($serverTimezone));