findOrFail($this->runId); $automation=$run->automation; $run->update(['status'=>'running','started_at'=>now()]); try { foreach($automation->steps->where('is_enabled',true) as $step) { $run->update(['current_step'=>$step->position]); try { $result=$actions->execute($step,$run->payload??[]); $run->logs()->create(['automation_step_id'=>$step->id,'level'=>'info','message'=>$step->name?:$step->action_type.' completed','context'=>$result,'created_at'=>now()]); } catch(\Throwable $e) { $run->logs()->create(['automation_step_id'=>$step->id,'level'=>'error','message'=>$e->getMessage(),'created_at'=>now()]); if($automation->stop_on_failure) throw $e; } } $run->update(['status'=>'completed','finished_at'=>now()]); } catch(\Throwable $e) { $run->update(['status'=>'failed','error'=>$e->getMessage(),'finished_at'=>now()]); throw $e; } finally { $automation->increment('run_count'); $automation->update(['last_ran_at'=>now()]); } } }