{"modulePath":"github.com/taskcluster/taskcluster-worker","version":"v0.1.20","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package plugins\n\nPackage plugins defines interfaces to be implemented by feature plugins.\n\nA plugin is an object that gets called for each step in the task execution flow. At each step the plugin will get access to engine and runtime object, such as the SandboxBuilder, the Sandbox, the SandboxContext, etc. A plugin is then supposed to implement a specific feature using these objects, this could be live logging, artifact uploading, attachment of proxies, mouting of caches, archival of caches, and many other things.\n\nA plugin does not get any plugin specific task.payload space, instead the options required by a plugin must be part of the task.payload. Hence, all plugins must be available on all platforms. To facilitate this, plugins should not use platform specific APIs, instead they should rely on interface offered by the engine objects to do so, fail gracefully if an engine method is not supported in a given configuration and returns ErrFeatureNotSupported.\n\nWhen a plugin encounters an unsupported feature that it needs, it may either return a MalformedPayloadError, or simply ignore the error and workaround it.\n\nPlugin packages should provide a method:\n\n\tNewXXXPluginFactory(engine.Engine,*runtime.EngineContext) PluginFactory\n\nand have it registered in pluginmanager.go\n\nAt high-level, PluginFactory stores the global state owned by the plugin, and Plugin stores the task-specific state owned by the plugin. A new Plugin instance will be created for each task.\n\nIn summary, plugins are not really \"plugins\", they are merely abstractions that allows us to implement features in complete isolation. Maybe they will become more flexible in the future, but there is no need to design for this at this point.\n\n## Functions\n\n```go\nfunc PluginManagerConfigSchema() schematypes.Object\n```\nPluginManagerConfigSchema returns configuration for PluginOptions.Config for NewPluginManager.\n\n```go\nfunc Plugins() map[string]PluginProvider\n```\nPlugins returns map from plugin name to PluginProviders.\n\n```go\nfunc Register(name string, provider PluginProvider)\n```\nRegister will register a PluginProvider. This is meant to be called from init(), and will panic if name is already used by another plugin.\n\n## Types\n\n```go\ntype Plugin interface {\n\t// Documentation returns a list of sections with end-user documentation.\n\t//\n\t// These sections will be combined with documentation sections from all\n\t// enabled plugins in-order to form end-user documentation.\n\tDocumentation() []runtime.Section\n\n\t// PayloadSchema returns a schematypes.Object with the properties for\n\t// for the TaskPluginOptions.Payload property.\n\t//\n\t// Note: this will be merged with payload schemas from engine and other\n\t// plugins, thus, it cannot contain conflicting properties. Furthermore the\n\t// metadata will be discarded and additionalProperties will not be allowed.\n\tPayloadSchema() schematypes.Object\n\n\t// NewTaskPlugin method will be called once for each task. The TaskPlugin\n\t// instance returned will be called for each stage in the task execution.\n\t//\n\t// NewTaskPlugin will be called in parallel with NewSandboxBuilder(), making\n\t// it a good place to start long-running operations, you then have to take\n\t// care to clean-up in Dispose() if they are still running.\n\t// You should wait for your long-running operations to finish in\n\t//  BuildSandbox() or whatever hook you need them in.\n\t//\n\t// Plugins implementing logging should not return an error here, as it\n\t// naturally follows that such an error can't be logged if no logging plugin\n\t// is created.\n\t//\n\t// Implementors may return TaskPluginBase{}, if the plugin doesn't have any\n\t// hooks for the given tasks.\n\t//\n\t// Non-fatal errors: MalformedPayloadError\n\tNewTaskPlugin(options TaskPluginOptions) (TaskPlugin, error)\n\n\t// ReportIdle is called if the worker is idle prior to polling for tasks.\n\t// The durationSinceBusy is the time since the worker as last busy.\n\t//\n\t// This is a perfect time to initiate a graceful stop the worker by\n\t// calling runtime.Environment.Worker.StopGracefully(). A plugin can call\n\t// StopGracefully() at any time, causing the worker to stop polling.\n\t// This hook is mainly useful as a way to gracefully stop the worker without\n\t// entering a new billing cycle, which might happen if you gracefully stop\n\t// while the worker is processing one or more tasks.\n\t//\n\t// Warning: This hook is called as part of the worker claim loop, hence, it\n\t// strongly to execution time in this method\n\tReportIdle(durationSinceBusy time.Duration)\n\n\t// ReportNonFatalError is called if the worker encountered a non-fatal error.\n\t//\n\t// Plugins can use this to implement a heuristic for shutting down after\n\t// certain number of non-fatal errors, or purging caches. This is mainly\n\t// intended to allow a configurable plugin to decide if the worker should\n\t// stop in response to a non-fatal error.\n\t//\n\t// The specific non-fatal error message is not available to the heuristic.\n\t// If a special heuristic is desired for a special non-fatal error, then this\n\t// should be handled in the plugin/engine where the error origins.\n\tReportNonFatalError()\n\n\t// Dispose is called when the worker is stopping, a plugin should free all\n\t// resources and halt all background processes.\n\t//\n\t// This is important for testing, but also for relevant when running on\n\t// bare metal machines that aren't re-imaged between worker restarts.\n\tDispose() error\n}\n```\nPlugin is a plugin to the worker, for each task NewTaskPlugin is created. The Plugin instance is responsible for creating these objects and managing data shared between TaskPlugin instances.\n\nAll methods on this interface must be thread-safe.\n\n```go\ntype PluginBase struct{}\n```\nPluginBase is a base implementation of the Plugin interface, it just handles all methods and does nothing.\n\nImplementors should embed this to ensure forward compatibility when we add new optional methods.\n\n```go\nfunc (PluginBase) Dispose() error\n```\nDispose does nothing\n\n```go\nfunc (PluginBase) Documentation() []runtime.Section\n```\nDocumentation returns no documentation.\n\n```go\nfunc (PluginBase) NewTaskPlugin(TaskPluginOptions) (TaskPlugin, error)\n```\nNewTaskPlugin returns TaskPluginBase{} which ignores all the stages.\n\n```go\nfunc (PluginBase) PayloadSchema() schematypes.Object\n```\nPayloadSchema returns a schema for an empty object for plugins that doesn't take any payload.\n\n```go\nfunc (PluginBase) ReportIdle(time.Duration)\n```\nReportIdle does nothing\n\n```go\nfunc (PluginBase) ReportNonFatalError()\n```\nReportNonFatalError does nothing\n\n```go\ntype PluginManager struct {\n\t// contains filtered or unexported fields\n}\n```\nA PluginManager combines a collection of plugins and implements an interface similar to Plugin. The interface is not exactly the same, notably, if there is an error, the PluginManager still returns a TaskPlugin wrapping plugins that didn't fail, such that artifacts and logs can be uploaded.\n\nFurthermore, the PluginManager doesn't require that the task payload matches the PayloadSchema(), as such a requirement would preclude running logging plugins to upload logs for the task. Which is important as errors in the task log makes no sense, if the task log isn't uploaded.\n\n```go\nfunc NewPluginManager(options PluginOptions) (*PluginManager, error)\n```\nNewPluginManager loads all plugins not disabled in configuration and returns a Plugin implementation that wraps all of the plugins.\n\nThis expects options.Config satisfying schema from PluginManagerConfigSchema().\n\nNote. While the PluginManager implements the Plugin interface, it does have different semantics for NewTaskPlugin(). This is because we need to run other TaskPlugins (such as logging), even if one of the TaskPlugins fails to be created or there is a schema validation error.\n\n```go\nfunc (pm *PluginManager) Dispose() error\n```\nDispose calls Dispose on all the managed plugins.\n\n```go\nfunc (pm *PluginManager) Documentation() []runtime.Section\n```\nDocumentation will collect documentation from all managed plugins.\n\n```go\nfunc (pm *PluginManager) NewTaskPlugin(options TaskPluginOptions) (TaskPlugin, error)\n```\nNewTaskPlugin constructs a TaskPlugin wrapping all the managed plugins whose PayloadSchema is satisfied by options.Payload.\n\nThis method always returns a TaskPlugin, even if there is errors. Because plugins that don't fail still want their Exception hook invoked.\n\n```go\nfunc (pm *PluginManager) PayloadSchema() schematypes.Object\n```\nPayloadSchema returns the 'task.payload' schema expected by plugins.\n\n```go\nfunc (pm *PluginManager) ReportIdle(durationSinceBusy time.Duration)\n```\nReportIdle call ReportIdle on all the managed plugins.\n\n```go\nfunc (pm *PluginManager) ReportNonFatalError()\n```\nReportNonFatalError calls ReportNonFatalError on all the managed plugins.\n\n```go\ntype PluginOptions struct {\n\tEnvironment *runtime.Environment\n\tEngine      engines.Engine\n\tMonitor     runtime.Monitor\n\tConfig      interface{}\n}\n```\nPluginOptions is a wrapper for the arguments/options given when instantiating a Plugin using PluginProvider.\n\nWe wrap all arguments so that we can add additional properties without breaking source compatibility with older plugins.\n\n```go\ntype PluginProvider interface {\n\tNewPlugin(options PluginOptions) (Plugin, error)\n\n\t// ConfigSchema returns schema for the PluginOptions.Config property.\n\t// May return nil, if no configuration should be given.\n\tConfigSchema() schematypes.Schema\n}\n```\nThe PluginProvider interface must be implemented and registered by anyone implementing a Plugin.\n\nIf an implementor can determine that a plugin isn't available at compile-time it is preferred not to register the plugin.\n\n```go\ntype PluginProviderBase struct{}\n```\nPluginProviderBase is a base struct that provides empty implementations of some methods for PluginProvider\n\nImplementors of PluginProvider should embed this struct to ensure forward compatibility when we add new optional method to PluginProvider.\n\n```go\nfunc (PluginProviderBase) ConfigSchema() schematypes.Schema\n```\nConfigSchema returns a nil schema.\n\n```go\ntype TaskPlugin interface {\n\t// BuildSandbox is called once NewSandboxBuilder() has returned.\n\t//\n\t// This is the place to wait for downloads and other expensive operations to\n\t// finished, before mounting caches, proxies, etc. and returning.\n\t//\n\t// Non-fatal errors: MalformedPayloadError\n\tBuildSandbox(sandboxBuilder engines.SandboxBuilder) error\n\n\t// Started is called once the sandbox has started execution. This is a good\n\t// place to hook if you want to do interactive things.\n\t//\n\t// Non-fatal errors: MalformedPayloadError\n\tStarted(sandbox engines.Sandbox) error\n\n\t// Stopped is called once the sandbox has terminated.\n\t//\n\t// This is a good place to upload artifacts, logs, check exit code, and start\n\t// to clean-up resources if such clean-up is expected to take a while.\n\t//\n\t// This will return false if the operation could not be completed successfully.\n\t// Such as artifact upload failure or files not existing.  If this returns false\n\t// it shall be assumed that the task has failed and should be reported as a failure.\n\t//\n\t// Non-fatal errors: MalformedPayloadError\n\tStopped(result engines.ResultSet) (bool, error)\n\n\t// Finished is called once the sandbox has terminated and Stopped() have been\n\t// called.\n\t//\n\t// At this stage the task-specific log is closed, and attempts to log data\n\t// using the previous TaskContext will fail. That makes this a good place to\n\t// upload logs and any processing of the logs. In fact logging is the primary\n\t// motivation for this stage.\n\t//\n\t// As there is no logging in this method, it's not recommend to do anything\n\t// that may fail here.\n\tFinished(success bool) error\n\n\t// Exception is called once the task is resolved exception. This may happen\n\t// instead of calls to Prepare(), BuildSandbox(), Started(), Stopped(), or\n\t// Finished().\n\t//\n\t// This is a good place for best-effort to upload artifacts and logs that you\n\t// wish to persist. Naturally, log messages written at this stage will be\n\t// dropped and all error messages will be fatal.\n\t//\n\t// Implementors should be aware that additional reasons may be added in the\n\t// future. Therefore they must handle the default case, if switching on the\n\t// reason parameter.\n\tException(reason runtime.ExceptionReason) error\n\n\t// Dispose is called once everything is done and it's time for clean-up.\n\t//\n\t// This method will be invoked following Finished() or Exception(). It is then\n\t// the responsibility of the implementor to abort or wait for any long-running\n\t// processes and clean-up any resources held.\n\tDispose() error\n}\n```\nTaskPlugin holds the task-specific state for a plugin\n\nEach method on this interface represents stage in the task execution and will be called when this stage is reached. The methods are allowed to take significant amounts of time, as they will run asynchronously.\n\nThese methods does not have to be thread-safe, we will never call the next method, before the previous method has returned.\n\nImplementors of this interface should be sure to embed TaskPluginBase. This will do absolutely nothing, but provide empty implementations for any current and future methods that isn't implemented.\n\nThe methods are called in the order listed here with the exception of Exception() which may be called following any method, and Dispose() which will always be called as a final step allowing you to clean up.\n\n```go\ntype TaskPluginBase struct{}\n```\nTaskPluginBase is a base implementation of the TaskPlugin interface, it just handles all methods and does nothing.\n\nImplementors should embed this to ensure forward compatibility when we add new optional methods.\n\n```go\nfunc (TaskPluginBase) BuildSandbox(engines.SandboxBuilder) error\n```\nBuildSandbox ignores the sandbox building stage.\n\n```go\nfunc (TaskPluginBase) Dispose() error\n```\nDispose ignores the stage where resources are disposed.\n\n```go\nfunc (TaskPluginBase) Exception(reason runtime.ExceptionReason) error\n```\nException ignores the stage where a task is resolved exception\n\n```go\nfunc (TaskPluginBase) Finished(success bool) error\n```\nFinished ignores the stage where a task has been finished\n\n```go\nfunc (TaskPluginBase) Started(engines.Sandbox) error\n```\nStarted ignores the stage where the sandbox has started\n\n```go\nfunc (TaskPluginBase) Stopped(engines.ResultSet) (bool, error)\n```\nStopped ignores the stage where the sandbox has returned a ResultSet, and returns true saying the task was successful, as not to poison the water.\n\n```go\ntype TaskPluginOptions struct {\n\tTaskInfo    *runtime.TaskInfo\n\tTaskContext *runtime.TaskContext\n\tPayload     map[string]interface{}\n\tMonitor     runtime.Monitor\n}\n```\nThe TaskPluginOptions is a wrapper for the set of arguments given to NewTaskPlugin.\n\nWe wrap the arguments in a single argument to maintain source compatibility when introducing additional arguments.\n\n","path":"github.com/taskcluster/taskcluster-worker/plugins","name":"plugins","synopsis":"Package plugins defines interfaces to be implemented by feature plugins.","isRedistributable":true}
