digitalclaim / azure-scheduler-laravel
由 Azure 计划函数触发的 Laravel 调度器。
v1.0.0-alpha1
2024-02-16 12:17 UTC
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-09-08 14:50:01 UTC
README
由 Azure 计划函数触发的 Laravel 调度器。而不是 CronJob,计划中的云函数将请求一个回调 URL,以触发 Laravel 调度器。
本包受到 stackkit/laravel-google-cloud-scheduler 的启发。
警告:回调路由被限制为每个限制仅一个请求。不支持 Laravel 子分钟任务。
安装
您可以通过 composer 安装此包
composer require digitalclaim/azure-scheduler-laravel
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="azure-scheduler-laravel-config"
这是发布配置文件的内容
return [
];
使用方法
- [可选] 维护模式下的白名单路由。此步骤是可选的,但强烈推荐。如果应用程序关闭(
php artisan down
)允许作业继续运行,您必须修改PreventRequestsDuringMaintenance
中间件
<?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',
];
}
- [可选] 设置应用程序 RUNNING_IN_CONSOLE(强烈推荐)。某些 Laravel 服务提供者只有在通过命令行(Artisan)访问应用程序时才会注册其命令。因为我们是从 HTTP 调用调用 Laravel 调度器,这意味着某些命令可能永远不会注册,例如 Laravel Scout 命令。为了避免这种情况,请在
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';
复制代码到这里
/* |-------------------------------------------------------------------------- | 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; }
- 创建一个新的每分钟触发一次的 Azure Function(nodejs)
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(); };
测试
composer test
变更日志
请参阅 CHANGELOG 了解最近更改的更多信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全漏洞
请参阅 我们的安全策略 了解如何报告安全漏洞。
致谢
许可证
MIT 许可证(MIT)。请参阅 许可证文件 了解更多信息。