<commit_before><?php

namespace N98\Magento\Command\System\Cron;

use Exception;
use RuntimeException;
use Symfony\Component\Console\Helper\DialogHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Validator\Exception\InvalidArgumentException;

class RunCommand extends AbstractCronCommand
{
    const REGEX_RUN_MODEL = '#^([a-z0-9_]+/[a-z0-9_]+)::([a-z0-9_]+)$#i';
    /**
     * @var array
     */
    protected $infos;

    protected function configure()
    {
        $this
            ->setName('sys:cron:run')
            ->addArgument('job', InputArgument::OPTIONAL, 'Job code')
            ->setDescription('Runs a cronjob by job code');
        $help = <<<HELP
If no `job` argument is passed you can select a job from a list.
See it in action: http://www.youtube.com/watch?v=QkzkLgrfNaM
HELP;
        $this->setHelp($help);
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     *
     * @return int|void
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->detectMagento($output, true);
        if (!$this->initMagento()) {
            return;
        }

        $jobCode = $input->getArgument('job');
        if (!$jobCode) {
            $this->writeSection($output, 'Cronjob');
            $jobCode = $this->askJobCode($output, $this->getJobs());
        }

        $jobsRoot = \Mage::getConfig()->getNode('crontab/jobs');
        $defaultJobsRoot = \Mage::getConfig()->getNode('default/crontab/jobs');

        $jobConfig = $jobsRoot->{$jobCode};
        if (!$jobConfig || !$jobConfig->run) {
            $jobConfig = $defaultJobsRoot->{$jobCode};
            if (!$jobConfig || !$jobConfig->run) {
                throw new RuntimeException('No job config found!');
            }
        }

        $runConfig = $jobConfig->run;

        if ($runConfig->model) {
            if (!preg_match(self::REGEX_RUN_MODEL, (string) $runConfig->model, $run)) {
                throw new RuntimeException('Invalid model/method definition, expecting "model/class::method".');
            }
            $model = \Mage::getModel($run[1]);
            $callback = array($model, $run[2]);
            $callableName = vsprintf("%1\$s::%2\$s", $run);
            if (!$model || !is_callable($callback, false, $callableName)) {
                throw new RuntimeException(sprintf('Invalid callback: %s', $callableName));
            }

            $output->write('<info>Run </info><comment>' . $callableName . '</comment> ');

            \Mage::getConfig()->init()->loadEventObservers('crontab');
            \Mage::app()->addEventArea('crontab');

            try {
                $schedule = \Mage::getModel('cron/schedule');
                $schedule
                    ->setJobCode($jobCode)
                    ->setStatus(\Mage_Cron_Model_Schedule::STATUS_RUNNING)
                    ->setExecutedAt(strftime('%Y-%m-%d %H:%M:%S', time()))
                    ->save();

                call_user_func_array($callback, array($schedule));
<commit_msg>Set created_at in schedule when running a cron job<commit_after><?php

namespace N98\Magento\Command\System\Cron;

use Exception;
use RuntimeException;
use Symfony\Component\Console\Helper\DialogHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Validator\Exception\InvalidArgumentException;

class RunCommand extends AbstractCronCommand
{
    const REGEX_RUN_MODEL = '#^([a-z0-9_]+/[a-z0-9_]+)::([a-z0-9_]+)$#i';
    /**
     * @var array
     */
    protected $infos;

    protected function configure()
    {
        $this
            ->setName('sys:cron:run')
            ->addArgument('job', InputArgument::OPTIONAL, 'Job code')
            ->setDescription('Runs a cronjob by job code');
        $help = <<<HELP
If no `job` argument is passed you can select a job from a list.
See it in action: http://www.youtube.com/watch?v=QkzkLgrfNaM
HELP;
        $this->setHelp($help);
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     *
     * @return int|void
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->detectMagento($output, true);
        if (!$this->initMagento()) {
            return;
        }

        $jobCode = $input->getArgument('job');
        if (!$jobCode) {
            $this->writeSection($output, 'Cronjob');
            $jobCode = $this->askJobCode($output, $this->getJobs());
        }

        $jobsRoot = \Mage::getConfig()->getNode('crontab/jobs');
        $defaultJobsRoot = \Mage::getConfig()->getNode('default/crontab/jobs');

        $jobConfig = $jobsRoot->{$jobCode};
        if (!$jobConfig || !$jobConfig->run) {
            $jobConfig = $defaultJobsRoot->{$jobCode};
            if (!$jobConfig || !$jobConfig->run) {
                throw new RuntimeException('No job config found!');
            }
        }

        $runConfig = $jobConfig->run;

        if ($runConfig->model) {
            if (!preg_match(self::REGEX_RUN_MODEL, (string) $runConfig->model, $run)) {
                throw new RuntimeException('Invalid model/method definition, expecting "model/class::method".');
            }
            $model = \Mage::getModel($run[1]);
            $callback = array($model, $run[2]);
            $callableName = vsprintf("%1\$s::%2\$s", $run);
            if (!$model || !is_callable($callback, false, $callableName)) {
                throw new RuntimeException(sprintf('Invalid callback: %s', $callableName));
            }

            $output->write('<info>Run </info><comment>' . $callableName . '</comment> ');

            \Mage::getConfig()->init()->loadEventObservers('crontab');
            \Mage::app()->addEventArea('crontab');

            try {
                $schedule = \Mage::getModel('cron/schedule');
                $schedule
                    ->setJobCode($jobCode)
                    ->setStatus(\Mage_Cron_Model_Schedule::STATUS_RUNNING)
                    ->setCreatedAt(strftime('%Y-%m-%d %H:%M:%S', time()))
                    ->setExecutedAt(strftime('%Y-%m-%d %H:%M:%S', time()))
                    ->save();

                call_user_func_array($callback, array($schedule));

{'fraction_non_alphanumeric': 0.16072255673923114, 'fraction_numerical': 0.0037054191755442334, 'mean_word_length': 2.4112690889942074, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2645735', 'n_tokens_mistral': 1953, 'n_tokens_neox': 1919, 'n_words': 345}