update scheduled jobs
This commit is contained in:
@@ -21,60 +21,56 @@ class ContainerStatusJob implements ShouldQueue
|
|||||||
}
|
}
|
||||||
public function handle(): void
|
public function handle(): void
|
||||||
{
|
{
|
||||||
if ($this->container_id) {
|
try {
|
||||||
$this->checkContainerStatus();
|
if ($this->container_id) {
|
||||||
} else {
|
$this->checkContainerStatus();
|
||||||
$this->checkAllServers();
|
} else {
|
||||||
|
$this->checkAllServers();
|
||||||
|
}
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::error($e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
protected function checkAllServers()
|
protected function checkAllServers()
|
||||||
{
|
{
|
||||||
try {
|
$servers = Server::all()->reject(fn (Server $server) => $server->settings->is_build_server);
|
||||||
$servers = Server::all()->reject(fn (Server $server) => $server->settings->is_build_server);
|
$applications = Application::all();
|
||||||
$applications = Application::all();
|
$not_found_applications = $applications;
|
||||||
$not_found_applications = $applications;
|
$containers = collect();
|
||||||
$containers = collect();
|
foreach ($servers as $server) {
|
||||||
foreach ($servers as $server) {
|
$output = runRemoteCommandSync($server, ['docker ps -a -q --format \'{{json .}}\'']);
|
||||||
$output = runRemoteCommandSync($server, ['docker ps -a -q --format \'{{json .}}\'']);
|
$containers = $containers->concat(formatDockerCmdOutputToJson($output));
|
||||||
$containers = $containers->concat(formatDockerCmdOutputToJson($output));
|
}
|
||||||
|
foreach ($containers as $container) {
|
||||||
|
$found_application = $applications->filter(function ($value, $key) use ($container) {
|
||||||
|
return $value->uuid == $container['Names'];
|
||||||
|
})->first();
|
||||||
|
if ($found_application) {
|
||||||
|
$not_found_applications = $not_found_applications->filter(function ($value, $key) use ($found_application) {
|
||||||
|
return $value->uuid != $found_application->uuid;
|
||||||
|
});
|
||||||
|
$found_application->status = $container['State'];
|
||||||
|
$found_application->save();
|
||||||
|
Log::info('Found application: ' . $found_application->uuid . '. Set status to: ' . $found_application->status);
|
||||||
}
|
}
|
||||||
foreach ($containers as $container) {
|
}
|
||||||
$found_application = $applications->filter(function ($value, $key) use ($container) {
|
foreach ($not_found_applications as $not_found_application) {
|
||||||
return $value->uuid == $container['Names'];
|
$not_found_application->status = 'exited';
|
||||||
})->first();
|
$not_found_application->save();
|
||||||
if ($found_application) {
|
Log::info('Not found application: ' . $not_found_application->uuid . '. Set status to: ' . $not_found_application->status);
|
||||||
$not_found_applications = $not_found_applications->filter(function ($value, $key) use ($found_application) {
|
|
||||||
return $value->uuid != $found_application->uuid;
|
|
||||||
});
|
|
||||||
$found_application->status = $container['State'];
|
|
||||||
$found_application->save();
|
|
||||||
Log::info('Found application: ' . $found_application->uuid . '. Set status to: ' . $found_application->status);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
foreach ($not_found_applications as $not_found_application) {
|
|
||||||
$not_found_application->status = 'exited';
|
|
||||||
$not_found_application->save();
|
|
||||||
Log::info('Not found application: ' . $not_found_application->uuid . '. Set status to: ' . $not_found_application->status);
|
|
||||||
}
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
Log::error($e->getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
protected function checkContainerStatus()
|
protected function checkContainerStatus()
|
||||||
{
|
{
|
||||||
try {
|
$application = Application::where('uuid', $this->container_id)->firstOrFail();
|
||||||
$application = Application::where('uuid', $this->container_id)->firstOrFail();
|
if (!$application) {
|
||||||
if (!$application) {
|
return;
|
||||||
return;
|
}
|
||||||
}
|
if ($application->destination->server) {
|
||||||
if ($application->destination->server) {
|
$container = runRemoteCommandSync($application->destination->server, ["docker inspect --format '{{json .State}}' {$this->container_id}"]);
|
||||||
$container = runRemoteCommandSync($application->destination->server, ["docker inspect --format '{{json .State}}' {$this->container_id}"]);
|
$container = formatDockerCmdOutputToJson($container);
|
||||||
$container = formatDockerCmdOutputToJson($container);
|
$application->status = $container[0]['Status'];
|
||||||
$application->status = $container[0]['Status'];
|
$application->save();
|
||||||
$application->save();
|
|
||||||
}
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
Log::error($e->getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -28,9 +28,13 @@ class DockerCleanupDanglingImagesJob implements ShouldQueue
|
|||||||
*/
|
*/
|
||||||
public function handle(): void
|
public function handle(): void
|
||||||
{
|
{
|
||||||
$servers = Server::all();
|
try {
|
||||||
foreach ($servers as $server) {
|
$servers = Server::all();
|
||||||
runRemoteCommandSync($server, ['docker image prune -f']);
|
foreach ($servers as $server) {
|
||||||
|
runRemoteCommandSync($server, ['docker image prune -f']);
|
||||||
|
}
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::error($e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user