Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
task
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Gaia-X
Trust Services API
task
Commits
2bfcd368
Commit
2bfcd368
authored
3 years ago
by
Lyuben Penkovski
Browse files
Options
Downloads
Patches
Plain Diff
Queue-like interface for retrieving tasks from storage
parent
b1ac1880
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!3
Queue-like interface for retrieving tasks from storage
Pipeline
#49872
passed
3 years ago
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
internal/service/task/service.go
+5
-2
5 additions, 2 deletions
internal/service/task/service.go
internal/storage/storage.go
+51
-1
51 additions, 1 deletion
internal/storage/storage.go
with
56 additions
and
3 deletions
internal/service/task/service.go
+
5
−
2
View file @
2bfcd368
...
@@ -17,7 +17,10 @@ type Storage interface {
...
@@ -17,7 +17,10 @@ type Storage interface {
}
}
type
Queue
interface
{
type
Queue
interface
{
AddTask
(
ctx
context
.
Context
,
task
*
Task
)
error
Add
(
ctx
context
.
Context
,
task
*
Task
)
error
Poll
(
ctx
context
.
Context
)
(
*
Task
,
error
)
Ack
(
ctx
context
.
Context
,
task
*
Task
)
error
Unack
(
ctx
context
.
Context
,
task
*
Task
)
error
}
}
type
Service
struct
{
type
Service
struct
{
...
@@ -56,7 +59,7 @@ func (s *Service) Create(ctx context.Context, req *goatask.CreateRequest) (res *
...
@@ -56,7 +59,7 @@ func (s *Service) Create(ctx context.Context, req *goatask.CreateRequest) (res *
task
.
CreatedAt
=
time
.
Now
()
task
.
CreatedAt
=
time
.
Now
()
task
.
Request
=
taskRequest
task
.
Request
=
taskRequest
if
err
:=
s
.
queue
.
Add
Task
(
ctx
,
task
);
err
!=
nil
{
if
err
:=
s
.
queue
.
Add
(
ctx
,
task
);
err
!=
nil
{
logger
.
Error
(
"error adding task to queue"
,
zap
.
Error
(
err
))
logger
.
Error
(
"error adding task to queue"
,
zap
.
Error
(
err
))
return
nil
,
errors
.
New
(
"failed to create task"
,
err
)
return
nil
,
errors
.
New
(
"failed to create task"
,
err
)
}
}
...
...
This diff is collapsed.
Click to expand it.
internal/storage/storage.go
+
51
−
1
View file @
2bfcd368
...
@@ -6,6 +6,7 @@ import (
...
@@ -6,6 +6,7 @@ import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"code.vereign.com/gaiax/tsa/golib/errors"
"code.vereign.com/gaiax/tsa/golib/errors"
"code.vereign.com/gaiax/tsa/task/internal/service/task"
"code.vereign.com/gaiax/tsa/task/internal/service/task"
...
@@ -49,7 +50,56 @@ func (s *Storage) TaskTemplate(ctx context.Context, taskName string) (*task.Task
...
@@ -49,7 +50,56 @@ func (s *Storage) TaskTemplate(ctx context.Context, taskName string) (*task.Task
return
&
task
,
nil
return
&
task
,
nil
}
}
func
(
s
*
Storage
)
Add
Task
(
ctx
context
.
Context
,
task
*
task
.
Task
)
error
{
func
(
s
*
Storage
)
Add
(
ctx
context
.
Context
,
task
*
task
.
Task
)
error
{
_
,
err
:=
s
.
tasks
.
InsertOne
(
ctx
,
task
)
_
,
err
:=
s
.
tasks
.
InsertOne
(
ctx
,
task
)
return
err
return
err
}
}
// Poll retrieves one task from the tasks collection with the
// older ones being retrieved first (FIFO). It updates the state
// of the task to "pending", so that consequent calls to Poll would
// not retrieve the same task.
func
(
s
*
Storage
)
Poll
(
ctx
context
.
Context
)
(
*
task
.
Task
,
error
)
{
opts
:=
options
.
FindOneAndUpdate
()
.
SetSort
(
bson
.
M
{
"createdAt"
:
1
})
.
SetReturnDocument
(
options
.
After
)
filter
:=
bson
.
M
{
"state"
:
task
.
Created
}
update
:=
bson
.
M
{
"$set"
:
bson
.
M
{
"state"
:
task
.
Pending
}}
result
:=
s
.
tasks
.
FindOneAndUpdate
(
ctx
,
filter
,
update
,
opts
,
)
if
result
.
Err
()
!=
nil
{
if
strings
.
Contains
(
result
.
Err
()
.
Error
(),
"no documents in result"
)
{
return
nil
,
errors
.
New
(
errors
.
NotFound
,
"task not found"
)
}
return
nil
,
result
.
Err
()
}
var
task
task
.
Task
if
err
:=
result
.
Decode
(
&
task
);
err
!=
nil
{
return
nil
,
err
}
return
&
task
,
nil
}
// Ack removes a task from the tasks collection.
func
(
s
*
Storage
)
Ack
(
ctx
context
.
Context
,
task
*
task
.
Task
)
error
{
_
,
err
:=
s
.
tasks
.
DeleteOne
(
ctx
,
bson
.
M
{
"id"
:
task
.
ID
})
return
err
}
// Unack changes the "pending" state of a task to "created", so that
// it can be retrieved for processing again.
func
(
s
*
Storage
)
Unack
(
ctx
context
.
Context
,
t
*
task
.
Task
)
error
{
filter
:=
bson
.
M
{
"id"
:
t
.
ID
}
update
:=
bson
.
M
{
"$set"
:
bson
.
M
{
"state"
:
task
.
Created
}}
_
,
err
:=
s
.
tasks
.
UpdateOne
(
ctx
,
filter
,
update
)
return
err
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment