digitalclaim / azure-scheduler-laravel
Laravel scheduler triggered by Azure Scheduled Functions.
Fund package maintenance!
digitalclaim
Requires
- php: ^8.1
- illuminate/contracts: ^10.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- larastan/larastan: ^2.0.1
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.8
- orchestra/testbench: ^8.8
- pestphp/pest: ^2.20
- pestphp/pest-plugin-arch: ^2.5
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
This package is auto-updated.
Last update: 2024-12-08 15:24:30 UTC
README
Laravel scheduler triggered by Azure Scheduled Functions. Instead of a CronJob the scheduled cloud function will request a callback url which triggers the Laravel scheduler.
This package is inspired by stackkit/laravel-google-cloud-scheduler
Warning: The callback route is rate limited to only one request per limit. Laravel sub-minute tasks are not supported.
Installation
You can install the package via composer:
composer require digitalclaim/azure-scheduler-laravel
You can publish the config file with:
php artisan vendor:publish --tag="azure-scheduler-laravel-config"
This is the contents of the published config file:
return [
];
Usage
- [Optional] Whitelist route for maintenance mode. This step is optional, but highly recommended. To allow jobs to keep running if the application is down (
php artisan down
) you must modify thePreventRequestsDuringMaintenance
middleware:
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance as Middleware;
class PreventRequestsDuringMaintenance extends Middleware
{
/**
* The URIs that should be reachable while maintenance mode is enabled.
*
* @var array
*/
protected $except = [
+ '/handle-scheduler',
];
}
- [Optional] Set application RUNNING_IN_CONSOLE (highly recommended). Some Laravel service providers only register their commands if the application is being accessed through the command line (Artisan). Because we are calling Laravel scheduler from a HTTP call, that means some commands may never register, such as the Laravel Scout command. To circumvent this, please add the following to
public/index.php
:
/* |-------------------------------------------------------------------------- | Check If Application Is Under Maintenance |-------------------------------------------------------------------------- | | If the application is maintenance / demo mode via the "down" command we | will require this file so that any prerendered template can be shown | instead of starting the framework, which could cause an exception. | */ if (file_exists(__DIR__.'/../storage/framework/maintenance.php')) { require __DIR__.'/../storage/framework/maintenance.php'; } + + /* + |-------------------------------------------------------------------------- + | Manually Set Running In Console for Google Cloud Scheduler + |-------------------------------------------------------------------------- + | + | Some service providers only register their commands if the application + | is running from the console. Since we are calling Cloud Scheduler + | from the browser we must manually trick the application into + | thinking that it is being run from the command line. + | + */ + + if (($_SERVER['REQUEST_URI'] ?? '') === '/cloud-scheduler-job') { + $_ENV['APP_RUNNING_IN_CONSOLE'] = true; + } + /* |-------------------------------------------------------------------------- | Register The Auto Loader |-------------------------------------------------------------------------- | | Composer provides a convenient, automatically generated class loader for | this application. We just need to utilize it! We'll simply require it | into the script here so we don't need to manually load our classes. | */ require __DIR__.'/../vendor/autoload.php';
Copy the code here:
/* |-------------------------------------------------------------------------- | Manually Set Running In Console for Azure Scheduler |-------------------------------------------------------------------------- | | Some service providers only register their commands if the application | is running from the console. Since we are calling Azure Scheduler | from the browser we must manually trick the application into | thinking that it is being run from the command line. | */ if (($_SERVER['REQUEST_URI'] ?? '') === '/handle-scheduler') { $_ENV['APP_RUNNING_IN_CONSOLE'] = true; }
- Create a new scheduled Azure Function (nodejs) that triggeres every minute
0 * * * * *
:
const axios = require("axios"); module.exports = async function (context, myTimer) { try { const response = await axios.get( "https://YOURSITE.azurewebsites.net/handle-scheduler" ); context.log(response.data); } catch (error) { // If the promise rejects, an error will be thrown and caught here context.log(error); } context.done(); };
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.