mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
* functions: add bounded concurrency * functions: plug runners to sync and async interfaces * functions: update documentation about the new env var * functions: fix test flakiness * functions: the runner is self-regulated, no need to set a number of runners * functions: push the execution to the background on incoming requests * functions: ensure async tasks are always on * functions: add prioritization to tasks consumption Ensure that Sync tasks are consumed before Async tasks. Also, fixes termination races problems for free. * functions: remove stale comments * functions: improve mem availability calculation * functions: parallel run for async tasks * functions: check for memory availability before pulling async task * functions: comment about rnr.hasAvailableMemory and sync.Cond * functions: implement memory check for async runners using Cond vars * functions: code grooming - remove unnecessary goroutines - fix stale docs - reorganize import group * Revert "functions: implement memory check for async runners using Cond vars" This reverts commit 922e64032201a177c03ce6a46240925e3d35430d. * Revert "functions: comment about rnr.hasAvailableMemory and sync.Cond" This reverts commit 49ad7d52d341f12da9603b1a1df9d145871f0e0a. * functions: set a minimum memory availability for sync * functions: simplify the implementation by removing the priority queue * functions: code grooming - code deduplication - review waitgroups Waits
186 lines
4.0 KiB
Go
186 lines
4.0 KiB
Go
package runner
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io/ioutil"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
"github.com/iron-io/functions/api/models"
|
|
"github.com/iron-io/runner/common"
|
|
)
|
|
|
|
func getTask(ctx context.Context, url string) (*models.Task, error) {
|
|
resp, err := http.Get(url)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
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, nil
|
|
}
|
|
return &task, nil
|
|
}
|
|
|
|
func getCfg(task *models.Task) *Config {
|
|
// TODO: should limit the size of this, error if gets too big. akin to: https://golang.org/pkg/io/#LimitReader
|
|
stderr := NewFuncLogger(task.AppName, task.Path, *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.AppName,
|
|
Stdout: stderr,
|
|
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 {
|
|
return err
|
|
}
|
|
return errors.New(string(body))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// RunAsyncRunner pulls tasks off a queue and processes them
|
|
func RunAsyncRunner(ctx context.Context, tasksrv string, tasks chan TaskRequest, rnr *Runner) {
|
|
u, h := tasksrvURL(tasksrv)
|
|
if isHostOpen(h) {
|
|
return
|
|
}
|
|
|
|
startAsyncRunners(ctx, u, tasks, rnr)
|
|
<-ctx.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, url string, tasks chan TaskRequest, rnr *Runner) {
|
|
var wg sync.WaitGroup
|
|
ctx, log := common.LoggerWithFields(ctx, logrus.Fields{"runner": "async"})
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
wg.Wait()
|
|
return
|
|
|
|
default:
|
|
if !rnr.hasAsyncAvailableMemory() {
|
|
log.Debug("memory full")
|
|
time.Sleep(1 * time.Second)
|
|
continue
|
|
}
|
|
task, err := getTask(ctx, url)
|
|
if err != nil {
|
|
if err, ok := err.(net.Error); ok && err.Timeout() {
|
|
log.WithError(err).Errorln("Could not fetch task, timeout.")
|
|
continue
|
|
}
|
|
log.WithError(err).Error("Could not fetch task")
|
|
time.Sleep(1 * time.Second)
|
|
continue
|
|
}
|
|
if task == nil {
|
|
time.Sleep(1 * time.Second)
|
|
continue
|
|
}
|
|
|
|
ctx, log := common.LoggerWithFields(ctx, logrus.Fields{"call_id": task.ID})
|
|
log.Debug("Running task:", task.ID)
|
|
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
// Process Task
|
|
if _, err := RunTask(tasks, ctx, getCfg(task)); err != nil {
|
|
log.WithError(err).Error("Cannot run task")
|
|
}
|
|
}()
|
|
|
|
log.Debug("Processed task")
|
|
|
|
// Delete task from queue
|
|
if err := deleteTask(url, task); err != nil {
|
|
log.WithError(err).Error("Cannot delete task")
|
|
continue
|
|
}
|
|
log.Info("Task complete")
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
func tasksrvURL(tasksrv string) (parsedURL, host string) {
|
|
parsed, err := url.Parse(tasksrv)
|
|
if err != nil {
|
|
logrus.WithError(err).Fatalln("cannot parse TASKSRV endpoint")
|
|
}
|
|
// host, port, err := net.SplitHostPort(parsed.Host)
|
|
// if err != nil {
|
|
// log.WithError(err).Fatalln("net.SplitHostPort")
|
|
// }
|
|
|
|
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, parsed)
|
|
// }
|
|
|
|
return parsed.String(), parsed.Host
|
|
}
|