2024-02-17 19:08:58 +06:00
|
|
|
package kernel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Queue struct {
|
|
|
|
Setting *ConvertSetting
|
|
|
|
Status StatusContract
|
|
|
|
Error error
|
|
|
|
}
|
|
|
|
|
|
|
|
type StatusContract interface {
|
2024-03-05 00:58:43 +05:00
|
|
|
Name() string
|
|
|
|
Ordinal() int
|
2024-02-17 19:08:58 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
Waiting = iota
|
|
|
|
InProgress
|
|
|
|
Completed
|
|
|
|
Error
|
|
|
|
)
|
|
|
|
|
|
|
|
type StatusType uint
|
|
|
|
|
|
|
|
var statusTypeStrings = []string{
|
|
|
|
"waiting",
|
|
|
|
"inProgress",
|
|
|
|
"completed",
|
|
|
|
"error",
|
|
|
|
}
|
|
|
|
|
2024-03-05 00:58:43 +05:00
|
|
|
func (status StatusType) Name() string {
|
2024-02-17 19:08:58 +06:00
|
|
|
return statusTypeStrings[status]
|
|
|
|
}
|
|
|
|
|
2024-03-05 00:58:43 +05:00
|
|
|
func (status StatusType) Ordinal() int {
|
2024-02-17 19:08:58 +06:00
|
|
|
return int(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
type QueueListenerContract interface {
|
|
|
|
Add(key int, queue *Queue)
|
|
|
|
Remove(key int)
|
|
|
|
}
|
|
|
|
|
|
|
|
type QueueListContract interface {
|
|
|
|
AddListener(queueListener QueueListenerContract)
|
|
|
|
GetItems() map[int]*Queue
|
|
|
|
Add(setting *ConvertSetting)
|
|
|
|
Remove(key int)
|
|
|
|
GetItem(key int) (*Queue, error)
|
|
|
|
Next() (key int, queue *Queue)
|
|
|
|
}
|
|
|
|
|
|
|
|
type QueueList struct {
|
|
|
|
currentKey *int
|
|
|
|
items map[int]*Queue
|
|
|
|
queueListener map[int]QueueListenerContract
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewQueueList() *QueueList {
|
|
|
|
currentKey := 0
|
|
|
|
return &QueueList{
|
|
|
|
currentKey: ¤tKey,
|
|
|
|
items: map[int]*Queue{},
|
|
|
|
queueListener: map[int]QueueListenerContract{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l QueueList) GetItems() map[int]*Queue {
|
|
|
|
return l.items
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l QueueList) Add(setting *ConvertSetting) {
|
|
|
|
queue := Queue{
|
|
|
|
Setting: setting,
|
|
|
|
Status: StatusType(Waiting),
|
|
|
|
}
|
|
|
|
|
|
|
|
*l.currentKey += 1
|
|
|
|
l.items[*l.currentKey] = &queue
|
|
|
|
l.eventAdd(*l.currentKey, &queue)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l QueueList) Remove(key int) {
|
|
|
|
if _, ok := l.items[key]; ok {
|
|
|
|
delete(l.items, key)
|
|
|
|
l.eventRemove(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l QueueList) GetItem(key int) (*Queue, error) {
|
|
|
|
if item, ok := l.items[key]; ok {
|
|
|
|
return item, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("key not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l QueueList) AddListener(queueListener QueueListenerContract) {
|
|
|
|
l.queueListener[len(l.queueListener)] = queueListener
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l QueueList) eventAdd(key int, queue *Queue) {
|
|
|
|
for _, listener := range l.queueListener {
|
|
|
|
listener.Add(key, queue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l QueueList) eventRemove(key int) {
|
|
|
|
for _, listener := range l.queueListener {
|
|
|
|
listener.Remove(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l QueueList) Next() (key int, queue *Queue) {
|
|
|
|
statusWaiting := StatusType(Waiting)
|
|
|
|
for key, item := range l.items {
|
|
|
|
if item.Status == statusWaiting {
|
|
|
|
return key, item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1, nil
|
|
|
|
}
|