
All checks were successful
Deploy Dev / Build (pull_request) Successful in 39s
Deploy Dev / Push (pull_request) Successful in 24s
Deploy Dev / Deploy dev (pull_request) Successful in 8s
Deploy Prod / Build (pull_request) Successful in 38s
Deploy Prod / Push (pull_request) Successful in 25s
Deploy Prod / Deploy prod (pull_request) Successful in 8s
51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package routers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
tasks "queues-go/app/storage/mongo/collections"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
)
|
|
|
|
type PutRequestBody struct {
|
|
Payload bson.M `json:"payload"`
|
|
SecondsToExecute int `json:"seconds_to_execute"`
|
|
Delay *int `json:"delay"`
|
|
}
|
|
|
|
func Put(r *http.Request) (interface{}, int) {
|
|
d := json.NewDecoder(r.Body)
|
|
body := PutRequestBody{}
|
|
err := d.Decode(&body)
|
|
if err != nil {
|
|
return nil, http.StatusBadRequest
|
|
}
|
|
|
|
queue := r.Header.Get("queue")
|
|
var availableFrom time.Time
|
|
if body.Delay == nil {
|
|
availableFrom = time.Now()
|
|
} else {
|
|
availableFrom = time.Now().Add(time.Second + time.Duration(*body.Delay))
|
|
}
|
|
|
|
task := tasks.InsertedTask{
|
|
Queue: queue,
|
|
Payload: body.Payload,
|
|
PutAt: time.Now(),
|
|
AvailableFrom: availableFrom,
|
|
SecondsToExecute: body.SecondsToExecute,
|
|
TakenAt: nil,
|
|
Attempts: 0,
|
|
}
|
|
|
|
err = tasks.Add(task)
|
|
if err != nil {
|
|
return nil, http.StatusInternalServerError
|
|
}
|
|
|
|
return nil, http.StatusAccepted
|
|
}
|