Skip to content

Scan can't find new cron #44

Open
Open

Description

@Filoz

Hi,
I had a problem in Symfony 5.3 with the shapecode:cron:scan command because it couldn't find any CronJob.

I got deeper and I found that the problem was here: https://github.com/shapecode/cron-bundle/blob/master/src/EventListener/AnnotationJobLoaderListener.php#L40

Because of that https://symfony.com/blog/new-in-symfony-5-3-lazy-command-description

In my command I had a description like
class MyCommand extends Command { protected static $defaultDescription = '...';

and that was the problem!

With the description, the $this->application->all() return Symfony\Component\Console\Command\LazyCommand , instead of MyCommand class.
Than the LazyCommand class is used as argument for the reflection in the next line
$reflClass = new ReflectionClass($command);

Removing the description from MyCommand worked fine.

I hope that this little explanation could help someone when updating symfony

best regards

Activity

  1. frostieDE commented on Aug 7, 2021

    @frostieDE
    Contributor

    As a workaround, you may specify the command schedule inside your configuration using a dedicated tag as such:

    App\Command\MyFancyCommand:
      tags:
        - { name: shapecode_cron.cron_job, expression: '@daily' }  

    (see https://github.com/shapecode/cron-bundle/blob/master/src/DependencyInjection/Compiler/CronJobCompilerPass.php)

    Unfortunately I am currently unable to verify whether this command is still considered lazy.

  2. alessandro-podo commented on Jan 23, 2022

    @alessandro-podo

    i want to use this Bundle with SF6

    an possible Solution for that is, to use the new TaggedIterator(SF5.3) to get all Commands.
    What do you say?

  3. nicklog commented on Jan 24, 2022

    @nicklog
    Contributor

    I'll take a look at it ;)

  4. alessandro-podo commented on Jan 24, 2022

    @alessandro-podo

    as a POC:

    `class GetTaggedService
    {
    public function __construct(#[TaggedIterator('console.command')] private iterable $commands, private Reader $reader)
    {
    }

    public function getCronJob(): array
    {
        $job = [];
        foreach ($this->commands as $command) {
            $reflectionClass = new ReflectionClass($command);
    
            foreach ($this->reader->getClassAnnotations($reflectionClass) as $annotation) {
                if (!($annotation instanceof CronJob)) {
                    continue;
                }
                $job[] = [
                    'command' => $command,
                    'annotation' => $annotation,
                ];
            }
            $job[] = $command;
        }
    
        return $job;
    }
    

    }
    `

  5. c33s commented on Jun 27, 2022

    @c33s

    my workaround:

    Shapecode\Bundle\CronBundle\EventListener\AnnotationJobLoaderListener:

    ...
        public function onLoadJobs(LoadJobsEvent $event): void
        {
            foreach ($this->application->all() as $command) {
                if ($command instanceof LazyCommand) {
                    $command = $command->getCommand();
                }
                // Check for an @CronJob annotation
                $reflectionClass = new ReflectionClass($command);
    
                foreach ($this->reader->getClassAnnotations($reflectionClass) as $annotation) {
                    if (! ($annotation instanceof CronJob)) {
                        continue;
                    }
    
                    $arguments    = $annotation->arguments;
                    $maxInstances = $annotation->maxInstances;
                    $schedule     = $annotation->value;
                    assert(is_string($schedule));
    
                    $meta = CronJobMetadata::createByCommand($schedule, $command, $arguments, $maxInstances);
                    $event->addJob($meta);
                }
            }
        }
    ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions