Laravel应用关闭时会触发什么事件? [英] What event is fired when Laravel app is being shutdown?

查看:103
本文介绍了Laravel应用关闭时会触发什么事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具体来说,我正在执行的操作是在AppServiceProvider-> boot()方法中,如下所示:

Specifically what I am doing is in my AppServiceProvider->boot() method I am creating a singleton class like below:

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->app->singleton('App\Support\PushNotificationHelper', function ($app) {
            return new PushNotificationHelper();
        });  
     }
 }

队列工作者工作I需要辅助类用于将通知推送到移动应用。当移动设备是Apple设备时,我需要建立一个curl连接,并使该连接在队列工作器作业的寿命之外仍然存在。这就是为什么我使用单例来保持连接,例如:

The helper class is needed for a Queue worker job I use for Pushing Notifications to mobile apps. When the mobile device is an Apple device I need to establish a curl connection and have the connection persist beyond the life of the queue worker job. This is why I am using the singleton to hold the connection like:

class PushNotificationHelper {
    protected $http2Connection;
    protected $http2Expire ;

    public function getConnection($options) {
        $this->http2Connection = curl_init();
        curl_setopt_array($this->http2Connection, $options);
        return $this->http2Connection;
    }

Apple声称如果我反复连接和断开连接,他们将发出拒绝服务通知(DOS)。我的应用程序每小时发送1000条通知。每当我使用连接时,我都会检查错误并在需要时关闭/重新打开连接,例如:

Apple claims if I connect and disconnect repeatedly then they will issue a Denial of Service (DOS). My app literally sends 1000s of notifications per hour. When ever I use the connection I check for errors and will close/reopen the connection when needed like:

 curl_close($http2Connection);

但是我想知道如何检测应用程序何时永久关闭,以便我能够正常运行关闭连接。如果无法执行此操作,随着时间的流逝,会断开开放的连接,从而对我的服务器造成伤害,可以说如果该应用每天要启动/停止几次,则要运行数月?

However I would like to know how I can detect when the app will close for good so I can gracefully close the connection. If there is no way to do this will it harm my server over time by leaving open connections hanging, lets say months of running if the app was to start/stop several times a day ?

另一个选项可能是curl选项,以告知连接在这么长的时间后自动断开连接。 (我强制关闭并每隔4个小时重新打开一次),因此,如果我至少可以告诉连接在5个小时后自动关闭,那可能是自清洁吗?

Another option could be is there a curl option to tell the connection to auto disconnect after so much time. (I force a close and reopen every 4 hours) so if I could tell connection to auto-close after 5 hours at least then maybe it would be self-cleaning?

推荐答案

恕我直言,您可以尝试在应用实例中添加终止回调,例如在 AppServiceProvider 中,即:

IMHO you can try to add a terminating callback to your app instance, for example in the AppServiceProvider, i.e.:

public function boot()
{
    $this->app->terminating(function () {
       // your terminating code here
    });
}

这篇关于Laravel应用关闭时会触发什么事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆