What event is fired when Laravel app is being shutdown?

限于喜欢 提交于 2020-08-20 05:18:45

问题


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

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 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 ?

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?


回答1:


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


来源:https://stackoverflow.com/questions/57095711/what-event-is-fired-when-laravel-app-is-being-shutdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!