Laravel队列任务广播事件无法触发Filament Livewire监听器
Laravel/Filament队列广播事件无法触发RelationManager监听器问题
问题描述
正在开发Laravel/Filament应用,需求是通过队列任务广播事件,更新Filament RelationManager的状态。当前有一个为患者生成分析报告的队列任务,分析完成时会广播事件,预期触发Filament RelationManager组件刷新。广播功能本身正常(任务完成可收到“Analysis Complete”通知),但RelationManager中的Livewire监听器始终未触发。
正常工作项
- 队列任务执行成功
- 事件已广播(日志已确认)
- 同一广播系统可接收Filament通知
异常情况
- RelationManager的Livewire监听器从未触发
- 任务完成后按钮仍处于禁用状态
- 表格未刷新显示新数据
日志信息
[2024-12-04 15:07:27] local.INFO: Dispatching job with: {"livewire_record_type":"App\\Models\\Patient","livewire_record_data":{"id":1...}} [2024-12-04 15:07:27] local.INFO: Job constructor called with: {"patient_type":"App\\Models\\Patient","patient_data":{"id":1...}} [2024-12-04 15:07:28] local.INFO: Broadcasting analysis event {"channel":"analyses","event":"analysis-completed"} [2024-12-04 15:07:29] local.INFO: Broadcasting analysis completed event
代码结构
Job(app/Jobs/GeneratePatientAnalysis.php)
class GeneratePatientAnalysis implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; protected int $userId; public function __construct(protected Patient $patient) { \Log::info('Job constructor called with:', [ 'patient_type' => get_class($patient), 'patient_data' => $patient->toArray() ]); $this->userId = auth()->id(); } public function handle(AiAnalysisService $aiService): void { $latestVersion = $this->patient->analyses() ->latest() ->first()?->version ?? '0.0'; $newVersion = number_format((float) $latestVersion + 0.1, 1); $analysis = Analysis::create([ 'patient_id' => $this->patient->id, 'generated_by' => $this->userId, 'status' => 'pending', 'version' => $newVersion, 'model_used' => 'gpt-4o', ]); try { $patientData = $this->gatherPatientData(); $analysis->update([ 'analysis_content' => 'Generated Analysis Here', 'status' => 'completed', 'completed_at' => now(), ]); $user = User::find($this->userId); Notification::make() ->title('Analysis Complete') ->success() ->broadcast([$user]) ->send(); \Log::info('Broadcasting analysis event', [ 'channel' => 'analyses', 'event' => 'analysis-completed' ]); broadcast(new AnalysisCompleted($analysis))->toOthers(); } catch (\Exception $e) { $analysis->update(['status' => 'failed']); Notification::make() ->title('Analysis Failed') ->danger() ->send(); } } }
Event(app/Events/AnalysisCompleted.php)
class AnalysisCompleted implements ShouldBroadcast { public Analysis $analysis; public function broadcastOn(): array { return ['analyses']; } public function broadcastAs(): string { return 'analysis-completed'; } }
RelationManager(app/Filament/Resources/PatientResource/RelationManagers/AnalysesRelationManager.php)
protected $listeners = [ 'echo:analyses,analysis-completed' => 'refreshAnalyses' ]; public function refreshAnalyses(): void { $this->isGenerating = false; $this->render(); } public function table(Table $table): Table { return $table ->columns([]) ->headerActions([ \Filament\Tables\Actions\Action::make('generate') ->label('Generate New Analysis') ->action(function ($livewire) { \Log::info('Dispatching job with:', [ 'livewire_record_type' => get_class($livewire->ownerRecord), 'livewire_record_data' => $livewire->ownerRecord->toArray() ]); $this->isGenerating = true; GeneratePatientAnalysis::dispatch($livewire->ownerRecord) ->onQueue('default'); $this->render(); Notification::make() ->title('Analysis Generation Started') ->body('You will be notified when the analysis is ready.') ->success() ->send(); }) ->icon('heroicon-o-plus') ->disabled(fn() => $this->isGenerating) ]); }
内容的提问来源于stack exchange,提问作者Halnex
相关产品推荐
相关产品推荐

