PHP code example of abhishekdas / shutdown-scheduler

1. Go to this page and download the library: Download abhishekdas/shutdown-scheduler library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

abhishekdas / shutdown-scheduler example snippets





// If installed via Composer
e_once 'ShutdownScheduler.php';

// Import the namespace
use AbhishekDas\ShutdownScheduler\ShutdownScheduler;

// Create a new instance
$shutdownScheduler = new ShutdownScheduler();

// Register a shutdown event with a callback function
$shutdownScheduler->registerShutdownEvent('cleanup', function() {
    echo "Cleaning up resources...\n";
    // Close database connections, file handlers, etc.
});

// Your application code here...
echo "Application running...\n";

// If needed, you can unregister an event
// $shutdownScheduler->unregisterShutdownEvent('cleanup');

// When the script ends, the 'cleanup' function will be executed automatically


AbhishekDas\ShutdownScheduler\ShutdownScheduler;

class Cron
{
    public function run()
    {
        // Check if the cron job is already running
        if ($this->isCronRunning()) {
            return false; // Avoid duplicate execution
        }

        // Set the cron job as running
        $this->setCronRunning();

        // Create a new ShutdownScheduler instance
        $shutdownScheduler = new ShutdownScheduler();

        // Register a shutdown event to clear the running status
        $eventName = 'clearRunning-' . time();
        $shutdownScheduler->registerShutdownEvent($eventName, [$this, 'clearCronRunning']);

        try {
            // Run your cron job logic here
            // ...
            
            // Set the cron job as successful
            $this->setCronSuccess();
        } finally {
            // Unregister the shutdown event if everything completes normally
            $shutdownScheduler->unregisterShutdownEvent($eventName);
        }
        
        return true;
    }

    // Implementation methods would go here
    // ...
}

public function __construct()