Documentation
¶
Index ¶
Constants ¶
const ( ScriptRunStatusSuccess = "success" ScriptRunStatusFail = "fail" )
Variables ¶
var ( ErrScriptSize = errors.New("script size exceeds limit") ErrScriptSyntax = errors.New("script is not valid python") ErrRunnerBusy = errors.New("no python worker available") ErrRunnerFailed = errors.New("python worker failed") ErrOutputLimit = errors.New("output limit exceeded") ErrRunnerClosed = errors.New("runner is closed") ErrRateLimited = errors.New("too many requests, retry later") )
var ScriptOrderFields = map[string]string{
"id": "id",
"name": "LOWER(name)",
"created_at": "created_at",
"updated_at": "updated_at",
}
ScriptOrderFields maps API-facing order keys to SQL column expressions for the scripts table.
var ScriptRunOrderFields = map[string]string{
"id": "id",
"started_at": "started_at",
"status": "status",
}
ScriptRunOrderFields maps API-facing order keys to SQL column expressions for the script_runs table.
Functions ¶
Types ¶
type Input ¶
type Input struct {
Payload map[string]any `json:"payload"`
Subtopic string `json:"subtopic,omitempty"`
Created int64 `json:"created,omitempty"`
ThingID string `json:"thing_id,omitempty"`
}
Input is what a run is given. It is the message shape the Python side sees as its "message" global, so the json tags are part of the contract with it.
type PageMetadata ¶
type PageMetadata struct {
Total uint64 `json:"total,omitempty"`
Offset uint64 `json:"offset,omitempty"`
Limit uint64 `json:"limit,omitempty"`
Order string `json:"order,omitempty"`
Dir string `json:"dir,omitempty"`
Name string `json:"name,omitempty"`
Status string `json:"status,omitempty"`
From time.Time `json:"from,omitempty"`
To time.Time `json:"to,omitempty"`
}
type Result ¶
type Result struct {
Value any
Logs []string
Error string
StartedAt time.Time
FinishedAt time.Time
Retired bool
}
Result is the outcome of one run. Error is either the script's own failure or the reason the run was cut short; the two are not distinguished here because a caller records both the same way.
type Runner ¶
type Runner interface {
Run(ctx context.Context, code string, in Input) (Result, error)
// Validate compiles code without executing it. It returns the Python syntax
// error as a string when the source does not compile, and an empty string
// when it does. A non-nil error means the check itself could not be run.
Validate(ctx context.Context, code string) (string, error)
Close() error
}
type ScriptRepository ¶
type ScriptRepository interface {
// SaveScripts persists multiple Python scripts.
SaveScripts(ctx context.Context, scripts ...Script) ([]Script, error)
// RetrieveScriptByID retrieves a single Script denoted by ID.
RetrieveScriptByID(ctx context.Context, id string) (Script, error)
// RetrieveGroupIDsByScripts retrieves the distinct IDs of the Groups the Scripts denoted by the provided IDs belong to.
RetrieveGroupIDsByScripts(ctx context.Context, ids ...string) ([]string, error)
// RetrieveScriptsByGroup retrieves a list of Scripts belonging to a specific Group, without their source.
RetrieveScriptsByGroup(ctx context.Context, groupID string, pm PageMetadata) (ScriptsPage, error)
// UpdateScript updates the Script denoted by script.ID.
UpdateScript(ctx context.Context, script Script) error
// RemoveScripts removes the Scripts denoted by the provided IDs.
RemoveScripts(ctx context.Context, ids ...string) error
// RemoveScriptsByGroup removes all Scripts belonging to a specific Group.
RemoveScriptsByGroup(ctx context.Context, groupID string) error
// SaveRuns persists multiple Script Runs.
SaveRuns(ctx context.Context, runs ...ScriptRun) ([]ScriptRun, error)
// RetrieveRunByID retrieves a single Run denoted by ID.
RetrieveRunByID(ctx context.Context, id string) (ScriptRun, error)
// RetrieveGroupIDsByRuns retrieves the distinct IDs of the Groups the Runs denoted by the provided IDs belong to.
RetrieveGroupIDsByRuns(ctx context.Context, ids ...string) ([]string, error)
// RetrieveRunsByScript retrieves a list of Runs belonging to a specific Script.
RetrieveRunsByScript(ctx context.Context, scriptID string, pm PageMetadata) (ScriptRunsPage, error)
// RemoveRuns removes the Runs denoted by the provided IDs.
RemoveRuns(ctx context.Context, ids ...string) error
}
type ScriptRunsPage ¶
type ScriptsPage ¶
type Service ¶
type Service interface {
// CreateScripts persists multiple Python scripts.
CreateScripts(ctx context.Context, token, groupID string, scripts ...Script) ([]Script, error)
// ListScriptsByGroup retrieves a list of scripts belonging to a specific Group.
ListScriptsByGroup(ctx context.Context, token, groupID string, pm PageMetadata) (ScriptsPage, error)
// ViewScript retrieves a specific Script by its ID.
ViewScript(ctx context.Context, token, id string) (Script, error)
// UpdateScript updates the Script identified by the provided ID.
UpdateScript(ctx context.Context, token string, script Script) error
// RemoveScripts removes the Scripts identified by the provided IDs.
RemoveScripts(ctx context.Context, token string, ids ...string) error
// RemoveScriptsByGroup removes all Scripts belonging to a specific Group.
RemoveScriptsByGroup(ctx context.Context, groupID string) error
// RunScript executes the Script identified by the provided ID and records the run.
RunScript(ctx context.Context, token, id string, in Input) (ScriptRun, error)
// ListRunsByScript retrieves a list of Runs belonging to a specific Script.
ListRunsByScript(ctx context.Context, token, scriptID string, pm PageMetadata) (ScriptRunsPage, error)
// RemoveRuns removes the Runs identified by the provided IDs.
RemoveRuns(ctx context.Context, token string, ids ...string) error
}
Service specifies an API that must be fulfilled by the domain service implementation, and all of its decorators (e.g. logging & metrics). All methods that accept a token parameter use it to identify and authorize the user performing the operation.
func New ¶
func New(scripts ScriptRepository, run Runner, things domain.ThingsClient, idp uuid.IDProvider, logger logger.Logger, maxScriptSize int) Service
New instantiates the pyscripts service implementation.