Fix race condition during initialization (#163)

Currently, async workers are started before HTTP interface is available
to get their requests. It fixes by ensuring that async workers are
started after HTTP interface is up.

Essentially we are getting rid of an error message during bootstrap:

        ERRO[0000] Could not fetch task error=Get http://127.0.0.1:8080/tasks: dial tcp 127.0.0.1:8080: getsockopt: connection refused
This commit is contained in:
C Cirello
2016-10-13 22:56:34 +02:00
committed by Seif Lotfy سيف لطفي
parent 34b4b25092
commit df3d5b48ce
6 changed files with 60 additions and 22 deletions

View File

@@ -96,8 +96,11 @@ func runTask(task *models.Task) (drivers.RunResult, error) {
}
// RunAsyncRunner pulls tasks off a queue and processes them
func RunAsyncRunner(ctx context.Context, wgAsync *sync.WaitGroup, tasksrv, port string, n int) {
u := tasksrvURL(tasksrv, port)
func RunAsyncRunner(ctx context.Context, tasksrv, port string, n int) {
u, h := tasksrvURL(tasksrv, port)
if isHostOpen(h) {
return
}
var wg sync.WaitGroup
for i := 0; i < n; i++ {
@@ -107,7 +110,15 @@ func RunAsyncRunner(ctx context.Context, wgAsync *sync.WaitGroup, tasksrv, port
wg.Wait()
<-ctx.Done()
wgAsync.Done()
}
func isHostOpen(host string) bool {
conn, err := net.Dial("tcp", host)
available := err == nil
if available {
conn.Close()
}
return available
}
func startAsyncRunners(ctx context.Context, wg *sync.WaitGroup, i int, url string, runTask func(task *models.Task) (drivers.RunResult, error)) {
@@ -150,7 +161,7 @@ func startAsyncRunners(ctx context.Context, wg *sync.WaitGroup, i int, url strin
}
}
func tasksrvURL(tasksrv, port string) string {
func tasksrvURL(tasksrv, port string) (parsedURL, host string) {
parsed, err := url.Parse(tasksrv)
if err != nil {
log.Fatalf("cannot parse TASKSRV endpoint: %v", err)
@@ -168,5 +179,5 @@ func tasksrvURL(tasksrv, port string) string {
parsed.Host = net.JoinHostPort(parsed.Host, port)
}
return parsed.String()
return parsed.String(), parsed.Host
}