AppServiceProvider.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Providers;
  3. use Illuminate\Support\ServiceProvider;
  4. use Illuminate\Support\Facades\URL;
  5. use Illuminate\Support\Facades\Redis;
  6. use App\Models\MarkdownImport;
  7. use App\Models\PreQuestionCandidate;
  8. class AppServiceProvider extends ServiceProvider
  9. {
  10. /**
  11. * Register any application services.
  12. */
  13. public function register(): void
  14. {
  15. // 注册 MathRecSys 服务
  16. $this->app->singleton(\App\Services\MathRecSysService::class, function () {
  17. return new \App\Services\MathRecSysService();
  18. });
  19. // 注册知识图谱服务
  20. $this->app->singleton(\App\Services\KnowledgeGraphService::class, function ($app) {
  21. return new \App\Services\KnowledgeGraphService(
  22. $app->make(\App\Services\MathRecSysService::class)
  23. );
  24. });
  25. }
  26. /**
  27. * Bootstrap any application services.
  28. */
  29. public function boot(): void
  30. {
  31. if (config('app.env') === 'production') {
  32. URL::forceScheme('https');
  33. }
  34. MarkdownImport::updated(function (MarkdownImport $import): void {
  35. try {
  36. Redis::publish('markdown-imports', json_encode([
  37. 'type' => 'markdown-imports',
  38. 'id' => $import->id,
  39. 'status' => $import->status,
  40. 'progress_stage' => $import->progress_stage,
  41. 'progress_message' => $import->progress_message,
  42. 'parsed_count' => $import->parsed_count ?? null,
  43. 'accepted_count' => $import->accepted_count ?? null,
  44. 'import_id' => $import->id,
  45. ], JSON_UNESCAPED_UNICODE));
  46. } catch (\Throwable $e) {
  47. // SSE publish failed, ignore to avoid affecting business flow.
  48. }
  49. });
  50. PreQuestionCandidate::updated(function (PreQuestionCandidate $candidate): void {
  51. try {
  52. Redis::publish('pre-question-candidates', json_encode([
  53. 'type' => 'pre-question-candidates',
  54. 'id' => $candidate->id,
  55. 'import_id' => $candidate->import_id,
  56. 'status' => $candidate->status,
  57. 'is_question_candidate' => $candidate->is_question_candidate,
  58. ], JSON_UNESCAPED_UNICODE));
  59. } catch (\Throwable $e) {
  60. // SSE publish failed, ignore to avoid affecting business flow.
  61. }
  62. });
  63. }
  64. }