简体   繁体   English

Laravel 队列工作器中的持久连接

[英]Persistent connection in laravel queue worker

I am trying to setup a service on my laravel application with third party library for connecting to provider.我正在尝试使用第三方库在我的 laravel 应用程序上设置服务以连接到提供者。

Its code goes as follows它的代码如下

$connection = new CustomConnection();
$connection->refresh();
$connection->sendMessage('user@myapp.com', ['message'=>'something', 'ttl'=>3600]);
$connection->refresh();
$connection->sendMessage('user2@myapp.com', ['message'=>'something', 'ttl'=>3600]);
$connection->close();

My goal is to keep the connection connected while sending message via laravel queue worker.我的目标是在通过 laravel 队列工作器发送消息时保持连接连接。

Something like if que worker establishes就像如果que worker建立一样

$connection = new CustomConnection();
$connection->refresh();

Executes $connection->refresh() every 5 seconds & whenever job is added in queue it should execute每 5 秒执行一次$connection->refresh()每当在队列中添加作业时它都应该执行

$connection->sendMessage('user@myapp.com', ['message'=>'something', 'ttl'=>3600]);
$connection->refresh();

Block of code.代码块。

I have no clue how laravel's core queue works in backend and if I can override it's functionality and how.我不知道 laravel 的核心队列是如何在后端工作的,以及我是否可以覆盖它的功能以及如何覆盖。

Thanks.谢谢。

In your service provider, register the connection (or a service that uses the connection) as a singleton . 在您的服务提供商中,将连接(或使用该连接的服务)注册为单例 Declare this as a dependency for your job, and all your jobs will have the same connection/service instance for the lifetime of the queue worker. 将此声明为您的作业的依赖项,并且在队列工作者的整个生命周期中,所有作业都将具有相同的连接/服务实例。

There's no way you can execute $connection->refresh() every fifth second. 您无法每五秒钟执行一次$ connection-> refresh()。 If the purpose of this call is some kind of heartbeat/healthcheck, listen for the queue-related events and use these instead. 如果此调用的目的是某种心跳/健康检查,请侦听与队列相关的事件,并改用这些事件 A combination of JobProcessing, JobProcessed, JobFailed and Looping will allow you to execute code before and after jobs execute. JobProcessing,JobProcessed,JobFailed和Looping的组合将使您能够在作业执行之前和之后执行代码。 You can use these to evaluate if you should call $connection->refresh(), like if at least five seconds has passed since last invocation. 您可以使用它们来评估是否应调用$ connection-> refresh(),例如自上次调用以来已过去至少五秒钟。

There's no event you can use to run code when a job is dispatched. 调度作业时,没有事件可用于运行代码。

Do not attempt to override the internal workings of the queue system. 不要尝试覆盖队列系统的内部工作原理。 There's no promises of backward compatibility between different Laravel releases, and you'll have to keep track of all (possible) subtle changes that are introduced upstream. 在不同的Laravel版本之间没有向后兼容性的保证,并且您必须跟踪上游引入的所有(可能的)细微更改。

Allows persisting database sessions between queue jobs * This pull request allows persisting database sessions between queue jobs.允许在队列作业之间持久化数据库会话* 此拉取请求允许在队列作业之间持久化数据库会话。 To opt-in to this behavior, users simply need set the VAPOR_QUEUE_DATABASE_SESSION_PERSIST environment variable to true.要选择加入此行为,用户只需将 VAPOR_QUEUE_DATABASE_SESSION_PERSIST 环境变量设置为 true。 Allowing to make a very simple job that uses the database at least once, up to 45% faster in a 512MB lambda functions.允许进行一个非常简单的工作,至少使用一次数据库,在 512MB 的 lambda 函数中速度提高 45%。 https://github.com/laravel/vapor-core/pull/97 https://github.com/laravel/vapor-core/pull/97

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM