mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
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:
@@ -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()
|
||||
}
|
||||
|
||||
@@ -142,3 +142,25 @@ func TestDeleteTask(t *testing.T) {
|
||||
t.Error("expected no error, got", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTasksrvURL(t *testing.T) {
|
||||
tests := []struct {
|
||||
port, in, out string
|
||||
}{
|
||||
{"8080", "//localhost", "http://localhost:8080/tasks"},
|
||||
{"8080", "//localhost/", "http://localhost:8080/tasks"},
|
||||
{"8080", "//localhost:8081", "http://localhost:8081/tasks"},
|
||||
{"8080", "//localhost:8081/", "http://localhost:8081/tasks"},
|
||||
{"8080", "http://localhost", "http://localhost:8080/tasks"},
|
||||
{"8080", "http://localhost/", "http://localhost:8080/tasks"},
|
||||
{"8080", "http://localhost:8081", "http://localhost:8081/tasks"},
|
||||
{"8080", "http://localhost:8081/", "http://localhost:8081/tasks"},
|
||||
{"8080", "http://localhost:8081/endpoint", "http://localhost:8081/endpoint"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
if got := tasksrvURL(tt.in, tt.port); got != tt.out {
|
||||
t.Errorf("port: %s\ttasksrv: %s\texpected: %s\tgot: %s", tt.port, tt.in, tt.out, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,3 +107,5 @@ func TestRunnerError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user