Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#451 Added chocolatey package support for Functions. #564

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions api/datastore/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const routesTableCreate = `CREATE TABLE IF NOT EXISTS routes (
maxc int NOT NULL,
memory int NOT NULL,
timeout int NOT NULL,
idle_timeout int NOT NULL,
type varchar(16) NOT NULL,
headers text NOT NULL,
config text NOT NULL,
Expand All @@ -39,7 +40,7 @@ const extrasTableCreate = `CREATE TABLE IF NOT EXISTS extras (
value varchar(256) NOT NULL
);`

const routeSelector = `SELECT app_name, path, image, format, maxc, memory, type, timeout, headers, config FROM routes`
const routeSelector = `SELECT app_name, path, image, format, maxc, memory, type, timeout, idle_timeout, headers, config FROM routes`

type rowScanner interface {
Scan(dest ...interface{}) error
Expand Down Expand Up @@ -302,10 +303,11 @@ func (ds *MySQLDatastore) InsertRoute(ctx context.Context, route *models.Route)
memory,
type,
timeout,
idle_timeout,
headers,
config
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`,
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`,
route.AppName,
route.Path,
route.Image,
Expand All @@ -314,6 +316,7 @@ func (ds *MySQLDatastore) InsertRoute(ctx context.Context, route *models.Route)
route.Memory,
route.Type,
route.Timeout,
route.IdleTimeout,
string(hbyte),
string(cbyte),
)
Expand Down Expand Up @@ -359,6 +362,7 @@ func (ds *MySQLDatastore) UpdateRoute(ctx context.Context, newroute *models.Rout
memory = ?,
type = ?,
timeout = ?,
idle_timeout = ?,
headers = ?,
config = ?
WHERE app_name = ? AND path = ?;`,
Expand All @@ -368,6 +372,7 @@ func (ds *MySQLDatastore) UpdateRoute(ctx context.Context, newroute *models.Rout
route.Memory,
route.Type,
route.Timeout,
route.IdleTimeout,
string(hbyte),
string(cbyte),
route.AppName,
Expand Down Expand Up @@ -431,6 +436,7 @@ func scanRoute(scanner rowScanner, route *models.Route) error {
&route.Memory,
&route.Type,
&route.Timeout,
&route.IdleTimeout,
&headerStr,
&configStr,
)
Expand Down
55 changes: 38 additions & 17 deletions api/datastore/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (

"context"

"bytes"
"github.com/Sirupsen/logrus"
"github.com/iron-io/functions/api/datastore/internal/datastoreutil"
"github.com/iron-io/functions/api/models"
"github.com/lib/pq"
_ "github.com/lib/pq"
"bytes"
"github.com/iron-io/functions/api/datastore/internal/datastoreutil"
)

const routesTableCreate = `
Expand All @@ -25,6 +25,7 @@ CREATE TABLE IF NOT EXISTS routes (
maxc integer NOT NULL,
memory integer NOT NULL,
timeout integer NOT NULL,
idle_timeout integer NOT NULL,
type character varying(16) NOT NULL,
headers text NOT NULL,
config text NOT NULL,
Expand All @@ -41,7 +42,7 @@ const extrasTableCreate = `CREATE TABLE IF NOT EXISTS extras (
value character varying(256) NOT NULL
);`

const routeSelector = `SELECT app_name, path, image, format, maxc, memory, type, timeout, headers, config FROM routes`
const routeSelector = `SELECT app_name, path, image, format, maxc, memory, type, timeout, idle_timeout, headers, config FROM routes`

type rowScanner interface {
Scan(dest ...interface{}) error
Expand Down Expand Up @@ -120,7 +121,7 @@ func (ds *PostgresDatastore) UpdateApp(ctx context.Context, newapp *models.App)
return err
}

if config != "" {
if len(config) > 0 {
err := json.Unmarshal([]byte(config), &app.Config)
if err != nil {
return err
Expand Down Expand Up @@ -184,8 +185,11 @@ func (ds *PostgresDatastore) GetApp(ctx context.Context, name string) (*models.A
Name: resName,
}

if err := json.Unmarshal([]byte(config), &res.Config); err != nil {
return nil, err
if len(config) > 0 {
err := json.Unmarshal([]byte(config), &res.Config)
if err != nil {
return nil, err
}
}

return res, nil
Expand All @@ -202,7 +206,14 @@ func scanApp(scanner rowScanner, app *models.App) error {
return err
}

return json.Unmarshal([]byte(configStr), &app.Config)
if len(configStr) > 0 {
err = json.Unmarshal([]byte(configStr), &app.Config)
if err != nil {
return err
}
}

return nil
}

func (ds *PostgresDatastore) GetApps(ctx context.Context, filter *models.AppFilter) ([]*models.App, error) {
Expand Down Expand Up @@ -274,10 +285,11 @@ func (ds *PostgresDatastore) InsertRoute(ctx context.Context, route *models.Rout
memory,
type,
timeout,
idle_timeout,
headers,
config
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);`,
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11);`,
route.AppName,
route.Path,
route.Image,
Expand All @@ -286,13 +298,13 @@ func (ds *PostgresDatastore) InsertRoute(ctx context.Context, route *models.Rout
route.Memory,
route.Type,
route.Timeout,
route.IdleTimeout,
string(hbyte),
string(cbyte),
)
return err
})


if err != nil {
return nil, err
}
Expand Down Expand Up @@ -329,8 +341,9 @@ func (ds *PostgresDatastore) UpdateRoute(ctx context.Context, newroute *models.R
memory = $6,
type = $7,
timeout = $8,
headers = $9,
config = $10
idle_timeout = $9,
headers = $10,
config = $11
WHERE app_name = $1 AND path = $2;`,
route.AppName,
route.Path,
Expand All @@ -340,6 +353,7 @@ func (ds *PostgresDatastore) UpdateRoute(ctx context.Context, newroute *models.R
route.Memory,
route.Type,
route.Timeout,
route.IdleTimeout,
string(hbyte),
string(cbyte),
)
Expand Down Expand Up @@ -398,21 +412,29 @@ func scanRoute(scanner rowScanner, route *models.Route) error {
&route.Memory,
&route.Type,
&route.Timeout,
&route.IdleTimeout,
&headerStr,
&configStr,
)
if err != nil {
return err
}

if headerStr == "" {
return models.ErrRoutesNotFound
if len(headerStr) > 0 {
err = json.Unmarshal([]byte(headerStr), &route.Headers)
if err != nil {
return err
}
}

if err := json.Unmarshal([]byte(headerStr), &route.Headers); err != nil {
return err
if len(configStr) > 0 {
err = json.Unmarshal([]byte(configStr), &route.Config)
if err != nil {
return err
}
}
return json.Unmarshal([]byte(configStr), &route.Config)

return nil
}

func (ds *PostgresDatastore) GetRoute(ctx context.Context, appName, routePath string) (*models.Route, error) {
Expand Down Expand Up @@ -557,7 +579,6 @@ func (ds *PostgresDatastore) Get(ctx context.Context, key []byte) ([]byte, error
return []byte(value), nil
}


func (ds *PostgresDatastore) Tx(f func(*sql.Tx) error) error {
tx, err := ds.db.Begin()
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions api/models/new_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ type NewTask struct {

*/
Timeout *int32 `json:"timeout,omitempty"`

/* Hot function idle timeout in seconds before termination.

*/
IdleTimeout *int32 `json:"idle_timeout,omitempty"`
}

// Validate validates this new task
Expand Down
14 changes: 14 additions & 0 deletions api/models/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

const (
defaultRouteTimeout = 30 // seconds
htfnScaleDownTimeout = 30 // seconds
)

var (
Expand Down Expand Up @@ -39,6 +40,7 @@ type Route struct {
Format string `json:"format"`
MaxConcurrency int `json:"max_concurrency"`
Timeout int32 `json:"timeout"`
IdleTimeout int32 `json:"idle_timeout"`
Config `json:"config"`
}

Expand All @@ -54,6 +56,7 @@ var (
ErrRoutesValidationMissingType = errors.New("Missing route Type")
ErrRoutesValidationPathMalformed = errors.New("Path malformed")
ErrRoutesValidationNegativeTimeout = errors.New("Negative timeout")
ErrRoutesValidationNegativeIdleTimeout = errors.New("Negative idle timeout")
ErrRoutesValidationNegativeMaxConcurrency = errors.New("Negative MaxConcurrency")
)

Expand Down Expand Up @@ -86,6 +89,10 @@ func (r *Route) SetDefaults() {
if r.Timeout == 0 {
r.Timeout = defaultRouteTimeout
}

//if r.IdleTimeout == 0 {
// r.IdleTimeout = htfnScaleDownTimeout
//}
}

// Validate validates field values, skipping zeroed fields if skipZero is true.
Expand Down Expand Up @@ -141,6 +148,10 @@ func (r *Route) Validate(skipZero bool) error {
res = append(res, ErrRoutesValidationNegativeTimeout)
}

if r.IdleTimeout < 0 {
res = append(res, ErrRoutesValidationNegativeIdleTimeout)
}

if len(res) > 0 {
return apiErrors.CompositeValidationError(res...)
}
Expand Down Expand Up @@ -171,6 +182,9 @@ func (r *Route) Update(new *Route) {
if new.Timeout != 0 {
r.Timeout = new.Timeout
}
if new.IdleTimeout != 0 {
r.IdleTimeout = new.IdleTimeout
}
if new.Format != "" {
r.Format = new.Format
}
Expand Down
6 changes: 5 additions & 1 deletion api/runner/async_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@ func getTask(ctx context.Context, url string) (*models.Task, error) {
}

func getCfg(t *models.Task) *task.Config {
timeout := int32(30)
if t.Timeout == nil {
timeout := int32(30)
t.Timeout = &timeout
}
if t.IdleTimeout == nil {
t.IdleTimeout = &timeout
}

cfg := &task.Config{
Image: *t.Image,
Timeout: time.Duration(*t.Timeout) * time.Second,
IdleTimeout: time.Duration(*t.IdleTimeout) * time.Second,
ID: t.ID,
AppName: t.AppName,
Env: t.EnvVars,
Expand Down
3 changes: 2 additions & 1 deletion api/runner/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func (t *containerTask) Id() string { return t.cfg.ID }
func (t *containerTask) Route() string { return "" }
func (t *containerTask) Image() string { return t.cfg.Image }
func (t *containerTask) Timeout() time.Duration { return t.cfg.Timeout }
func (t *containerTask) Logger() (stdout, stderr io.Writer) { return t.cfg.Stdout, t.cfg.Stderr }
func (t *containerTask) IdleTimeout() time.Duration { return t.cfg.IdleTimeout }
func (t *containerTask) Logger() (io.Writer, io.Writer) { return t.cfg.Stdout, t.cfg.Stderr }
func (t *containerTask) Volumes() [][2]string { return [][2]string{} }
func (t *containerTask) WorkDir() string { return "" }

Expand Down
19 changes: 10 additions & 9 deletions api/runner/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import (
)

type Config struct {
ID string
Path string
Image string
Timeout time.Duration
AppName string
Memory uint64
Env map[string]string
Format string
MaxConcurrency int
ID string
Path string
Image string
Timeout time.Duration
IdleTimeout time.Duration
AppName string
Memory uint64
Env map[string]string
Format string
MaxConcurrency int

Stdin io.Reader
Stdout io.Writer
Expand Down
Loading