setono/message-scheduler-bundle

在将来安排 Symfony Messenger 消息

dev-master / 1.0.x-dev 2020-07-21 06:59 UTC

This package is auto-updated.

Last update: 2024-09-15 22:03:12 UTC


README

Latest Version Latest Unstable Version Software License Build Status Coverage Status Quality Score

在将来安排 Symfony Messenger 消息。

这个包的需求来源于一个项目,我们需要使用 cron job 以每分钟为基准检查一个事件的有效期。因此,一个事件可能在 10 天后的 10:05:00 结束,但 cron job 不会知道这一点。相反,cron job 每分钟运行一次来检查它。检查是一个非常耗费内存的任务,因此从长远来看这是不可行的。

因此,我们不再每分钟检查一次,而是在我们知道事件结束的时间上安排一个命令在未来运行。

问题解决 🎉

安装

步骤 1:下载

$ composer require setono/message-scheduler-bundle

步骤 2:启用包

如果您使用 Symfony Flex,则它将自动启用。否则,您需要将其添加到 config/bundles.php

<?php
// config/bundles.php

return [
    // ...

    Setono\MessageSchedulerBundle\SetonoMessageSchedulerBundle::class => ['all' => true],

    // ...
];

步骤 3:更新您的数据库模式

此包引入了一个新的实体,ScheduledMessage,因此您需要添加一个迁移

$ php bin/console doctrine:migrations:diff
$ php bin/console doctrine:migrations:migrate

步骤 4:设置用于发送消息的 cron job

此包引入了一个命令,setono:message-scheduler:dispatch,您应该经常运行它。频率取决于您应用程序的需求。每分钟是推荐的。这里有一个 crontab 段落 供您使用

* * * * * php /absolute/path/to/bin/console setono:message-scheduler:dispatch

步骤 5:配置 Symfony Messenger

因为这个包使用自己的命令发送您的消息,所以异步发送那个命令非常重要。这将确保您在上一步中设置的 cron job 将快速运行。

framework:
    messenger:
        routing:
            # Route all command messages to the async transport
            # This presumes that you have already set up an 'async' transport
            'Setono\MessageSchedulerBundle\Message\Command\CommandInterface': async

用法

待办事项