What event is fired when Laravel app is being shutdown?
Asked Answered
L

2

6

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?

Laconia answered 18/7, 2019 at 13:31 Comment(4)
"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:" I don't think this will work the way you think it does. A singleton in PHP exists for the duration of the running script. It doesn't exist in a subsequent execution - it gets reinitialized the next time the worker process is run. PHP closes open connections (cURL, SQL, etc.) when the script ends.Kalakalaazar
So is it as simple as PHP will auto close the curl connection? That would be great. The reason I say persistent is I said this is for a "Queue Worker" job. This is a long lived process. It is started using Supervisor on a linux server when the server starts and runs until either it crashes, or is manually stopped.Laconia
Yes, barring a major failure like a segmentation fault or you doing a kill -9, PHP will clean up after itself.Kalakalaazar
You may want to post that so I can mark it as an answerLaconia
L
16

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
    });
}
Leaseholder answered 18/7, 2019 at 13:49 Comment(0)
R
-1

You can use the boot method for anything. From laravel docs:

This method is called after all other service providers have been registered, meaning you have access to all other services that have been registered by the framework

The key is that boot method run when all services are registered, so, you can inject services in the boot method definition.

public function boot(SomeService $someService, OtherService $otherService)
{
    $someService->doSomething();
    $otherService->doSomething();
}

In my opinion, you must to use this method to run code required by your app in all contexts: user logged in, user logged out, post, get, put, etc., etc.

Redon answered 13/10, 2020 at 12:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.