Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Job Queues
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Jira
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container registry
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Libraries
Go
Services
Job Queues
Commits
5f2b8c87
Verified
Commit
5f2b8c87
authored
1 year ago
by
Volker Schukai
Browse files
Options
Downloads
Patches
Plain Diff
feat: save jobs after execution
#8
parent
8d575c14
Branches
Branches containing commit
Tags
v1.6.0
Tags containing commit
No related merge requests found
Changes
22
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
runnable_test.go
+9
-6
9 additions, 6 deletions
runnable_test.go
worker.go
+18
-6
18 additions, 6 deletions
worker.go
with
27 additions
and
12 deletions
runnable_test.go
+
9
−
6
View file @
5f2b8c87
package
jobqueue
import
(
"context"
"errors"
"testing"
)
...
...
@@ -8,21 +9,21 @@ import (
// MockSuccessfulRunnable gibt immer ResultStatusSuccess zurück
type
MockSuccessfulRunnable
struct
{}
func
(
m
MockSuccessfulRunnable
)
Run
()
(
RunResult
[
string
],
error
)
{
func
(
m
MockSuccessfulRunnable
)
Run
(
ctx
context
.
Context
)
(
RunResult
[
string
],
error
)
{
return
RunResult
[
string
]{
Status
:
ResultStatusSuccess
,
Data
:
"Success"
},
nil
}
// MockFailedRunnable gibt immer ResultStatusFailed zurück
type
MockFailedRunnable
struct
{}
func
(
m
MockFailedRunnable
)
Run
()
(
RunResult
[
string
],
error
)
{
func
(
m
MockFailedRunnable
)
Run
(
ctx
context
.
Context
)
(
RunResult
[
string
],
error
)
{
return
RunResult
[
string
]{
Status
:
ResultStatusFailed
,
Data
:
"Failed"
},
nil
}
// MockErrorRunnable gibt immer einen Fehler zurück
type
MockErrorRunnable
struct
{}
func
(
m
MockErrorRunnable
)
Run
()
(
RunResult
[
string
],
error
)
{
func
(
m
MockErrorRunnable
)
Run
(
ctx
context
.
Context
)
(
RunResult
[
string
],
error
)
{
return
RunResult
[
string
]{},
errors
.
New
(
"RunError"
)
}
...
...
@@ -52,23 +53,25 @@ func (m MockSuccessfulRunnable) GetPersistence() RunnableImport {
func
TestRunnable
(
t
*
testing
.
T
)
{
var
run
Runnable
[
string
]
ctx
:=
context
.
Background
()
// Test für erfolgreiche Ausführung
run
=
MockSuccessfulRunnable
{}
result
,
err
:=
run
.
Run
()
result
,
err
:=
run
.
Run
(
ctx
)
if
result
.
Status
!=
ResultStatusSuccess
||
err
!=
nil
{
t
.
Errorf
(
"Expected success, got %v, %v"
,
result
.
Status
,
err
)
}
// Test für fehlgeschlagene Ausführung
run
=
MockFailedRunnable
{}
result
,
err
=
run
.
Run
()
result
,
err
=
run
.
Run
(
ctx
)
if
result
.
Status
!=
ResultStatusFailed
||
err
!=
nil
{
t
.
Errorf
(
"Expected failure, got %v, %v"
,
result
.
Status
,
err
)
}
// Test für Ausführungsfehler
run
=
MockErrorRunnable
{}
result
,
err
=
run
.
Run
()
result
,
err
=
run
.
Run
(
ctx
)
if
err
==
nil
{
t
.
Errorf
(
"Expected error, got nil"
)
}
...
...
This diff is collapsed.
Click to expand it.
worker.go
+
18
−
6
View file @
5f2b8c87
...
...
@@ -143,19 +143,22 @@ func (w *LocalWorker) run(jobChannel chan GenericJob, stopChan chan bool, cancel
var
err
error
for
retries
>
0
{
var
cancel
context
.
CancelFunc
timeout
:=
job
.
GetTimeout
()
if
timeout
==
0
{
timeout
=
1
*
time
.
Min
ut
e
if
timeout
>
0
{
ctx
,
cancel
=
context
.
WithTimeout
(
ctx
,
time
o
ut
)
}
ctxTimeout
,
cancelTimeout
:=
context
.
WithTimeout
(
ctx
,
timeout
)
if
w
.
manager
!=
nil
&&
w
.
manager
.
logger
!=
nil
{
w
.
manager
.
logger
.
Info
(
"Executing job on worker thread"
,
"worker"
,
w
.
ID
,
"thread_id"
,
workerThreadID
,
"job_id"
,
job
.
GetID
())
}
_
,
err
=
job
.
Execute
(
ctxTimeout
)
cancelTimeout
()
_
,
err
=
job
.
Execute
(
ctx
)
if
cancel
!=
nil
{
cancel
()
}
if
err
==
nil
||
ctx
.
Err
()
==
context
.
Canceled
{
break
...
...
@@ -170,6 +173,15 @@ func (w *LocalWorker) run(jobChannel chan GenericJob, stopChan chan bool, cancel
cancel
()
if
w
.
manager
!=
nil
&&
w
.
manager
.
dbSaver
!=
nil
{
err
=
w
.
manager
.
dbSaver
.
SaveJob
(
job
)
if
err
!=
nil
{
if
w
.
manager
.
logger
!=
nil
{
w
.
manager
.
logger
.
Error
(
"Error while saving job"
,
"job_id"
,
job
.
GetID
(),
"error"
,
err
)
}
}
}
case
<-
stopChan
:
stopFlag
=
true
break
...
...
This diff is collapsed.
Click to expand it.
Prev
1
2
Next
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