Skip to content

Commit

Permalink
Fixed reported issue #6 and #7
Browse files Browse the repository at this point in the history
- added rerun count in session handling
- added wait on app execution to avoid overlap exits too early
  • Loading branch information
zhongjie-cai committed Sep 3, 2022
1 parent 17cd61e commit bafa24a
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 106 deletions.
75 changes: 35 additions & 40 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,50 +1,45 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.154.2/containers/go
{
"name": "Go",
"dockerComposeFile": "./docker-compose.yaml",
"service": "jobrunner_code",
"shutdownAction": "stopCompose",
"name": "Go",
"dockerComposeFile": "./docker-compose.yaml",
"service": "jobrunner_code",
"shutdownAction": "stopCompose",

// Set *default* container specific settings.json values on container create.
"settings": {
"terminal.integrated.shell.linux": "/bin/bash",
"go.toolsManagement.checkForUpdates": "local",
"go.useLanguageServer": true,
"go.gopath": "/go",
"go.goroot": "/usr/local/go",
"go.toolsGopath": "/go/bin",
"go.buildOnSave": "package",
"go.testOnSave": true,
"go.coverOnTestPackage": true,
"go.coverageDecorator": {
"type": "gutter",
"coveredHighlightColor": "rgba(64,128,128,0.5)",
"uncoveredHighlightColor": "rgba(128,64,64,0.25)",
"coveredGutterStyle": "blockgreen",
"uncoveredGutterStyle": "blockred"
},
"go.coverOnSingleTest": true,
"go.coverOnSave": true,
"go.testFlags": ["-short"]
},
// Set *default* container specific settings.json values on container create.
"settings": {
"go.toolsManagement.checkForUpdates": "local",
"go.useLanguageServer": true,
"go.gopath": "/go",
"go.goroot": "/usr/local/go",
"go.toolsGopath": "/go/bin",
"go.buildOnSave": "package",
"go.testOnSave": true,
"go.coverOnTestPackage": true,
"go.coverageDecorator": {
"type": "gutter",
"coveredHighlightColor": "rgba(64,128,128,0.5)",
"uncoveredHighlightColor": "rgba(128,64,64,0.25)",
"coveredGutterStyle": "blockgreen",
"uncoveredGutterStyle": "blockred"
},
"go.coverOnSingleTest": true,
"go.coverOnSave": true,
"go.testFlags": ["-short"]
},

// Mount to proper location
"workspaceFolder": "/go/src/github.com/zhongjie-cai/job-runner",
// Mount to proper location
"workspaceFolder": "/go/src/github.com/zhongjie-cai/job-runner",

// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"golang.Go",
"eamodio.gitlens",
"yzhang.markdown-all-in-one"
],
// Add the IDs of extensions you want installed when the container is created.
"extensions": ["golang.Go", "eamodio.gitlens", "yzhang.markdown-all-in-one"]

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "go version",
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "go version",

// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
//"remoteUser": "vscode"
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
//"remoteUser": "vscode"
}
8 changes: 0 additions & 8 deletions 0_functionPointers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
"net/http"
"net/url"
"reflect"
"regexp"
"runtime"
"runtime/debug"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -44,7 +42,6 @@ var (
fmtPrintf = fmt.Printf
fmtSprintf = fmt.Sprintf
marshalIgnoreErrorFunc = marshalIgnoreError
debugStack = debug.Stack
)

// func pointers for injection / testing: handler.go
Expand Down Expand Up @@ -88,11 +85,6 @@ var (
stringsJoin = strings.Join
)

// func pointers for injection / testing: parameter.go
var (
regexpMatchString = regexp.MatchString
)

// func pointers for injection / testing: pointerutil.go
var (
reflectValueOf = reflect.ValueOf
Expand Down
33 changes: 7 additions & 26 deletions 0_functionPointers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (
"net/http"
"net/url"
"reflect"
"regexp"
"runtime"
"runtime/debug"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -62,8 +60,6 @@ var (
fmtSprintfCalled int
marshalIgnoreErrorFuncExpected int
marshalIgnoreErrorFuncCalled int
debugStackExpected int
debugStackCalled int
stringsSplitExpected int
stringsSplitCalled int
strconvAtoiExpected int
Expand Down Expand Up @@ -104,8 +100,6 @@ var (
sortStringsCalled int
stringsJoinExpected int
stringsJoinCalled int
regexpMatchStringExpected int
regexpMatchStringCalled int
reflectValueOfExpected int
reflectValueOfCalled int
timeDateExpected int
Expand Down Expand Up @@ -144,8 +138,6 @@ var (
ioutilNopCloserCalled int
bytesNewBufferExpected int
bytesNewBufferCalled int
constructResponseFuncExpected int
constructResponseFuncCalled int
logProcessEnterFuncExpected int
logProcessEnterFuncCalled int
logProcessExitFuncExpected int
Expand Down Expand Up @@ -276,7 +268,7 @@ func createMock(t *testing.T) {
}
handleSessionFuncExpected = 0
handleSessionFuncCalled = 0
handleSessionFunc = func(app *application, index int) error {
handleSessionFunc = func(app *application, index int, reruns int) error {
handleSessionFuncCalled++
return nil
}
Expand Down Expand Up @@ -329,12 +321,6 @@ func createMock(t *testing.T) {
marshalIgnoreErrorFuncCalled++
return ""
}
debugStackExpected = 0
debugStackCalled = 0
debugStack = func() []byte {
debugStackCalled++
return nil
}
stringsSplitExpected = 0
stringsSplitCalled = 0
stringsSplit = func(s, sep string) []string {
Expand All @@ -349,7 +335,7 @@ func createMock(t *testing.T) {
}
initiateSessionFuncExpected = 0
initiateSessionFuncCalled = 0
initiateSessionFunc = func(app *application, index int) *session {
initiateSessionFunc = func(app *application, index int, reruns int) *session {
initiateSessionFuncCalled++
return nil
}
Expand Down Expand Up @@ -453,12 +439,6 @@ func createMock(t *testing.T) {
stringsJoinCalled++
return ""
}
regexpMatchStringExpected = 0
regexpMatchStringCalled = 0
regexpMatchString = func(pattern string, s string) (bool, error) {
regexpMatchStringCalled++
return false, nil
}
reflectValueOfExpected = 0
reflectValueOfCalled = 0
reflectValueOf = func(i interface{}) reflect.Value {
Expand Down Expand Up @@ -825,8 +805,6 @@ func verifyAll(t *testing.T) {
assert.Equal(t, fmtSprintfExpected, fmtSprintfCalled, "Unexpected number of calls to method fmtSprintf")
marshalIgnoreErrorFunc = marshalIgnoreError
assert.Equal(t, marshalIgnoreErrorFuncExpected, marshalIgnoreErrorFuncCalled, "Unexpected number of calls to method marshalIgnoreErrorFunc")
debugStack = debug.Stack
assert.Equal(t, debugStackExpected, debugStackCalled, "Unexpected number of calls to debugStack")
stringsSplit = strings.Split
assert.Equal(t, stringsSplitExpected, stringsSplitCalled, "Unexpected number of calls to method stringsSplit")
strconvAtoi = strconv.Atoi
Expand Down Expand Up @@ -867,8 +845,6 @@ func verifyAll(t *testing.T) {
assert.Equal(t, sortStringsExpected, sortStringsCalled, "Unexpected number of calls to sortStrings")
stringsJoin = strings.Join
assert.Equal(t, stringsJoinExpected, stringsJoinCalled, "Unexpected number of calls to stringsJoin")
regexpMatchString = regexp.MatchString
assert.Equal(t, regexpMatchStringExpected, regexpMatchStringCalled, "Unexpected number of calls to regexpMatchString")
reflectValueOf = reflect.ValueOf
assert.Equal(t, reflectValueOfExpected, reflectValueOfCalled, "Unexpected number of calls to reflectValueOf")
timeDate = time.Date
Expand Down Expand Up @@ -1074,6 +1050,11 @@ func (session *dummySession) GetIndex() int {
return 0
}

func (session *dummySession) GetReruns() int {
assert.Fail(session.t, "Unexpected call to GetReruns")
return 0
}

func (session *dummySession) Attach(name string, value interface{}) bool {
assert.Fail(session.t, "Unexpected call to Attach")
return false
Expand Down
46 changes: 30 additions & 16 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jobrunner

import (
"sync"
"sync/atomic"
)

// Application is the interface for job runner application
Expand All @@ -20,13 +21,15 @@ type application struct {
name string
version string
instances int
reruns []int32
schedule Schedule
overlap bool
session *session
customization Customization
shutdown chan bool
started bool
lastErrors []error
waits sync.WaitGroup
}

// NewApplication creates a new application for job runner hosting
Expand All @@ -45,21 +48,23 @@ func NewApplication(
customization = customizationDefault
}
var application = &application{
name,
version,
instances,
schedule,
overlap,
&session{
uuidNew(),
0,
map[string]interface{}{},
customization,
name: name,
version: version,
instances: instances,
reruns: make([]int32, instances),
schedule: schedule,
overlap: overlap,
session: &session{
id: uuidNew(),
index: 0,
attachment: map[string]interface{}{},
customization: customization,
},
customization,
make(chan bool),
false,
[]error{},
customization: customization,
shutdown: make(chan bool),
started: false,
lastErrors: []error{},
waits: sync.WaitGroup{},
}
return application
}
Expand Down Expand Up @@ -197,10 +202,12 @@ func runInstances(app *application) {
var waitGroup sync.WaitGroup
for id := 0; id < app.instances; id++ {
waitGroup.Add(1)
go func(index int) {
atomic.AddInt32(&app.reruns[id], 1)
go func(index int, reruns int) {
var sessionError = handleSessionFunc(
app,
index,
reruns,
)
if sessionError != nil {
app.lastErrors = append(
Expand All @@ -209,9 +216,12 @@ func runInstances(app *application) {
)
}
waitGroup.Done()
}(id)
}(id, int(app.reruns[id]))
}
waitGroup.Wait()
if app.overlap {
app.waits.Done()
}
}

func scheduleExecution(app *application) {
Expand All @@ -223,6 +233,7 @@ func scheduleExecution(app *application) {
break
}
if app.overlap {
app.waits.Add(1)
go runInstancesFunc(
app,
)
Expand All @@ -232,6 +243,9 @@ func scheduleExecution(app *application) {
)
}
}
if app.overlap {
app.waits.Wait()
}
}

func runApplication(app *application) {
Expand Down
Loading

0 comments on commit bafa24a

Please sign in to comment.