1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

fix: prometheus metrics types

This commit is contained in:
Ferdinand Mütsch
2022-11-20 10:59:06 +01:00
parent 99e50b1062
commit 61f13fce20
5 changed files with 164 additions and 48 deletions

View File

@@ -8,7 +8,8 @@ import (
"runtime"
)
var jobqueues map[string]*artifex.Dispatcher
var jobQueues map[string]*artifex.Dispatcher
var jobCounts map[string]int
const (
QueueDefault = "wakapi.default"
@@ -16,8 +17,14 @@ const (
QueueReports = "wakapi.reports"
)
type JobQueueMetrics struct {
Queue string
EnqueuedJobs int
FinishedJobs int
}
func init() {
jobqueues = make(map[string]*artifex.Dispatcher)
jobQueues = make(map[string]*artifex.Dispatcher)
InitQueue(QueueDefault, 1)
InitQueue(QueueProcessing, int(math.Ceil(float64(runtime.NumCPU())/2.0)))
@@ -25,28 +32,40 @@ func init() {
}
func InitQueue(name string, workers int) error {
if _, ok := jobqueues[name]; ok {
if _, ok := jobQueues[name]; ok {
return fmt.Errorf("queue '%s' already existing", name)
}
logbuch.Info("creating job queue '%s' (%d workers)", name, workers)
jobqueues[name] = artifex.NewDispatcher(workers, 4096)
jobqueues[name].Start()
jobQueues[name] = artifex.NewDispatcher(workers, 4096)
jobQueues[name].Start()
return nil
}
func GetDefaultQueue() *artifex.Dispatcher {
return GetQueue("")
return GetQueue(QueueDefault)
}
func GetQueue(name string) *artifex.Dispatcher {
if _, ok := jobqueues[name]; !ok {
if _, ok := jobQueues[name]; !ok {
InitQueue(name, 1)
}
return jobqueues[name]
return jobQueues[name]
}
func GetQueueMetrics() []*JobQueueMetrics {
metrics := make([]*JobQueueMetrics, 0, len(jobQueues))
for name, queue := range jobQueues {
metrics = append(metrics, &JobQueueMetrics{
Queue: name,
EnqueuedJobs: queue.CountEnqueued(),
FinishedJobs: queue.CountDispatched(),
})
}
return metrics
}
func CloseQueues() {
for _, q := range jobqueues {
for _, q := range jobQueues {
q.Stop()
}
}