调度程序没有发送任何消息。

huangapple go评论116阅读模式
英文:

Scheduler not sending any messages

问题

symfony/scheduler是自6.3版本以来的新组件,尚未有文档,所以我希望有人知道如何让这个组件工作。我所读的只有他们的博客文章,可以在这里找到

Javier描述了这个组件的实现几乎与symfony/messenger组件相同,唯一的区别是计划消息需要有一个定义计划的ScheduleProvider和它自己的队列名称。

我已经添加了我的计划如下:

#[AsSchedule("default")]
class DefaultScheduleProvider implements ScheduleProviderInterface
{
    public function getSchedule(): Schedule
    {
        $schedule = new Schedule();

        $schedule->add(RecurringMessage::every("5 seconds", new TestScheduledMessage()));

        return $schedule;
    }
}

然后我在messenger配置中定义了我的消息队列:

framework:
    messenger:
        failure_transport: failed
        transports:
            scheduler_default: '%env(MESSENGER_TRANSPORT_DSN)%'
        routing:
            App\Scheduler\TestScheduledMessage: scheduler_default

然后我运行symfony console debug:scheduler来验证计划是否被检测到:

default
-------
 ------------------------- ------------------------------------ ---------------------------
  Message                   Trigger                              Next Run
 ------------------------- ------------------------------------ ---------------------------
  TestScheduledMessage      PeriodicalTrigger: every 5 seconds   2023-08-10T15:28:30+00:00
 ------------------------- ------------------------------------ ---------------------------

Messenger也识别到了我的传输:

 ------------------- -------
  Transport           Count
 ------------------- -------
  scheduler_default   0
 ------------------- -------

然后,我通过symfony console messenger:consume -vv scheduler_default设置了我的工作进程,但我从未收到任何消息。它只是永远坐在那里什么都不做。

是否还有其他需要做的事情以使一切正常运行?

编辑

另外,如果可能有用的信息,这是我的处理程序:

#[AsMessageHandler]
final class TestMessageHandler
{
    public function __invoke(TestScheduledMessage $message)
    {
        // 处理您的消息
    }
}
英文:

symfony/scheduler is a new component since 6.3, it's not yet documented so I'm hoping someone knows how to get this component to work. All I've read is their blog post which can be found here.

Javier describes the component to be almost equal in it's implementation to the symfony/messenger component, and that the only difference is that a scheduled message needs to have a ScheduleProvider that defines the schedule, and it's own queue name.

I've added my schedule as such:

#[AsSchedule("default")]
class DefaultScheduleProvider implements ScheduleProviderInterface
{
    public function getSchedule(): Schedule
    {
        $schedule = new Schedule();

        $schedule->add(RecurringMessage::every("5 seconds", new TestScheduledMessage()));

        return $schedule;
    }
}

Then I defined my message queue in the messenger configuration:

framework:
    messenger:
        failure_transport: failed
        transports:
            scheduler_default: '%env(MESSENGER_TRANSPORT_DSN)%'
        routing:
            App\Scheduler\TestScheduledMessage: scheduler_default

Then I run symfony console debug:scheduler to verify that the schedule is detected:

default
-------
 ------------------------- ------------------------------------ --------------------------- 
  Message                   Trigger                              Next Run                   
 ------------------------- ------------------------------------ --------------------------- 
  TestScheduledMessage      PeriodicalTrigger: every 5 seconds   2023-08-10T15:28:30+00:00  
 ------------------------- ------------------------------------ ---------------------------

And the messenger recognizes my transport:

 ------------------- ------- 
  Transport           Count  
 ------------------- ------- 
  scheduler_default   0      
 ------------------- ------- 

I then setup my worker via symfony console messenger:consume -vv scheduler_default, but I never receive any messages. It just sits there forever and does nothing.

Is there something else I need to do to get things up and running?

Edit

Also, if it may be useful information, this is my handler:

#[AsMessageHandler]
final class TestMessageHandler
{
    public function __invoke(TestScheduledMessage $message)
    {
        // do something with your message
    }
}

答案1

得分: 0

所以,它与信使组件并非完全相同,因为传输不必在messenger.yaml配置中定义。 SchedulerProvider会自动设置队列,如果队列已存在,则不会执行任何操作。

在我的上面的代码中删除scheduler_default传输,然后将消息路由到另一个传输,比如默认的async将使其正常工作。

示例:

framework:
    messenger:
        async: '%env(MESSENGER_TRANSPORT_DSN)%'
        failure_transport: failed
        routing:
            App\Scheduler\TestScheduledMessage: async

由于没有文档,并且博客文章说它的配置方式与信使组件完全相同,我错误地认为你也必须配置传输。

然而,调度程序传输仍然需要其特定的工作器,就像任何常规的信使传输一样。

英文:

So, it's not entirely like the messenger component, as the transport must not be defined in the messenger.yaml configuration. The SchedulerProvider set up the queue automagically, and if the queue already exists, it does nothing.

Removing the scheduler_default transport in my code above, and then routing the messages to another transport, such as the default async will make it work.

Example:

framework:
    messenger:
        async: '%env(MESSENGER_TRANSPORT_DSN)%'
        failure_transport: failed
        routing:
            App\Scheduler\TestScheduledMessage: async

As there was no documentation, and the blog post said it's configured just the same as the messenger component, I was falsely assuming you had to configure the transport too.

The scheduler transport still needs it's specific worker though, just like any regular messenger transport.

huangapple
  • 本文由 发表于 2023年8月10日 23:33:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定