Compare commits

..

No commits in common. "bf152e6d139512eb1903d4a7213a41f658ada12f" and "c2659fb49c8bbb9bac8ce726b89693c1351fb1a9" have entirely different histories.

6 changed files with 12 additions and 30 deletions

1
.gitignore vendored
View File

@ -1 +0,0 @@
queues-go

View File

@ -1,6 +1,6 @@
FROM golang:alpine
RUN mkdir /code
WORKDIR /code
RUN mkdir /usr/src/app
WORKDIR /usr/src/app
COPY . .
RUN go build
ENTRYPOINT ["./queues-go"]

View File

@ -5,14 +5,12 @@ import (
"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"`
Payload json.RawMessage `json:"payload"`
SecondsToExecute int `json:"seconds_to_execute"`
Delay *int `json:"delay"`
}
func Put(w http.ResponseWriter, r *http.Request) {
@ -23,7 +21,6 @@ func Put(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
return
}
queue := r.Header.Get("queue")
var availableFrom time.Time
if body.Delay == nil {
@ -31,7 +28,6 @@ func Put(w http.ResponseWriter, r *http.Request) {
} else {
availableFrom = time.Now().Add(time.Second + time.Duration(*body.Delay))
}
task := tasks.InsertedTask{
Queue: queue,
Payload: body.Payload,
@ -41,12 +37,10 @@ func Put(w http.ResponseWriter, r *http.Request) {
TakenAt: nil,
Attempts: 0,
}
err = tasks.Add(task)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusAccepted)
}

View File

@ -20,11 +20,9 @@ func Take(w http.ResponseWriter, r *http.Request) {
queue := r.Header.Get("queue")
task, err := tasks.Take(queue)
if err != nil {
println("Error taking")
w.WriteHeader(http.StatusInternalServerError)
return
}
var response TakeResponse
if task == nil {
response.Task = nil
@ -35,13 +33,10 @@ func Take(w http.ResponseWriter, r *http.Request) {
Payload: task.Payload,
}
}
data, err := json.Marshal(response)
if err != nil {
println("Error marshalling")
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(data)
}

View File

@ -23,13 +23,13 @@ type Task struct {
}
type InsertedTask struct {
Queue string `bson:"queue"`
Payload bson.M `bson:"payload"`
PutAt time.Time `bson:"put_at"`
AvailableFrom time.Time `bson:"available_from"`
SecondsToExecute int `bson:"seconds_to_execute"`
TakenAt *time.Time `bson:"taken_at"`
Attempts int `bson:"attempts"`
Queue string `bson:"queue"`
Payload interface{} `bson:"payload"`
PutAt time.Time `bson:"put_at"`
AvailableFrom time.Time `bson:"available_from"`
SecondsToExecute int `bson:"seconds_to_execute"`
TakenAt *time.Time `bson:"taken_at"`
Attempts int `bson:"attempts"`
}
func Add(task InsertedTask) error {
@ -64,8 +64,6 @@ func Take(queue string) (*Task, error) {
}
_, err = collection().UpdateByID(context.TODO(), task.Id, bson.M{"$set": bson.M{"taken_at": now, "attempts": task.Attempts + 1}})
if err != nil {
println("Error updaing")
println(err.Error())
return nil, errors.ErrInternalError
}
return task, nil
@ -80,16 +78,12 @@ func findTask(queue string, now time.Time) (*Task, error) {
},
)
if err != nil {
println("Error find")
println(err.Error())
return nil, errors.ErrInternalError
}
var results []Task
err = cursor.All(context.TODO(), &results)
if err != nil {
println("Error all")
println(err.Error())
return nil, errors.ErrInternalError
}

BIN
queues-go Executable file

Binary file not shown.