Skip to content
Snippets Groups Projects
Verified Commit 5f2b8c87 authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

feat: save jobs after execution #8

parent 8d575c14
Branches
Tags v1.6.0
No related merge requests found
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")
}
......
......@@ -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.Minute
if timeout > 0 {
ctx, cancel = context.WithTimeout(ctx, timeout)
}
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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment