Cleanup context, more logging

This commit is contained in:
Ettore Di Giacinto
2024-04-20 00:19:17 +02:00
parent 34f6d821b9
commit 22160cbf9e
4 changed files with 32 additions and 7 deletions

View File

@@ -241,6 +241,13 @@ func (a *Agent) consumeJob(job *Job, role string) {
}
a.Unlock()
defer func() {
a.Lock()
a.actionContext.Cancel()
a.actionContext = nil
a.Unlock()
}()
if selfEvaluation {
defer func() {
a.Lock()
@@ -711,6 +718,7 @@ func (a *Agent) Run() error {
//todoTimer := time.NewTicker(a.options.periodicRuns)
timer := time.NewTimer(a.options.periodicRuns)
for {
xlog.Debug("Agent is waiting for a job", "agent", a.Character.Name)
select {
case job := <-a.jobQueue:
// Consume the job and generate a response
@@ -719,15 +727,18 @@ func (a *Agent) Run() error {
if !timer.Stop() {
<-timer.C
}
xlog.Debug("Agent is consuming a job", "agent", a.Character.Name, "job", job)
a.consumeJob(job, UserRole)
timer.Reset(a.options.periodicRuns)
case <-a.context.Done():
// Agent has been canceled, return error
xlog.Warn("Agent has been canceled", "agent", a.Character.Name)
return ErrContextCanceled
case <-timer.C:
if !a.options.standaloneJob {
continue
}
xlog.Debug("Agent is running periodically", "agent", a.Character.Name)
a.periodicallyRun()
timer.Reset(a.options.periodicRuns)
}