Simplified Task runner even more, moved logic out of shell scripting to the tool itself.

This commit is contained in:
Abdulmhsen B. A. A
2022-04-19 19:04:02 +03:00
parent cd416317e6
commit 6fedb5159d
3 changed files with 54 additions and 47 deletions

View File

@@ -1,29 +1,10 @@
#!/usr/bin/env sh
UID=$(id -u)
NOW=$(date +"%Y_%m_%d")
WS_UID=${WS_UID:-1000}
WS_GID=${WS_GID:-1000}
WS_CRON_DEBUG=${WS_CRON_DEBUG:-v}
# check for data path.
if [ -z "${WS_DATA_PATH}" ]; then
WS_DATA_PATH="/config"
fi
LOGFILE="${WS_DATA_PATH}/logs/cron/${NOW}.log"
# Check cron logs path.
if [ ! -d "${WS_DATA_PATH}/logs/cron/" ]; then
if [ 0 == "${UID}" ]; then
runuser -u www-data -- mkdir -p "${WS_DATA_PATH}/logs/cron/"
else
mkdir -p "${WS_DATA_PATH}/logs/cron/"
fi
fi
if [ 0 == "${UID}" ]; then
runuser -u www-data -- /usr/bin/console scheduler:run -o -${WS_CRON_DEBUG} >>${LOGFILE}
runuser -u www-data -- /usr/bin/console scheduler:run --save-log -${WS_CRON_DEBUG}
else
/usr/bin/console scheduler:run -o -${WS_CRON_DEBUG} >>${LOGFILE}
/usr/bin/console scheduler:run --save-log -${WS_CRON_DEBUG}
fi

View File

@@ -6,6 +6,7 @@ namespace App\Commands\Scheduler;
use App\Command;
use App\Libs\Config;
use App\Libs\Extends\ConsoleOutput;
use App\Libs\Scheduler\Scheduler;
use App\Libs\Scheduler\Task;
use App\Libs\Scheduler\TaskTimer;
@@ -22,6 +23,7 @@ final class RunCommand extends Command
{
private Scheduler $scheduler;
private array $registered = [];
private array $logs = [];
public function __construct(Scheduler $scheduler)
{
@@ -33,8 +35,8 @@ final class RunCommand extends Command
protected function configure(): void
{
$this->setName('scheduler:run')
->addOption('show-output', 'o', InputOption::VALUE_NONE, 'Show tasks output.')
->addOption('no-headers', 'g', InputOption::VALUE_NONE, 'Do not prefix output with headers.')
->addOption('save-log', null, InputOption::VALUE_NONE, 'Save Tasks Output to file.')
->addArgument('task', InputArgument::OPTIONAL, 'Run specific task.', null)
->setDescription('Run Scheduled Tasks.');
}
@@ -74,43 +76,62 @@ final class RunCommand extends Command
$count = count($executedTasks);
if (0 === $count) {
$output->writeln(
$this->write(
'!{date} <info>No Tasks Scheduled to run at this time.</info>',
$input,
$output,
OutputInterface::VERBOSITY_VERY_VERBOSE
);
}
if ($input->getOption('show-output')) {
$tasks = array_reverse($executedTasks);
$noHeaders = (bool)$input->getOption('no-headers');
$tasks = array_reverse($executedTasks);
$noHeaders = (bool)$input->getOption('no-headers');
foreach ($tasks as $task) {
$taskOutput = trim($task->getOutput());
foreach ($tasks as $task) {
$taskOutput = trim($task->getOutput());
if (empty($taskOutput)) {
continue;
}
if (empty($taskOutput)) {
continue;
}
if (false === $noHeaders) {
$output->writeln('--------------------------');
$output->writeln('Command: ' . $task->getCommand() . ' ' . $task->getArgs());
$output->writeln('--------------------------');
$output->writeln(sprintf('Task %s Output.', $task->getName()));
$output->writeln('--------------------------');
$output->writeln('');
}
if (false === $noHeaders) {
$this->write('--------------------------', $input, $output);
$this->write('Command: ' . $task->getCommand() . ' ' . $task->getArgs(), $input, $output);
$this->write('--------------------------', $input, $output);
$this->write(sprintf('Task %s Output.', $task->getName()), $input, $output);
$this->write('--------------------------', $input, $output);
$this->write('', $input, $output);
}
$output->writeln($taskOutput);
$this->write($taskOutput, $input, $output);
if (false === $noHeaders) {
$output->writeln('');
}
if (false === $noHeaders) {
$this->write('', $input, $output);
}
}
if ($input->getOption('save-log')) {
$logfile = Config::get('tmpDir') . '/logs/crons/' . gmdate('Y_m_d') . '.log';
file_put_contents($logfile, implode(PHP_EOL, $this->logs) . PHP_EOL, FILE_APPEND);
}
return self::SUCCESS;
}
private function write(
string $text,
InputInterface $input,
OutputInterface $output,
int $level = OutputInterface::OUTPUT_NORMAL
): void {
assert($output instanceof ConsoleOutput);
$output->writeln($text, $level);
if ($input->getOption('save-log')) {
$this->logs[] = $output->getLastMessage();
}
}
public static function getTasks(): array
{
$tasks = [];

View File

@@ -4,18 +4,23 @@ declare(strict_types=1);
namespace App\Libs\Extends;
use Exception;
use Symfony\Component\Console\Output\ConsoleOutput as baseConsoleOutput;
final class ConsoleOutput extends baseConsoleOutput
{
/**
* @throws Exception
*/
private mixed $message = '';
protected function doWrite(string $message, bool $newline): void
{
$message = str_replace('!{date}', '[' . makeDate('now') . ']', $message);
$this->message = $message;
parent::doWrite($message, $newline);
}
public function getLastMessage(): mixed
{
return $this->message;
}
}