fix
All checks were successful
Deploy Dev / Build (pull_request) Successful in 53s
Deploy Dev / Push (pull_request) Successful in 26s
Deploy Dev / Deploy dev (pull_request) Successful in 9s
Deploy Prod / Build (pull_request) Successful in 1m25s
Deploy Prod / Push (pull_request) Successful in 45s
Deploy Prod / Deploy prod (pull_request) Successful in 8s
All checks were successful
Deploy Dev / Build (pull_request) Successful in 53s
Deploy Dev / Push (pull_request) Successful in 26s
Deploy Dev / Deploy dev (pull_request) Successful in 9s
Deploy Prod / Build (pull_request) Successful in 1m25s
Deploy Prod / Push (pull_request) Successful in 45s
Deploy Prod / Deploy prod (pull_request) Successful in 8s
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
type Task struct {
|
||||
@@ -62,7 +63,19 @@ func Take(queue string) (*Task, error) {
|
||||
if task == nil {
|
||||
return nil, nil
|
||||
}
|
||||
_, err = collection().UpdateByID(context.TODO(), task.Id, bson.M{"$set": bson.M{"taken_at": now, "attempts": task.Attempts + 1}})
|
||||
_, err = collection().UpdateByID(
|
||||
context.TODO(),
|
||||
task.Id,
|
||||
bson.M{
|
||||
"$set": bson.M{
|
||||
"taken_at": now,
|
||||
"attempts": task.Attempts + 1,
|
||||
"available_from": now.Add(
|
||||
time.Duration(task.SecondsToExecute) * time.Second,
|
||||
),
|
||||
},
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
println("Error updaing")
|
||||
println(err.Error())
|
||||
@@ -78,6 +91,7 @@ func findTask(queue string, now time.Time) (*Task, error) {
|
||||
"queue": queue,
|
||||
"available_from": bson.M{"$lte": now},
|
||||
},
|
||||
options.Find().SetLimit(1),
|
||||
)
|
||||
if err != nil {
|
||||
println("Error find")
|
||||
@@ -94,14 +108,7 @@ func findTask(queue string, now time.Time) (*Task, error) {
|
||||
}
|
||||
|
||||
for _, task := range results {
|
||||
if task.TakenAt == nil {
|
||||
return &task, nil
|
||||
}
|
||||
takenAt := *task.TakenAt
|
||||
|
||||
if takenAt.Add(time.Second * time.Duration(task.SecondsToExecute)).Before(now) {
|
||||
return &task, nil
|
||||
}
|
||||
return &task, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user