Refactor async runner and its reference and add unit tests

This commit is contained in:
Seif Lotfy
2016-09-30 18:21:34 +02:00
parent 067100bf9c
commit d8801d5be7
4 changed files with 231 additions and 60 deletions

View File

@@ -3,6 +3,7 @@ package runner
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
@@ -15,84 +16,109 @@ import (
"github.com/iron-io/functions/api/models"
)
func RunAsyncRunners(mqAdr string) {
url := fmt.Sprintf("http://%s/tasks", mqAdr)
logAndWait := func(err error) {
log.WithError(err)
time.Sleep(1 * time.Second)
func getTask(url string) (*models.Task, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
for {
resp, err := http.Get(url)
if err != nil {
logAndWait(err)
continue
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var task models.Task
if err := json.Unmarshal(body, &task); err != nil {
return nil, err
}
if task.ID == "" {
return nil, errors.New("Invalid Task: ID empty")
}
return &task, nil
}
func getCfg(task *models.Task) *Config {
var stdout bytes.Buffer // TODO: should limit the size of this, error if gets too big. akin to: https://golang.org/pkg/io/#LimitReader
stderr := NewFuncLogger(task.RouteName, "", *task.Image, task.ID) // TODO: missing path here, how do i get that?
if task.Timeout == nil {
timeout := int32(30)
task.Timeout = &timeout
}
cfg := &Config{
Image: *task.Image,
Timeout: time.Duration(*task.Timeout) * time.Second,
ID: task.ID,
AppName: task.RouteName,
Stdout: &stdout,
Stderr: stderr,
Env: task.EnvVars,
}
return cfg
}
func deleteTask(url string, task *models.Task) error {
// Unmarshal task to be sent over as a json
body, err := json.Marshal(task)
if err != nil {
return err
}
// Send out Delete request to delete task from queue
req, err := http.NewRequest(http.MethodDelete, url, bytes.NewBuffer(body))
if err != nil {
return err
}
c := &http.Client{}
if resp, err := c.Do(req); err != nil {
return err
} else if resp.StatusCode != http.StatusAccepted {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
logAndWait(err)
continue
return err
}
return errors.New(string(body))
}
return nil
}
var task models.Task
func runTask(task *models.Task) error {
// Set up runner and process task
cfg := getCfg(task)
ctx := context.Background()
rnr, err := New(NewMetricLogger())
if err != nil {
return err
}
_, err = rnr.Run(ctx, cfg)
return err
}
if err := json.Unmarshal(body, &task); err != nil {
logAndWait(err)
continue
}
if task.ID == "" {
// RunAsyncRunner pulls tasks off a queue and processes them
func RunAsyncRunner(mqAdr string) {
url := fmt.Sprintf("http://%s/tasks", mqAdr)
for {
task, err := getTask(url)
if err != nil {
log.WithError(err)
time.Sleep(1 * time.Second)
continue
}
log.Info("Picked up task:", task.ID)
var stdout bytes.Buffer // TODO: should limit the size of this, error if gets too big. akin to: https://golang.org/pkg/io/#LimitReader
stderr := NewFuncLogger(task.RouteName, "", *task.Image, task.ID) // TODO: missing path here, how do i get that?
if task.Timeout == nil {
timeout := int32(30)
task.Timeout = &timeout
}
cfg := &Config{
Image: *task.Image,
Timeout: time.Duration(*task.Timeout) * time.Second,
ID: task.ID,
AppName: task.RouteName,
Stdout: &stdout,
Stderr: stderr,
Env: task.EnvVars,
}
metricLogger := NewMetricLogger()
rnr, err := New(metricLogger)
if err != nil {
// Process Task
if err := runTask(task); err != nil {
log.WithError(err)
continue
}
ctx := context.Background()
if _, err = rnr.Run(ctx, cfg); err != nil {
log.WithError(err)
continue
}
log.Info("Processed task:", task.ID)
req, err := http.NewRequest(http.MethodDelete, url, bytes.NewBuffer(body))
if err != nil {
log.WithError(err)
}
c := &http.Client{}
if _, err := c.Do(req); err != nil {
// Delete task from queue
if err := deleteTask(url, task); err != nil {
log.WithError(err)
continue
} else {
log.Info("Deleted task:", task.ID)
}
log.Info("Deleted task:", task.ID)
}
}