Skip to content
Code-Schnipsel Gruppen Projekte
Console.php 5,47 KiB
Newer Older
<?php

/**
 * This file contains QUI\Cron\Console
 */

namespace QUI\Cron;

Henning Leutz's avatar
Henning Leutz committed
 * @author www.pcsg.de (Henning Leutz)
class Console extends QUI\System\Console\Tool
{
    /**
     * Konstruktor
     */
    public function __construct()
    {
        $this->setName('package:cron')
            ->setDescription('Cron Manager');
Henning Leutz's avatar
Henning Leutz committed
     *
     * @see \QUI\System\Console\Tool::execute()
     */
    public function execute()
    {
        $run     = $this->getArgument('--run');
        $list    = $this->getArgument('--list');
        $listall = $this->getArgument('--list-all');
        $runCron = $this->getArgument('--cron');
Henning Leutz's avatar
Henning Leutz committed
        if ($run) {
            $this->run();
            return;
        }

Henning Leutz's avatar
Henning Leutz committed
        if ($list) {
            $this->listCrons();
            return;
        }

Henning Leutz's avatar
Henning Leutz committed
        if ($listall) {
            $this->listAllCrons();
Henning Leutz's avatar
Henning Leutz committed

        if ($runCron) {
            $this->runCron($runCron);
Henning Leutz's avatar
Henning Leutz committed
        $this->writeLn('Welcome to the Cron Manager');
        $this->writeLn('Which Command would you execute?');
        $this->writeLn('');

        $this->commandRead();
    }

    /**
     * Read the command from the command line
     */
    public function commandRead()
    {
Henning Leutz's avatar
Henning Leutz committed
        $this->writeLn('Available Commands: ');
        $this->writeLn("- run\t\trun all active crons");
        $this->writeLn("- list\t\tlist all active crons");
        $this->writeLn("- list-all\tlist all crons");
        $this->writeLn("- cron\trun a specific cron");
Henning Leutz's avatar
Henning Leutz committed
        $this->writeLn('');
Henning Leutz's avatar
Henning Leutz committed
        $this->writeLn('Command: ');
Henning Leutz's avatar
Henning Leutz committed
        switch ($command) {
Henning Leutz's avatar
Henning Leutz committed
            // run all crons
            case 'run':
                $this->run();
                $this->commandRead();
Henning Leutz's avatar
Henning Leutz committed
                break;
Henning Leutz's avatar
Henning Leutz committed
            // list all inserted crons
            case 'list':
                $this->listCrons();
                $this->commandRead();
Henning Leutz's avatar
Henning Leutz committed
                break;
Henning Leutz's avatar
Henning Leutz committed

            // list all inserted crons
            case 'list-all':
                $this->listAllCrons();
                $this->commandRead();
Henning Leutz's avatar
Henning Leutz committed
                break;
            case 'cron':
                $this->write("Please enter the Cron-ID: ");
                $cronId = $this->readInput();

                try {
                    $this->runCron($cronId);
                } catch (QUI\Exception $Exception) {
                    $this->writeLn($Exception->getMessage(), 'red');
                    $this->resetColor();
                    $this->writeLn('');
                }

                $this->commandRead();
                break;

                $this->writeLn(
                    'Command not found, please type another command',
                    'red'
                );

                $this->commandRead();
        }
    }

    /**
     * Execute all upcoming crons
     */
    public function run()
    {
Henning Leutz's avatar
Henning Leutz committed
        $Manager = new Manager();
Henning Leutz's avatar
Henning Leutz committed
        $this->writeLn('');
        $this->write('Execute all upcoming crons ...');
        $Manager->execute();

        $this->write('finish');
Henning Leutz's avatar
Henning Leutz committed
        $this->writeLn('');
Henning Leutz's avatar
Henning Leutz committed

    /**
     * List all active crons
     */
    public function listCrons()
    {
Henning Leutz's avatar
Henning Leutz committed
        $Manager = new Manager();
        $list    = $Manager->getList();
Henning Leutz's avatar
Henning Leutz committed
        $this->writeLn('Cron list:');
        $this->writeLn('=======================================================');
        $this->writeLn('');
Henning Leutz's avatar
Henning Leutz committed
        foreach ($list as $entry) {
            if ($entry['active'] != 1) {
            $time = $entry['min']
                    . ' ' . $entry['hour']
                    . ' ' . $entry['day']
                    . ' ' . $entry['month'];

Henning Leutz's avatar
Henning Leutz committed
            $exec = $entry['exec'];

            $this->writeLn('ID: ' . $entry['id']);
            $this->writeLn($time . "\t" . $exec, 'green');
Henning Leutz's avatar
Henning Leutz committed

            $this->resetColor();
Henning Leutz's avatar
Henning Leutz committed
            $this->writeLn('');
Henning Leutz's avatar
Henning Leutz committed
        $this->writeLn('=======================================================');
        $this->writeLn('');
Henning Leutz's avatar
Henning Leutz committed
    }

    /**
     * List all inserted Crons
     */
    public function listAllCrons()
    {
Henning Leutz's avatar
Henning Leutz committed
        $Manager = new Manager();
        $list    = $Manager->getList();
Henning Leutz's avatar
Henning Leutz committed
        $this->writeLn('Cron list:');
        $this->writeLn('=======================================================');
        $this->writeLn('');
Henning Leutz's avatar
Henning Leutz committed
        foreach ($list as $entry) {
            $time = $entry['min']
                    . ' ' . $entry['hour']
                    . ' ' . $entry['day']
                    . ' ' . $entry['month'];

Henning Leutz's avatar
Henning Leutz committed
            $exec = $entry['exec'];

            $this->writeLn('ID: ' . $entry['id']);
            $this->writeLn($time . "\t" . $exec, 'green');
Henning Leutz's avatar
Henning Leutz committed

            $this->resetColor();
Henning Leutz's avatar
Henning Leutz committed
            $this->writeLn('');
Henning Leutz's avatar
Henning Leutz committed
        $this->writeLn('=======================================================');
        $this->writeLn('');

    /**
     * Run a specific cron
     *
     * @param Boolean|Integer $cronId - ID of the cron
     * @throws QUI\Exception
     */
    public function runCron($cronId = false)
    {
        $Manager = new Manager();
        $cron    = $Manager->getCronById($cronId);

        if (!$cron) {
            throw new QUI\Exception('Cron not found');
        }

        $this->writeLn('Execute Cron: ' . $cronId. ' '. $cron['title']);
        $Manager->executeCron($cronId);

        $this->writeLn('=======================================================');
        $this->writeLn('');
    }