Fix start problem with two IronFunction colliding configurations

By default, BoltDB will hang while waiting to acquire lock to the
datafile, thus the users might find themselves waiting for something
but not what. The added timeout aims inform use about what's
happening.

Also this renames MQADR to TASKSRV, refactor configuration to read
environment variables. RunAsyncRunner now fills the gaps when
parsing TASKSRV.

Fixes #119
This commit is contained in:
Carlos C
2016-10-04 23:45:02 +02:00
parent 3db24e2745
commit bc3fba088f
7 changed files with 83 additions and 63 deletions

View File

@@ -4,16 +4,15 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"time"
"golang.org/x/net/context"
log "github.com/Sirupsen/logrus"
"github.com/iron-io/functions/api/models"
"golang.org/x/net/context"
)
func getTask(url string) (*models.Task, error) {
@@ -96,10 +95,10 @@ func runTask(task *models.Task) error {
}
// RunAsyncRunner pulls tasks off a queue and processes them
func RunAsyncRunner(mqAdr string) {
url := fmt.Sprintf("http://%s/tasks", mqAdr)
func RunAsyncRunner(tasksrv, port string) {
u := tasksrvURL(tasksrv, port)
for {
task, err := getTask(url)
task, err := getTask(u)
if err != nil {
log.WithError(err)
time.Sleep(1 * time.Second)
@@ -115,10 +114,31 @@ func RunAsyncRunner(mqAdr string) {
log.Info("Processed task:", task.ID)
// Delete task from queue
if err := deleteTask(url, task); err != nil {
if err := deleteTask(u, task); err != nil {
log.WithError(err)
} else {
log.Info("Deleted task:", task.ID)
}
}
}
func tasksrvURL(tasksrv, port string) string {
parsed, err := url.Parse(tasksrv)
if err != nil {
log.Fatalf("cannot parse TASKSRV endpoint: %v", err)
}
if parsed.Scheme == "" {
parsed.Scheme = "http"
}
if parsed.Path == "" || parsed.Path == "/" {
parsed.Path = "/tasks"
}
if _, _, err := net.SplitHostPort(parsed.Host); err != nil {
parsed.Host = net.JoinHostPort(parsed.Host, port)
}
return parsed.String()
}