jobkit

package
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2019 License: MIT Imports: 17 Imported by: 0

README

jobkit

This package is meant to be a suite of helpers to make writing robust job workers easier.

It provides facilities that plug into go-sdk/cron to help with:

  • A management server to streamline allowing forced runs of jobs.
  • Sending email notifications for job results.
  • Sending slack notifications for job results.
  • Logging Airbrakes
  • Logging DD Metrics

Documentation

Index

Constants

View Source
const (
	// DefaultEmailMimeType is the default email mime type.
	DefaultEmailMimeType = "text/plain"

	// DefaultEmailSubjectTemplate is the default subject template.
	DefaultEmailSubjectTemplate = `{{.Var "jobName" }} :: {{ .Var "status" }}`

	// DefaultEmailHTMLBodyTemplate is the default email html body template.
	DefaultEmailHTMLBodyTemplate = `` /* 632-byte string literal not displayed */

	// DefaultEmailTextBodyTemplate is the default body template.
	DefaultEmailTextBodyTemplate = `{{ .Var "jobName" }} {{ .Var "status" }}
Elapsed: {{ .Var "elapsed" }}
{{ if .HasVar "err" }}Error: {{ .Var "err" }}{{end}}`
)
View Source
const (
	DefaultMaxLogBytes = 10 * (1 << 10)
)

Constants and Defaults

Variables

This section is empty.

Functions

func AddNotificationClients

func AddNotificationClients(job *Job, cfg *Config) error

AddNotificationClients adds notification clients to a given job.

func Debugf

func Debugf(ctx context.Context, log logger.Log, format string, args ...interface{})

Debugf prints an info message if the logger is set.

func Error

func Error(ctx context.Context, log logger.Log, err error)

Error prints an error if the logger is set.

func Errorf

func Errorf(ctx context.Context, log logger.Log, format string, args ...interface{})

Errorf prints an error message if the logger is set.

func Fatal

func Fatal(ctx context.Context, log logger.Log, err error)

Fatal prints a fatal error if the logger is set.

func Fatalf

func Fatalf(ctx context.Context, log logger.Log, format string, args ...interface{})

Fatalf prints a fatal error message if the logger is set.

func Infof

func Infof(ctx context.Context, log logger.Log, format string, args ...interface{})

Infof prints an info message if the logger is set.

func NewEmailMessage

func NewEmailMessage(ji *cron.JobInvocation, options ...email.MessageOption) (email.Message, error)

NewEmailMessage returns a new email message.

func NewManagementServer

func NewManagementServer(jm *cron.JobManager, cfg *Config) *web.App

NewManagementServer returns a new management server that lets you trigger jobs or look at job statuses via. a json api.

func NewSlackMessage

func NewSlackMessage(ji *cron.JobInvocation, options ...slack.MessageOption) slack.Message

NewSlackMessage returns a new job started message.

func Warning

func Warning(ctx context.Context, log logger.Log, err error)

Warning prints an warning if the logger is set.

func Warningf

func Warningf(ctx context.Context, log logger.Log, format string, args ...interface{})

Warningf prints a warning message if the logger is set.

func WithJobInvocationState

func WithJobInvocationState(ctx context.Context, jis *JobInvocationState) context.Context

WithJobInvocationState sets the job invocation state on a context.

Types

type Config

type Config struct {
	cron.Config `json:",inline" yaml:",inline"`

	MaxLogBytes int `json:"maxLogBytes" yaml:"maxLogBytes"`

	Logger logger.Config `json:"logger" yaml:"logger"`
	Web    web.Config    `json:"web" yaml:"web"`

	Airbrake airbrake.Config `json:"airbrake" yaml:"airbrake"`
	AWS      aws.Config      `json:"aws" yaml:"aws"`
	Email    email.Message   `json:"email" yaml:"email"`
	Datadog  datadog.Config  `json:"datadog" yaml:"datadog"`
	Slack    slack.Config    `json:"slack" yaml:"slack"`
}

Config is the jobkit config.

func (Config) MaxLogBytesOrDefault

func (c Config) MaxLogBytesOrDefault() int

MaxLogBytesOrDefault is a the maximum amount of log data to buffer.

type Job

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

Job is the main job body.

func NewJob

func NewJob(cfg *JobConfig, action func(context.Context) error) (*Job, error)

NewJob returns a new job.

func (Job) Config

func (job Job) Config() *JobConfig

Config returns the job config.

func (*Job) Description

func (job *Job) Description() string

Description returns an optional description for the job.

func (Job) Execute

func (job Job) Execute(ctx context.Context) error

Execute is the job body.

func (Job) Name

func (job Job) Name() string

Name returns the job name.

func (Job) OnBroken

func (job Job) OnBroken(ctx context.Context)

OnBroken is a lifecycle event handler.

func (Job) OnCancellation

func (job Job) OnCancellation(ctx context.Context)

OnCancellation is a lifecycle event handler.

func (Job) OnComplete

func (job Job) OnComplete(ctx context.Context)

OnComplete is a lifecycle event handler.

func (Job) OnDisabled

func (job Job) OnDisabled(ctx context.Context)

OnDisabled is a lifecycle event handler.

func (Job) OnEnabled

func (job Job) OnEnabled(ctx context.Context)

OnEnabled is a lifecycle event handler.

func (Job) OnFailure

func (job Job) OnFailure(ctx context.Context)

OnFailure is a lifecycle event handler.

func (Job) OnFixed

func (job Job) OnFixed(ctx context.Context)

OnFixed is a lifecycle event handler.

func (Job) OnStart

func (job Job) OnStart(ctx context.Context)

OnStart is a lifecycle event handler.

func (Job) Schedule

func (job Job) Schedule() cron.Schedule

Schedule returns the job schedule.

func (Job) Timeout

func (job Job) Timeout() time.Duration

Timeout returns the timeout.

func (*Job) WithConfig

func (job *Job) WithConfig(cfg *JobConfig) *Job

WithConfig sets the config.

func (*Job) WithDescription

func (job *Job) WithDescription(description string) *Job

WithDescription returns the job description.

func (*Job) WithEmailClient

func (job *Job) WithEmailClient(client email.Sender) *Job

WithEmailClient sets the job email client.

func (*Job) WithErrorClient

func (job *Job) WithErrorClient(client diagnostics.Notifier) *Job

WithErrorClient sets the job error client.

func (*Job) WithLogger

func (job *Job) WithLogger(log logger.FullReceiver) *Job

WithLogger sets the job logger.

func (*Job) WithName

func (job *Job) WithName(name string) *Job

WithName sets the name.

func (*Job) WithSchedule

func (job *Job) WithSchedule(schedule cron.Schedule) *Job

WithSchedule sets the schedule.

func (*Job) WithSlackClient

func (job *Job) WithSlackClient(client slack.Sender) *Job

WithSlackClient sets the job slack client.

func (*Job) WithStatsClient

func (job *Job) WithStatsClient(client stats.Collector) *Job

WithStatsClient sets the job stats client.

func (*Job) WithTimeout

func (job *Job) WithTimeout(d time.Duration) *Job

WithTimeout sets the job timeout.

type JobConfig

type JobConfig struct {
	// Name is the name of the job.
	Name string `json:"name" yaml:"name"`
	// Description is a description of the job.
	Description string `json:"description" yaml:"description"`
	// Schedule returns the job schedule.
	Schedule string `json:"schedule" yaml:"schedule"`
	// Timeout represents the abort threshold for the job.
	Timeout time.Duration `json:"timeout" yaml:"timeout"`

	// NotifyOnStart governs if we should send notifications job start.
	NotifyOnStart *bool `json:"notifyOnStart" yaml:"notifyOnStart"`
	// NotifyOnSuccess governs if we should send notifications on any success.
	NotifyOnSuccess *bool `json:"notifyOnSuccess" yaml:"notifyOnSuccess"`
	// NotifyOnFailure governs if we should send notifications on any failure.
	NotifyOnFailure *bool `json:"notifyOnFailure" yaml:"notifyOnFailure"`
	// NotifyOnBroken governs if we should send notifications on a success => failure transition.
	NotifyOnBroken *bool `json:"notifyOnBroken" yaml:"notifyOnBroken"`
	// NotifyOnFixed governs if we should send notifications on a failure => success transition.
	NotifyOnFixed *bool `json:"notifyOnFixed" yaml:"notifyOnFixed"`
	// NotifyOnEnabled governs if we should send notifications when a job is enabled.
	NotifyOnEnabled *bool `json:"notifyOnEnabled" yaml:"notifyOnEnabled"`
	// NotifyOnDisabled governs if we should send notifications when a job is disabled.
	NotifyOnDisabled *bool `json:"notifyOnDisabled" yaml:"notifyOnDisabled"`
}

JobConfig is something you can use to give your jobs some knobs to turn from configuration. You can use this job config by embedding it into your larger job config struct.

func (JobConfig) DescritionOrDefault

func (jc JobConfig) DescritionOrDefault() string

DescritionOrDefault returns the job description if it's set.

func (JobConfig) NameOrDefault

func (jc JobConfig) NameOrDefault() string

NameOrDefault returns the job name if it's set.

func (JobConfig) NotifyOnBrokenOrDefault

func (jc JobConfig) NotifyOnBrokenOrDefault() bool

NotifyOnBrokenOrDefault returns a value or a default.

func (JobConfig) NotifyOnDisabledOrDefault

func (jc JobConfig) NotifyOnDisabledOrDefault() bool

NotifyOnDisabledOrDefault returns a value or a default.

func (JobConfig) NotifyOnEnabledOrDefault

func (jc JobConfig) NotifyOnEnabledOrDefault() bool

NotifyOnEnabledOrDefault returns a value or a default.

func (JobConfig) NotifyOnFailureOrDefault

func (jc JobConfig) NotifyOnFailureOrDefault() bool

NotifyOnFailureOrDefault returns a value or a default.

func (JobConfig) NotifyOnFixedOrDefault

func (jc JobConfig) NotifyOnFixedOrDefault() bool

NotifyOnFixedOrDefault returns a value or a default.

func (JobConfig) NotifyOnStartOrDefault

func (jc JobConfig) NotifyOnStartOrDefault() bool

NotifyOnStartOrDefault returns a value or a default.

func (JobConfig) NotifyOnSuccessOrDefault

func (jc JobConfig) NotifyOnSuccessOrDefault() bool

NotifyOnSuccessOrDefault returns a value or a default.

func (JobConfig) ScheduleOrDefault

func (jc JobConfig) ScheduleOrDefault() string

ScheduleOrDefault returns the schedule or a default (every 5 minutes).

func (JobConfig) TimeoutOrDefault

func (jc JobConfig) TimeoutOrDefault() time.Duration

TimeoutOrDefault the job timeout or a default

type JobInvocationState

type JobInvocationState struct {
	Output      *bytes.Buffer
	ErrorOutput *bytes.Buffer
}

JobInvocationState is the state object for a job invocation.

func GetJobInvocationState

func GetJobInvocationState(ctx context.Context) *JobInvocationState

GetJobInvocationState returns the job invocation state.

func NewJobInvocationState

func NewJobInvocationState() *JobInvocationState

NewJobInvocationState returns a new job invocation state.

Jump to

Keyboard shortcuts

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