frostfs-node/pkg/services/audit/taskmanager/listen.go
Dmitrii Stepanov e8d340287f [#222] auditsvc: Refactor audit task
Resolve containedctx linter. Cancel task by listen cancel.

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
2023-04-07 17:29:13 +03:00

84 lines
1.9 KiB
Go

package audittask
import (
"context"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/audit"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/audit/auditor"
"go.uber.org/zap"
)
// Listen starts the process of processing tasks from the queue.
//
// The listener is terminated by context.
func (m *Manager) Listen(ctx context.Context) {
m.log.Info("process routine",
zap.Uint32("queue_capacity", m.queueCap),
)
m.ch = make(chan *audit.Task, m.queueCap)
for {
select {
case <-ctx.Done():
m.log.Warn("stop listener by context",
zap.String("error", ctx.Err().Error()),
)
m.workerPool.Release()
return
case task, ok := <-m.ch:
if !ok {
m.log.Warn("queue channel is closed")
return
}
tCtx, tCancel := context.WithCancel(ctx) // cancel task in case of listen cancel
go func() {
select {
case <-tCtx.Done(): // listen cancelled or task completed
return
case <-task.CancelChannel(): // new epoch
tCancel()
}
}()
m.handleTask(tCtx, task, tCancel)
}
}
}
func (m *Manager) handleTask(ctx context.Context, task *audit.Task, onCompleted func()) {
pdpPool, err := m.pdpPoolGenerator()
if err != nil {
m.log.Error("could not generate PDP worker pool",
zap.String("error", err.Error()),
)
onCompleted()
return
}
porPool, err := m.pdpPoolGenerator()
if err != nil {
m.log.Error("could not generate PoR worker pool",
zap.String("error", err.Error()),
)
onCompleted()
return
}
auditContext := m.generateContext(task).
WithPDPWorkerPool(pdpPool).
WithPoRWorkerPool(porPool)
if err := m.workerPool.Submit(func() { auditContext.Execute(ctx, onCompleted) }); err != nil {
// may be we should report it
m.log.Warn("could not submit audit task")
onCompleted()
}
}
func (m *Manager) generateContext(task *audit.Task) *auditor.Context {
return auditor.NewContext(m.ctxPrm).
WithTask(task)
}