Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scan can't find new cron #44

Open
Filoz opened this issue Jul 29, 2021 · 5 comments
Open

Scan can't find new cron #44

Filoz opened this issue Jul 29, 2021 · 5 comments

Comments

@Filoz
Copy link

Filoz commented Jul 29, 2021

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

@frostieDE
Copy link
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.

@alessandro-podo
Copy link

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?

@nicklog
Copy link
Contributor

nicklog commented Jan 24, 2022

I'll take a look at it ;)

@alessandro-podo
Copy link

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;
}

}
`

@c33s
Copy link

c33s commented Jun 27, 2022

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants