48 lines
927 B
Go
48 lines
927 B
Go
package routers
|
|
|
|
import (
|
|
"net/http"
|
|
tasks "queues-go/app/storage/mongo/collections"
|
|
)
|
|
|
|
type TaskResponse struct {
|
|
Id string `json:"id"`
|
|
Attempt int `json:"attempt"`
|
|
Payload interface{} `json:"payload"`
|
|
}
|
|
|
|
type TakeResponse struct {
|
|
Task *TaskResponse `json:"task"`
|
|
}
|
|
|
|
func Take(r *http.Request) (interface{}, int) {
|
|
queue := r.Header.Get("queue")
|
|
// mutex := redis.Sync.NewMutex(fmt.Sprintf("lock_queues_%s", queue))
|
|
// for {
|
|
// err := mutex.Lock()
|
|
// if err != nil {
|
|
// time.Sleep(time.Millisecond * 5)
|
|
// } else {
|
|
// break
|
|
// }
|
|
// }
|
|
task, err := tasks.Take(queue)
|
|
// mutex.Unlock()
|
|
if err != nil {
|
|
return nil, http.StatusInternalServerError
|
|
}
|
|
|
|
var response TakeResponse
|
|
if task == nil {
|
|
response.Task = nil
|
|
} else {
|
|
response.Task = &TaskResponse{
|
|
Id: task.Id.Hex(),
|
|
Attempt: task.Attempts,
|
|
Payload: task.Payload,
|
|
}
|
|
}
|
|
|
|
return response, http.StatusOK
|
|
}
|