<commit_before><?php

namespace DelayedJobs\Shell;

use App\Shell\AppShell;
use Cake\Console\Shell;
use Cake\Core\Configure;
use Cake\Core\Exception\Exception;
use Cake\Event\Event;
use Cake\Event\EventManager;
use Cake\I18n\Time;
use Cake\ORM\TableRegistry;
use DelayedJobs\Amqp\AmqpManager;
use DelayedJobs\DelayedJob\Job;
use DelayedJobs\DelayedJob\Manager;
use DelayedJobs\DelayedJob\EnqueueTrait;
use DelayedJobs\Lock;
use DelayedJobs\Model\Entity\Worker;
use DelayedJobs\Model\Table\WorkersTable;
use DelayedJobs\Process;

/**
 * Class WatchdogShell
 *
 * @property \DelayedJobs\Model\Table\WorkersTable $Workers
 * @property \DelayedJobs\Model\Table\DelayedJobsTable $DelayedJobs
 */
class WatchdogShell extends AppShell
{
    use EnqueueTrait;

    const BASEPATH = ROOT . '/bin/cake DelayedJobs.worker ';
    public $Lock;
    public $modelClass = 'DelayedJobs.Workers';
    protected $_workers;

    /**
     * Creates (cpu-count - 1) worker processes (Minimum of 1 worker)
     *
     * @return int
     */
    protected function _autoWorker()
    {
        $worker_count = (int)exec('nproc') - 1;

        return $worker_count >= 1 ? $worker_count : 1;
    }

    protected function _welcome()
    {
        if (!Configure::check('dj.service.name')) {
            throw new Exception('Could not load config, check your load settings in bootstrap.php');
        }
        $hostname = php_uname('n');

        $this->clear();
        $this->out('App Name: <info>' . Configure::read('dj.service.name') . '</info>');
        $this->out('Hostname: <info>' . $hostname . '</info>');
        $this->hr();
    }

    /**
     * @return void
     */
    public function main()
    {
        if (file_exists(TMP . '/lockWatchdog')) {
            $this->out('Lock file exists, quiting', 1, Shell::VERBOSE);
            $this->_stop();
        }

        $this->out('Starting Watchdog');

        if ($this->param('workers') > 0) {
            $this->startWorkers();
        } else {
            $this->stopWorkers();
        }

        $this->recurring();
        $this->clean();

        $this->out('<success>!! All done !!</success>');
    }

    protected function _checkHeartbeat(Worker $worker)
    {
        $max_time = Configure::read('dj.max.execution.time');
        $last_beat = $worker->pulse->diffInSeconds();
        return $last_beat <= $max_time;
    }

    public function startWorkers($worker_count = null)
    {
        $worker_count = $worker_count ?: $this->param('workers');
        $max_workers = Configure::read('dj.max.workers');

        if ($worker_count > $max_workers) {
            $worker_count = $max_workers;
            $this->out('<error>Too many workers (max_workers:' . $max_workers . ')</error>');
        }

        $hostname = php_uname('n');
        $workers = $this->Workers->find('forHost', ['host' => $hostname]);

        $this->out(sprintf(' - Require <info>%d</info> Workers to be running. <info>%d</info> currently running.', $worker_count, $workers->count()));

        $this->out(' - Checking status of running workers.');

        foreach ($workers as $worker) {
            $this->_checkWorkerInstance($worker);
        }<commit_msg>Watchdog should not clean or check recurring jobs<commit_after><?php

namespace DelayedJobs\Shell;

use App\Shell\AppShell;
use Cake\Console\Shell;
use Cake\Core\Configure;
use Cake\Core\Exception\Exception;
use Cake\Event\Event;
use Cake\Event\EventManager;
use Cake\I18n\Time;
use Cake\ORM\TableRegistry;
use DelayedJobs\Amqp\AmqpManager;
use DelayedJobs\DelayedJob\Job;
use DelayedJobs\DelayedJob\Manager;
use DelayedJobs\DelayedJob\EnqueueTrait;
use DelayedJobs\Lock;
use DelayedJobs\Model\Entity\Worker;
use DelayedJobs\Model\Table\WorkersTable;
use DelayedJobs\Process;

/**
 * Class WatchdogShell
 *
 * @property \DelayedJobs\Model\Table\WorkersTable $Workers
 * @property \DelayedJobs\Model\Table\DelayedJobsTable $DelayedJobs
 */
class WatchdogShell extends AppShell
{
    use EnqueueTrait;

    const BASEPATH = ROOT . '/bin/cake DelayedJobs.worker ';
    public $Lock;
    public $modelClass = 'DelayedJobs.Workers';
    protected $_workers;

    /**
     * Creates (cpu-count - 1) worker processes (Minimum of 1 worker)
     *
     * @return int
     */
    protected function _autoWorker()
    {
        $worker_count = (int)exec('nproc') - 1;

        return $worker_count >= 1 ? $worker_count : 1;
    }

    protected function _welcome()
    {
        if (!Configure::check('dj.service.name')) {
            throw new Exception('Could not load config, check your load settings in bootstrap.php');
        }
        $hostname = php_uname('n');

        $this->clear();
        $this->out('App Name: <info>' . Configure::read('dj.service.name') . '</info>');
        $this->out('Hostname: <info>' . $hostname . '</info>');
        $this->hr();
    }

    /**
     * @return void
     */
    public function main()
    {
        if (file_exists(TMP . '/lockWatchdog')) {
            $this->out('Lock file exists, quiting', 1, Shell::VERBOSE);
            $this->_stop();
        }

        $this->out('Starting Watchdog');

        if ($this->param('workers') > 0) {
            $this->startWorkers();
        } else {
            $this->stopWorkers();
        }

        $this->out('<success>!! All done !!</success>');
    }

    protected function _checkHeartbeat(Worker $worker)
    {
        $max_time = Configure::read('dj.max.execution.time');
        $last_beat = $worker->pulse->diffInSeconds();
        return $last_beat <= $max_time;
    }

    public function startWorkers($worker_count = null)
    {
        $worker_count = $worker_count ?: $this->param('workers');
        $max_workers = Configure::read('dj.max.workers');

        if ($worker_count > $max_workers) {
            $worker_count = $max_workers;
            $this->out('<error>Too many workers (max_workers:' . $max_workers . ')</error>');
        }

        $hostname = php_uname('n');
        $workers = $this->Workers->find('forHost', ['host' => $hostname]);

        $this->out(sprintf(' - Require <info>%d</info> Workers to be running. <info>%d</info> currently running.', $worker_count, $workers->count()));

        $this->out(' - Checking status of running workers.');

        foreach ($workers as $worker) {
            $this->_checkWorkerInstance($worker);
        }
{'fraction_non_alphanumeric': 0.155517078545569, 'fraction_numerical': 0.0028333070990083425, 'mean_word_length': 2.7179637214745465, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3683541', 'n_tokens_mistral': 2077, 'n_tokens_neox': 1981, 'n_words': 409}