| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- namespace App\Module\Mex\Providers;
- use Illuminate\Support\ServiceProvider;
- use Illuminate\Console\Scheduling\Schedule;
- use Illuminate\Support\Facades\Event;
- use Illuminate\Support\Facades\Schedule as ScheduleFacade;
- /**
- * Mex模块服务提供者
- *
- * 负责注册Mex模块的服务、命令、计划任务等
- */
- class MexServiceProvider extends ServiceProvider
- {
- /**
- * 注册服务
- */
- public function register(): void
- {
- // 注册配置文件
- $this->mergeConfigFrom(
- __DIR__ . '/../Config/mex.php',
- 'mex'
- );
- // 注册单例服务 ;不注册服务
- // $this->registerServices();
- }
- /**
- * 启动服务
- */
- public function boot(): void
- {
- // 注册命令
- $this->registerCommands();
- // 注册Mex定时任务
- $this->registerSchedules();
- // 注册事件监听器
- $this->registerEventListeners();
- // 发布配置文件
- $this->publishes([
- __DIR__ . '/../Config/mex.php' => config_path('mex.php'),
- ], 'mex-config');
- }
- /**
- * 注册命令
- */
- protected function registerCommands(): void
- {
- if ($this->app->runningInConsole()) {
- $this->commands([
- \App\Module\Mex\Commands\MexUserBuyItemMatchCommand::class,
- \App\Module\Mex\Commands\MexUserSellItemMatchCommand::class,
- \App\Module\Mex\Commands\MexTestCommand::class,
- \App\Module\Mex\Commands\TestMultiCurrencyCommand::class,
- \App\Module\Mex\Commands\TestOpenHandlerCommand::class,
- \App\Module\Mex\Commands\FixMissingTransactionRecordsCommand::class,
- \App\Module\Mex\Commands\GenerateDailyPriceTrendsCommand::class,
- \App\Module\Mex\Commands\TestConfigCommand::class,
- ]);
- }
- }
- /**
- * 注册事件监听器
- */
- protected function registerEventListeners(): void
- {
- Event::listen(
- \App\Module\Mex\Events\OrderCreatedEvent::class,
- \App\Module\Mex\Listeners\OrderCreatedListener::class
- );
- Event::listen(
- \App\Module\Mex\Events\OrderCompletedEvent::class,
- [\App\Module\Mex\Listeners\OrderCreatedListener::class, 'handleCompleted']
- );
- Event::listen(
- \App\Module\Mex\Events\TransactionCreatedEvent::class,
- \App\Module\Mex\Listeners\TransactionCreatedListener::class
- );
- }
- /**
- * 注册Mex相关的定时任务
- *
- * 将原本在 routes/console.php 中的Mex调度配置迁移到此处
- */
- protected function registerSchedules(): void
- {
- // 在应用完全启动后注册定时任务
- $this->app->booted(function () {
- // 每天凌晨00:05生成每日价格趋势数据
- ScheduleFacade::command('mex:generate-daily-trends')
- ->dailyAt('00:05')
- ->description('生成交易所每日价格趋势数据');
- // 注释掉的匹配任务(可根据需要启用)
- // php artisan mex:user-sell-item-match
- // ScheduleFacade::command('mex:user-sell-item-match')
- // ->everyMinute()
- // ->onOneServer()
- // ->description('用户卖出物品匹配');
- // php artisan mex:user-sell-item-match
- // ScheduleFacade::command('mex:user-buy-item-match')
- // ->everyFiveMinutes()
- // ->onOneServer()
- // ->description('用户买入物品匹配');
- });
- }
- }
|