mirror of
https://github.com/coollabsio/coolify.git
synced 2026-11-04 10:42:23 +00:00
Containers without sh (distroless or scratch images) could not run scheduled tasks, because Coolify always ran the command with sh -c. Turn off "Run in a shell" to split the command into arguments and run it directly. The option is also in the API, the MCP tools, and the server transfer. Closes #8244 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
163 lines
4.6 KiB
PHP
163 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Project\Shared\ScheduledTask;
|
|
|
|
use App\Models\Application;
|
|
use App\Models\ScheduledTask;
|
|
use App\Models\Service;
|
|
use App\Models\StandalonePostgresql;
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
use Illuminate\Support\Collection;
|
|
use Livewire\Attributes\Locked;
|
|
use Livewire\Component;
|
|
|
|
class Add extends Component
|
|
{
|
|
use AuthorizesRequests;
|
|
|
|
public $parameters;
|
|
|
|
#[Locked]
|
|
public string $id;
|
|
|
|
#[Locked]
|
|
public string $type;
|
|
|
|
#[Locked]
|
|
public Collection $containerNames;
|
|
|
|
#[Locked]
|
|
public $resource;
|
|
|
|
public string $name;
|
|
|
|
public string $command;
|
|
|
|
public string $frequency;
|
|
|
|
public ?string $container = '';
|
|
|
|
public int $timeout = 300;
|
|
|
|
public bool $useShell = true;
|
|
|
|
#[Locked]
|
|
public string $serverTimezone = 'UTC';
|
|
|
|
protected $rules = [
|
|
'name' => 'required|string',
|
|
'command' => 'required|string',
|
|
'frequency' => 'required|string',
|
|
'container' => 'nullable|string',
|
|
'timeout' => 'required|integer|min:60|max:36000',
|
|
'useShell' => 'boolean',
|
|
];
|
|
|
|
protected $validationAttributes = [
|
|
'name' => 'name',
|
|
'command' => 'command',
|
|
'frequency' => 'frequency',
|
|
'container' => 'container',
|
|
'timeout' => 'timeout',
|
|
'useShell' => 'run in a shell',
|
|
];
|
|
|
|
public function mount()
|
|
{
|
|
$this->parameters = get_route_parameters();
|
|
|
|
// Get the resource based on type and id
|
|
switch ($this->type) {
|
|
case 'application':
|
|
$this->resource = Application::ownedByCurrentTeam()->findOrFail($this->id);
|
|
break;
|
|
case 'service':
|
|
$this->resource = Service::ownedByCurrentTeam()->findOrFail($this->id);
|
|
break;
|
|
case 'standalone-postgresql':
|
|
$this->resource = StandalonePostgresql::ownedByCurrentTeam()->findOrFail($this->id);
|
|
break;
|
|
default:
|
|
throw new \Exception('Invalid resource type');
|
|
}
|
|
|
|
$this->serverTimezone = getServerTimezone($this->resource->destination?->server);
|
|
|
|
if ($this->containerNames->count() > 0) {
|
|
$this->container = $this->containerNames->first();
|
|
}
|
|
}
|
|
|
|
public function submit()
|
|
{
|
|
try {
|
|
$this->authorize('update', $this->resource);
|
|
$this->validate();
|
|
$isValid = validate_cron_expression($this->frequency);
|
|
if (! $isValid) {
|
|
$this->dispatch('error', 'Invalid Cron / Human expression.');
|
|
|
|
return;
|
|
}
|
|
if (empty($this->container) || $this->container === 'null') {
|
|
if ($this->type === 'service') {
|
|
$this->container = $this->subServiceName;
|
|
}
|
|
}
|
|
$this->saveScheduledTask();
|
|
$this->clear();
|
|
} catch (\Exception $e) {
|
|
return handleError($e, $this);
|
|
}
|
|
}
|
|
|
|
private function saveScheduledTask(): void
|
|
{
|
|
try {
|
|
$task = new ScheduledTask;
|
|
$task->name = $this->name;
|
|
$task->command = $this->command;
|
|
$task->frequency = $this->frequency;
|
|
$task->container = $this->container;
|
|
$task->timeout = $this->timeout;
|
|
$task->use_shell = $this->useShell;
|
|
$task->team_id = $this->resource->team()->id;
|
|
|
|
switch ($this->type) {
|
|
case 'application':
|
|
$task->application_id = $this->id;
|
|
break;
|
|
case 'standalone-postgresql':
|
|
$task->standalone_postgresql_id = $this->id;
|
|
break;
|
|
case 'service':
|
|
$task->service_id = $this->id;
|
|
break;
|
|
}
|
|
$task->save();
|
|
auditLog('ui.scheduled_task.created', [
|
|
'team_id' => $task->team_id,
|
|
'scheduled_task_uuid' => $task->uuid,
|
|
'scheduled_task_name' => $task->name,
|
|
'resource_type' => $this->type,
|
|
'resource_uuid' => $this->resource->uuid,
|
|
'resource_name' => $this->resource->name,
|
|
]);
|
|
$this->dispatch('refreshTasks');
|
|
$this->dispatch('success', 'Scheduled task added.');
|
|
} catch (\Throwable $e) {
|
|
handleError($e, $this);
|
|
}
|
|
}
|
|
|
|
public function clear()
|
|
{
|
|
$this->name = '';
|
|
$this->command = '';
|
|
$this->frequency = '';
|
|
$this->container = '';
|
|
$this->timeout = 300;
|
|
$this->useShell = true;
|
|
}
|
|
}
|