appwrite/src/Appwrite/Platform/Tasks/VolumeSync.php

60 lines
1.9 KiB
PHP
Raw Normal View History

2022-11-02 14:56:03 +00:00
<?php
2022-11-14 10:01:41 +00:00
namespace Appwrite\Platform\Tasks;
2022-11-02 14:56:03 +00:00
use Utopia\CLI\Console;
use Utopia\Database\DateTime;
2022-11-13 08:36:31 +00:00
use Utopia\Platform\Action;
2022-11-02 14:56:03 +00:00
use Utopia\Validator\Integer;
use Utopia\Validator\Text;
2022-11-13 08:36:31 +00:00
class VolumeSync extends Action
{
public static function getName(): string
{
return 'volume-sync';
}
public function __construct()
{
$this
->desc('Runs rsync to sync certificates between the storage mount and traefik.')
->param('source', null, new Text(255), 'Source path to sync from.', false)
->param('destination', null, new Text(255), 'Destination path to sync to.', false)
->param('interval', null, new Integer(true), 'Interval to run rsync', false)
2022-11-14 10:38:17 +00:00
->callback(fn ($source, $destination, $interval) => $this->action($source, $destination, $interval));
2022-11-13 08:36:31 +00:00
}
public function action(string $source, string $destination, int $interval)
{
2022-11-02 14:56:03 +00:00
Console::title('RSync V1');
Console::success(APP_NAME . ' rsync process v1 has started');
2022-11-07 11:47:51 +00:00
if (!file_exists($source)) {
Console::error('Source directory does not exist. Exiting ... ');
2022-11-07 12:06:42 +00:00
Console::exit(0);
2022-11-07 11:47:51 +00:00
}
2022-11-02 14:56:03 +00:00
Console::loop(function () use ($interval, $source, $destination) {
$time = DateTime::now();
Console::info("[{$time}] Executing rsync every {$interval} seconds");
Console::info("Syncing between $source and $destination");
if (!file_exists($source)) {
Console::error('Source directory does not exist. Skipping ... ');
return;
}
$stdin = "";
$stdout = "";
$stderr = "";
Console::execute("rsync -av $source $destination", $stdin, $stdout, $stderr);
Console::success($stdout);
Console::error($stderr);
}, $interval);
2022-11-13 08:36:31 +00:00
}
}