timeStart = hrtime(true); $user = $this->activity->getExtraProperty('user'); $destination = $this->activity->getExtraProperty('destination'); $port = $this->activity->getExtraProperty('port'); $command = $this->activity->getExtraProperty('command'); $delimiter = 'EOF-COOLIFY-SSH'; $sshCommand = 'ssh ' . '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ' . '-o PasswordAuthentication=no ' . "-p {$port} " . "{$user}@{$destination} " . " 'bash -se' << \\$delimiter" . PHP_EOL . $command . PHP_EOL . $delimiter; $process = Process::start($sshCommand, $this->handleOutput(...)); $res = $process->wait(); if (app()->environment('testing')) { return $res; } $status = match ($res->exitCode()) { 0 => ProcessStatus::FINISHED, default => ProcessStatus::ERROR, }; $this->activity->properties = $this->activity->properties->merge([ 'exitCode' => $res->exitCode(), 'stdout' => $res->output(), 'stderr' => $res->errorOutput(), 'status' => $status, ]); $this->activity->save(); return $res; } protected function handleOutput(string $type, string $output) { $this->currentTime = $this->elapsedTime(); if ($type === 'out') { $this->stdOutIncremental .= $output; } else { $this->stdErrIncremental .= $output; } $this->activity->description .= $output; if ($this->isAfterLastThrottle()) { // Let's write to database. DB::transaction(function () { $this->activity->save(); $this->lastWriteAt = $this->currentTime; }); } } /** * Decides if it's time to write again to database. * * @return bool */ protected function isAfterLastThrottle() { // If DB was never written, then we immediately decide we have to write. if ($this->lastWriteAt === 0) { return true; } return ($this->currentTime - $this->throttleIntervalMS) > $this->lastWriteAt; } protected function elapsedTime(): int { $timeMs = (hrtime(true) - $this->timeStart) / 1_000_000; return intval($timeMs); } }