service

package
v0.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2024 License: Apache-2.0 Imports: 42 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BuilderRunDir        = ".run"     // -> links to .build-%s
	BuilderIdFile        = ".buildid" // Contains the current build id
	BuilderBuildDir      = ".build"   // Used while building, renamed to .build-%s when done
	BuilderArchivePrefix = ".build-"  // The build directory format
)

Variables

View Source
var DefaultBuildEnv = map[string]string{
	"YARN_PRODUCTION": "1",
	"PM3_BUILDING":    "1",
}
View Source
var DefaultRunEnv = map[string]string{
	"PM3":              "1",
	"DEBIAN_FRONTEND":  "noninteractive",
	"NODE_ENV":         "production",
	"npm_config_prod":  "1",
	"NODE_NO_WARNINGS": "1",
	"FLASK_DEBUG":      "0",
}
View Source
var Registry = variant.NewRegistry[service]()
View Source
var SubnetAllocator = sync.OnceValue(func() *subnet.Allocator {
	return subnet.NewAllocator(*config.ServiceSubnet, true)
})

Functions

func KillOrphans

func KillOrphans()

func NewMemoryFileSystem

func NewMemoryFileSystem(path string, pattern *regexp.Regexp) (*memoryFileSystem, error)

Types

type Advisor added in v0.2.7

type Advisor interface {
	Advise(path string) any
}

type AppServer

type AppServer struct {
	*AppService
	Checksum     glob.Checksum
	Context      context.Context
	LoadBalancer *lb.LoadBalancer
	// contains filtered or unexported fields
}

func (*AppServer) GetLoadBalancer

func (run *AppServer) GetLoadBalancer() *lb.LoadBalancer

func (*AppServer) GetProcessTrees

func (run *AppServer) GetProcessTrees() (res []ProcessTree)

func (*AppServer) ServeHTTP

func (run *AppServer) ServeHTTP(w http.ResponseWriter, r *http.Request) vhttp.Result

func (*AppServer) Stop

func (run *AppServer) Stop(c context.Context)

type AppService

type AppService struct {
	Options
	Monitor          health.Monitor     `yaml:"monitor,omitempty"`           // The health monitor.
	LoadBalancerOpts lb.Options         `yaml:"lb,omitempty"`                // The load balancer.
	Root             string             `yaml:"root,omitempty"`              // The root directory of the app.
	Run              Command            `yaml:"run,omitempty"`               // The command to run the app.
	Build            util.Some[Command] `yaml:"build,omitempty"`             // The command to build the app.
	Shutdown         util.Some[Command] `yaml:"shutdown,omitempty"`          // The command to shutdown the app.
	Cluster          string             `yaml:"cluster,omitempty"`           // The number of instances to run.
	ClusterMin       string             `yaml:"cluster_min,omitempty"`       // The minimum number of instances to run.
	Env              map[string]string  `yaml:"env,omitempty"`               // The environment variables to set.
	EnvHost          string             `yaml:"env_host,omitempty"`          // The environment variable for the host.
	EnvPort          string             `yaml:"env_port,omitempty"`          // The environment variable for the port.
	EnvListen        string             `yaml:"env_listen,omitempty"`        // The environment variable for the address.
	ReadyTimeout     util.Duration      `yaml:"ready_timeout,omitempty"`     // The timeout for the app to become ready.
	StopTimeout      util.Duration      `yaml:"stop_timeout,omitempty"`      // The timeout for the app to stop.
	NoBuildControl   bool               `yaml:"no_build_control,omitempty"`  // If true, linking logic between .run and .build will be disabled.
	Background       bool               `yaml:"background,omitempty"`        // If true, the app is not a HTTP server.
	LogFile          string             `yaml:"log,omitempty"`               // The log file for stdout&stderr, default = app.log
	UnhealtyTimeout  util.Duration      `yaml:"unhealthy_timeout,omitempty"` // The timeout after which an unhealthy instance is killed.
	SlowStart        bool               `yaml:"slow_start,omitempty"`        // If true, instances are started one by one.
	MaxMemory        util.Size          `yaml:"max_memory,omitempty"`        // Maximum amount of memory the process is allowed to use, <= 0 means unlimited.
	AutoScale        bool               `yaml:"auto_scale,omitempty"`        // If true, the app will be auto-scaled.
	AutoScaleStreak  int                `yaml:"auto_scale_streak,omitempty"` // The number of consecutive ticks to trigger auto-scaling.
	AutoScaleDefer   util.Duration      `yaml:"auto_scale_defer,omitempty"`  // The time to wait until considering a process in auto-scaling.
	UpscalePercent   float64            `yaml:"upscale_percent,omitempty"`   // The percentage of CPU usage to trigger upscale.
	DownscalePercent float64            `yaml:"downscale_percent,omitempty"` // The percentage of CPU usage to trigger downscale.
	Stdin            bool               `yaml:"stdin,omitempty"`             // If true, the app will read from stdin.
	// contains filtered or unexported fields
}

func (*AppService) BuildApp

func (app *AppService) BuildApp(c context.Context, force bool) (chk glob.Checksum, err error)

func (*AppService) Prepare

func (app *AppService) Prepare(opt Options) error

func (*AppService) ShutdownApp

func (app *AppService) ShutdownApp(c context.Context, chk glob.Checksum) error

func (*AppService) Start

func (app *AppService) Start(c context.Context, invaliate bool) (instance Instance, err error)

func (*AppService) String

func (app *AppService) String() string

type BuildFS

type BuildFS struct {
	Root string
}

func (BuildFS) AbortBuild

func (bfs BuildFS) AbortBuild()

Aborts the current build.

func (BuildFS) Clean

func (bfs BuildFS) Clean()

Cleans the directory removing past builds.

func (BuildFS) Folders

func (bfs BuildFS) Folders() (r map[string]string)

Gets all relevant folders.

func (BuildFS) LinkRun

func (bfs BuildFS) LinkRun(buildDir string) error

Links the run directory.

func (BuildFS) PostBuild

func (bfs BuildFS) PostBuild(chk glob.Checksum) error

Runs the post-build steps.

func (BuildFS) PreBuild

func (bfs BuildFS) PreBuild() error

Runs the pre-build steps.

func (BuildFS) ReadBuildId

func (bfs BuildFS) ReadBuildId() (glob.Checksum, error)

Reads the build ID.

func (BuildFS) RunBuild

func (bfs BuildFS) RunBuild(chk glob.Checksum, cb func() error) error

Runs the builder.

func (BuildFS) UnlinkRun

func (bfs BuildFS) UnlinkRun() error

Unlinks the run directory.

func (BuildFS) WriteBuildId

func (bfs BuildFS) WriteBuildId(id glob.Checksum, buildDir string) error

Writes the build ID.

type Command

type Command struct {
	// contains filtered or unexported fields
}

func CommandFromString

func CommandFromString(s string) (c Command, e error)

func NewCommand

func NewCommand(exec string, args ...string) Command

func (*Command) Clone

func (c *Command) Clone() *Command

func (*Command) Create

func (c *Command) Create(root string, ctx context.Context) *exec.Cmd

func (Command) IsZero

func (c Command) IsZero() bool

func (Command) MarshalYAML

func (c Command) MarshalYAML() (any, error)

func (*Command) MergeEnv

func (c *Command) MergeEnv(m map[string]string)

func (Command) String

func (c Command) String() string

func (*Command) UnmarshalText

func (c *Command) UnmarshalText(data []byte) error

func (*Command) UnmarshalYAML

func (c *Command) UnmarshalYAML(node *yaml.Node) error

type FileServer

type FileServer struct {
	*FileService
	// contains filtered or unexported fields
}

func (*FileServer) ServeHTTP

func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) vhttp.Result

func (*FileServer) Stop

func (fsrv *FileServer) Stop(context.Context)

type FileService

type FileService struct {
	Options
	Path             string         `yaml:"path,omitempty"`               // The path to serve
	NotFound         string         `yaml:"404,omitempty"`                // Static file for 404 errors
	Dynamic          bool           `yaml:"dynamic,omitempty"`            // If true, the fileserver will serve files directly from the filesystem instead of in-memory
	Immutable        bool           `yaml:"immutable,omitempty"`          // If true, the fileserver will assume the files are immutable and set cache headers accordingly
	NoImmutableMatch bool           `yaml:"no_immutable_match,omitempty"` // If true, the fileserver will not assume the files are immutable if the path contains /immutable/
	IndexFile        string         `yaml:"index,omitempty"`              // The index file to serve if the path is a directory
	Match            *regexp.Regexp `yaml:"match,omitempty"`              // The pattern to match
}

func (*FileService) Prepare

func (fsrv *FileService) Prepare(opt Options) error

func (*FileService) Start

func (fsrv *FileService) Start(c context.Context, invaliate bool) (Instance, error)

func (*FileService) String

func (fsrv *FileService) String() string

Implement service.Service

func (*FileService) UnmarshalInline

func (fsrv *FileService) UnmarshalInline(text string) error

type FlaskApp

type FlaskApp struct {
	PyApp `yaml:",inline"`
}

func (*FlaskApp) Advise added in v0.2.7

func (app *FlaskApp) Advise(path string) any

func (*FlaskApp) Prepare

func (app *FlaskApp) Prepare(opt Options) error

type GluedCommand

type GluedCommand struct {
	*exec.Cmd
}

func (GluedCommand) Run

func (cmd GluedCommand) Run() (err error)

func (GluedCommand) Start

func (cmd GluedCommand) Start() (err error)

type GoApp

type GoApp struct {
	AppService `yaml:",inline"`
	Main       string `yaml:"main,omitempty"`
}

func (*GoApp) Advise added in v0.2.7

func (app *GoApp) Advise(path string) any

func (*GoApp) Prepare

func (app *GoApp) Prepare(opt Options) error

type Instance

type Instance interface {
	// Handles requests
	vhttp.Handler
	// Gracefully stops the instance
	Stop(c context.Context)
}

type InstanceLB

type InstanceLB interface {
	GetLoadBalancer() *lb.LoadBalancer
}

type InstanceProc

type InstanceProc interface {
	GetProcessTrees() []ProcessTree
}

type JsApp

type JsApp struct {
	AppService `yaml:",inline"`
	Index      string `yaml:"index,omitempty"`
}

func (*JsApp) Advise added in v0.2.7

func (app *JsApp) Advise(path string) any

func (*JsApp) Prepare

func (app *JsApp) Prepare(opt Options) error

type NpmApp

type NpmApp struct {
	AppService     `yaml:",inline"`
	StartScript    string `yaml:"start_script,omitempty"`
	BuildScript    string `yaml:"build_script,omitempty"`
	PackageManager string `yaml:"package_manager,omitempty"`
	NoInstall      bool   `yaml:"no_install,omitempty"`
}

func (*NpmApp) Advise added in v0.2.7

func (app *NpmApp) Advise(path string) any

func (*NpmApp) Prepare

func (app *NpmApp) Prepare(opt Options) error

type Options

type Options struct {
	Name        string       `yaml:"-"`
	ServiceRoot string       `yaml:"-"`
	Logger      *xlog.Logger `yaml:"-"`
}

type ProcMetrics

type ProcMetrics struct {
	PID        int32     `json:"pid"`
	CreateTime time.Time `json:"create_time,omitempty"`
	Cmd        string    `json:"cmd,omitempty"`
	ProcUsageMetrics
}

func GetProcMetrics

func GetProcMetrics(p *process.Process) (metrics ProcMetrics)

func (*ProcMetrics) BriefCmd

func (m *ProcMetrics) BriefCmd() string

type ProcTreeMetrics

type ProcTreeMetrics struct {
	ProcMetrics
	Tree map[int32]ProcMetrics `json:"tree,omitempty"`
}

type ProcUsageMetrics

type ProcUsageMetrics struct {
	CPU     float64 `json:"cpu,omitempty"`
	RSS     uint64  `json:"rss,omitempty"`
	VMS     uint64  `json:"vms,omitempty"`
	HWM     uint64  `json:"hwm,omitempty"`
	Data    uint64  `json:"data,omitempty"`
	Stack   uint64  `json:"stack,omitempty"`
	Locked  uint64  `json:"locked,omitempty"`
	Swap    uint64  `json:"swap,omitempty"`
	IoRead  uint64  `json:"io_read,omitempty"`
	IoWrite uint64  `json:"io_write,omitempty"`
}

func (*ProcUsageMetrics) Combine

func (u *ProcUsageMetrics) Combine(other ProcUsageMetrics)

func (*ProcUsageMetrics) Fill

func (u *ProcUsageMetrics) Fill(proc *process.Process)

type ProcessTree

type ProcessTree struct {
	Parent *process.Process
	Tree   map[int32]*process.Process
}

func NewProcessTree

func NewProcessTree(p *process.Process) (tree ProcessTree)

func (*ProcessTree) AddProcess

func (tree *ProcessTree) AddProcess(p *process.Process)

func (*ProcessTree) AddProcessPid

func (tree *ProcessTree) AddProcessPid(pid int32)

func (ProcessTree) IsParentRunning

func (tree ProcessTree) IsParentRunning() bool

func (ProcessTree) IsZero

func (tree ProcessTree) IsZero() bool

func (ProcessTree) Kill

func (tree ProcessTree) Kill()

func (ProcessTree) Metrics

func (tree ProcessTree) Metrics() (metrics ProcTreeMetrics)

func (ProcessTree) Refresh

func (tree ProcessTree) Refresh()

type ProxyService

type ProxyService struct {
	Options
	Monitor      health.Monitor    `yaml:"monitor,omitempty"`
	LoadBalancer lb.LoadBalancer   `yaml:"lb,omitempty"`
	Upstreams    util.Some[string] `yaml:"upstreams,omitempty"`
}

func (*ProxyService) GetLoadBalancer

func (px *ProxyService) GetLoadBalancer() *lb.LoadBalancer

func (*ProxyService) Prepare

func (px *ProxyService) Prepare(opt Options) error

func (*ProxyService) ServeHTTP

func (px *ProxyService) ServeHTTP(w http.ResponseWriter, r *http.Request) vhttp.Result

Implement service.Instance and service.InstanceLB

func (*ProxyService) Start

func (px *ProxyService) Start(c context.Context, invaliate bool) (Instance, error)

func (*ProxyService) Stop

func (px *ProxyService) Stop(context.Context)

func (*ProxyService) String

func (px *ProxyService) String() string

Implement service.Service

func (*ProxyService) UnmarshalInline

func (px *ProxyService) UnmarshalInline(text string) error

type PyApp

type PyApp struct {
	AppService   `yaml:",inline"`
	Requirements string `yaml:"requirements,omitempty"`
	Main         string `yaml:"main,omitempty"` // Main file to run
}

func (*PyApp) Advise added in v0.2.7

func (app *PyApp) Advise(path string) any

func (*PyApp) Prepare

func (app *PyApp) Prepare(opt Options) error

type Service

type Service struct {
	// contains filtered or unexported fields
}

func (*Service) UnmarshalYAML

func (t *Service) UnmarshalYAML(node *yaml.Node) (e error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL