Documentation
¶
Overview ¶
Package karajo implement HTTP workers and manager similar to cron but works only on HTTP.
karajo has the web user interface (WUI) for monitoring the jobs that run on port 31937 by default and can be configurable.
A single instance of karajo is configured through code or configuration file using ini file format.
For more information see the README file in this repository.
Index ¶
- Constants
- Variables
- func Sign(payload, secret []byte) (sign string)
- type Client
- func (cl *Client) Env() (env *Env, err error)
- func (cl *Client) JobExecCancel(id string) (job *JobExec, err error)
- func (cl *Client) JobExecLog(jobID string, counter int) (joblog *JobLog, err error)
- func (cl *Client) JobExecPause(id string) (job *JobExec, err error)
- func (cl *Client) JobExecResume(id string) (job *JobExec, err error)
- func (cl *Client) JobExecRun(jobPath string) (job *JobExec, err error)
- func (cl *Client) JobHTTP(id string) (httpJob *JobHTTP, err error)
- func (cl *Client) JobHTTPLog(id string, counter int) (jlog *JobLog, err error)
- func (cl *Client) JobHTTPPause(id string) (jobHTTP *JobHTTP, err error)
- func (cl *Client) JobHTTPResume(id string) (jobHTTP *JobHTTP, err error)
- type ClientOptions
- type Env
- type EnvNotif
- type JobBase
- type JobExec
- type JobExecHTTPHandler
- type JobHTTP
- type JobHTTPRequest
- type JobLog
- type Karajo
- type User
Constants ¶
const ( JobStatusCanceled = `canceled` JobStatusFailed = `failed` JobStatusPaused = `paused` JobStatusRunning = `running` JobStatusStarted = `started` JobStatusSuccess = `success` )
List of [JobBase.Status]. The job status have the following cycle,
started --> running -+-> success ---+ +--> paused --> started | | | +-> canceled --+--+ | | | +-> failed ---+ +--> running
const ( JobAuthKindGithub = `github` JobAuthKindHmacSha256 = `hmac-sha256` // Default AuthKind if not set. JobAuthKindSourcehut = `sourcehut` )
List of [JobExec.AuthKind] for authorization.
const HeaderNameXKarajoSign = `X-Karajo-Sign`
HeaderNameXKarajoSign the header key for the signature of body.
Variables ¶
var Version = `0.9.3`
Version of this library and program.
Functions ¶
Types ¶
type Client ¶ added in v0.6.0
Client HTTP client for Karajo server.
func NewClient ¶ added in v0.6.0
func NewClient(opts ClientOptions) (cl *Client)
NewClient create new HTTP client.
func (*Client) JobExecCancel ¶ added in v0.9.0
JobExecCancel cancel the running JobExec by its ID.
func (*Client) JobExecLog ¶ added in v0.9.0
JobExecLog get the JobExec log by its ID and counter.
func (*Client) JobExecPause ¶ added in v0.9.0
JobExecPause pause the JobExec by its ID.
func (*Client) JobExecResume ¶ added in v0.9.0
JobExecResume resume the JobExec execution by its ID.
func (*Client) JobExecRun ¶ added in v0.9.0
JobExecRun trigger the JobExec by its path.
func (*Client) JobHTTPLog ¶ added in v0.9.0
JobHTTPLog get the job logs by its ID.
func (*Client) JobHTTPPause ¶ added in v0.9.0
JobHTTPPause pause the HTTP job by its ID.
type ClientOptions ¶ added in v0.6.0
type ClientOptions struct { // Secret for API that require authorization, for example to pause or // resume a job. Secret string libhttp.ClientOptions }
ClientOptions define the options for Karajo HTTP client.
type Env ¶ added in v0.8.0
type Env struct { // List of JobExec by name. ExecJobs map[string]*JobExec `ini:"job" json:"jobs"` // List of JobHTTP by name. HTTPJobs map[string]*JobHTTP `ini:"job.http" json:"http_jobs"` // Notif contains list of notification setting. Notif map[string]EnvNotif `ini:"notif" json:"-"` // Users list of user that can access web user interface. // The list of user optionally loaded from // $DirBase/etc/karajo/user.conf if the file exist. Users map[string]*User `json:"-"` // Name of the service. // The Name will be used for title on the web user interface, as log // prefix, as file prefix on the jobs state, and as file prefix on // log files. // If this value is empty, it will be set to "karajo". Name string `ini:"karajo::name" json:"name"` // Define the address for WUI, default to ":31937". ListenAddress string `ini:"karajo::listen_address" json:"listen_address"` // DirBase define the base directory where configuration, job state, // and job log stored. // This field is optional, default to current directory. // The structure of directory follow the common UNIX system, // // $DirBase // | // +-- /etc/karajo/ +-- karajo.conf // | +-- job.d/ // | +-- job_http.d/ // | // +-- /var/lib/karajo/ +-- job/$JobExec.ID // | +-- job_http/$JobHTTP.ID // | // +-- /var/log/karajo/ +-- job/$JobExec.ID // | +-- job_http/$JobHTTP.ID // | // +-- /var/run/karajo/job_http/$JobHTTP.ID // // Each job log stored under directory /var/log/karajo/job and the job // state under directory /var/run/karajo/job. DirBase string `ini:"karajo::dir_base" json:"dir_base"` // DirPublic define a path to serve to public. // While the WUI is served under "/karajo", a directory DirPublic // will be served under "/". // A DirPublic can contains sub directory as long as its name is not // "karajo". DirPublic string `ini:"karajo::dir_public" json:"dir_public"` Version string `json:"version"` // Secret define the default secret to authorize the incoming HTTP // request. // The signature is generated from HTTP payload (query or body) with // HMAC+SHA-256. // The signature is read from HTTP header "X-Karajo-Sign" as hex // string. // This field is optional, if its empty the new secret will be // generated and printed to standard output on each run. Secret string `ini:"karajo::secret" json:"-"` // HTTPTimeout define the global HTTP client timeout when executing // each jobs. // This field is optional, default to 5 minutes. // The value of this option is using the Go [time.Duration] // format, for example, "30s" for 30 seconds, "1m" for 1 minute. HTTPTimeout time.Duration `ini:"karajo::http_timeout" json:"http_timeout"` // MaxJobRunning define the maximum job running at the same time. // This field is optional default to 1. MaxJobRunning int `ini:"karajo::max_job_running" json:"max_job_running"` // IsDevelopment if its true, the files in DirPublic will be loaded // directly from disk instead from embedded memfs. IsDevelopment bool `ini:"karajo::is_development" json:"is_development"` // contains filtered or unexported fields }
Env contains configuration for HTTP server, logs, and list of jobs.
type EnvNotif ¶ added in v0.8.0
type EnvNotif struct { Name string Kind string `ini:"::kind"` SMTPServer string `ini:"::smtp_server"` SMTPUser string `ini:"::smtp_user"` SMTPPassword string `ini:"::smtp_password"` From string `ini:"::from"` To []string `ini:"::to"` SMTPInsecure bool `ini:"::smtp_insecure"` }
EnvNotif environment for notification.
type JobBase ¶ added in v0.6.0
type JobBase struct { // The last time the job is finished running, in UTC. LastRun time.Time `ini:"-" json:"last_run,omitempty"` // The next time the job will running, in UTC. NextRun time.Time `ini:"-" json:"next_run,omitempty"` // ID of the job. // It must be unique, otherwise when jobs loaded, the last job will // replace the previous job with the same ID. // If ID is empty, it will generated from Name by replacing // non-alphanumeric character with '-'. ID string `ini:"-" json:"id"` // Name of job for human. Name string `ini:"-" json:"name"` // Description of the Job. // It could contains simple HTML tags. Description string `ini:"::description" json:"description,omitempty"` // Status of the job on last execution. Status string `ini:"-" json:"status,omitempty"` // Schedule a timer that run periodically based on calendar or day // time. // A schedule is divided into monthly, weekly, daily, hourly, and // minutely. // See [time.Scheduler] for format of schedule. // // If both Schedule and Interval set, only Schedule will be processed. // // [time.Scheduler]: https://pkg.go.dev/git.sr.ht/~shulhan/pakakeh.go/lib/time#Scheduler Schedule string `ini:"::schedule" json:"schedule,omitempty"` // NotifOnSuccess define list of notification where the job's log will // be send when job execution finish successfully. NotifOnSuccess []string `ini:"::notif_on_success" json:"notif_on_success,omitempty"` // NotifOnFailed define list of notification where the job's log will // be send when job execution failed. NotifOnFailed []string `ini:"::notif_on_failed" json:"notif_on_failed,omitempty"` // Logs contains cache of log sorted by its counter. Logs []*JobLog `json:"logs,omitempty"` // Interval duration when job will be repeatedly executed. // This field is optional, the minimum value is one minute. // // If both Schedule and Interval set, only Schedule will be processed. Interval time.Duration `ini:"::interval" json:"interval,omitempty"` // LogRetention define the maximum number of logs to keep in storage. // This field is optional, default to 5. LogRetention int `ini:"::log_retention" json:"log_retention,omitempty"` sync.Mutex // contains filtered or unexported fields }
JobBase define the base fields and commons methods for all job types.
The base configuration in INI format,
[job "name"] description = schedule = interval = log_retention = notif_on_success = notif_on_failed =
type JobExec ¶ added in v0.8.0
type JobExec struct { // Call define a function or method to be called, as an // alternative to Commands. // This field is optional, it is only used if JobExec created // through code. Call JobExecHTTPHandler `ini:"-" json:"-"` // HTTP path where JobExec can be triggered using HTTP. // The Path is automatically prefixed with "/karajo/api/job_exec/run", it // is not static. // For example, if it set to "/my", then the actual path would be // "/karajo/api/job_exec/run/my". // This field is optional and unique between JobExec. Path string `ini:"::path" json:"path,omitempty"` // Supported AuthKind are, // // - github: the signature read from "x-hub-signature-256" and // compare it by signing request body with Secret using // HMAC-SHA256. // If the header is empty, it will check another header // "x-hub-signature" and then sign the request body with Secret // using HMAC-SHA1. // // - hmac-sha256: the signature read from HeaderSign and compare it // by signing request body with Secret using HMAC-SHA256. // // - sourcehut: See https://man.sr.ht/api-conventions.md#webhooks // // If this field is empty or invalid it will be set to hmac-sha256. AuthKind string `ini:"::auth_kind" json:"auth_kind,omitempty"` // HeaderSign define the HTTP header where the signature is read. // Default to "X-Karajo-Sign" if its empty. HeaderSign string `ini:"::header_sign" json:"header_sign,omitempty"` // Secret define a string to validate the signature of request. // If its empty, it will be set to global Secret from Env. Secret string `ini:"::secret" json:"-"` // Commands list of command to be executed. // This option can be defined multiple times. // The following environment variables are available inside the // command: // // - KARAJO_JOB_COUNTER: contains the current job counter. Commands []string `ini:"::command" json:"commands,omitempty"` JobBase // contains filtered or unexported fields }
JobExec define a job to execute Go code or list of commands. A JobExec can be triggered manually by sending HTTP POST request or automatically by timer (per interval or schedule).
For job triggered by HTTP request, the Path and Secret must be set. For job triggered by timer, the Interval or Schedule must not be empty. See the JobBase's Interval and Schedule fields for more information.
Each JobExec have its own working directory, a callback or list of commands to be executed.
The JobExec configuration in INI format,
[job "name"] path = auth_kind = header_sign = secret = command =
type JobExecHTTPHandler ¶ added in v0.9.0
type JobExecHTTPHandler func(ctx context.Context, log io.Writer, epr *libhttp.EndpointRequest) error
JobExecHTTPHandler define an handler for triggering a JobExec using HTTP.
The log parameter is used to log all output and error. The epr parameter contains HTTP request, body, and response writer.
type JobHTTP ¶ added in v0.9.0
type JobHTTP struct { // Secret define a string to sign the request query or body with // HMAC+SHA-256. // The signature is sent on HTTP header "X-Karajo-Sign" as hex string. // This field is optional. Secret string `ini:"::secret" json:"-"` // HeaderSign define the HTTP header where the signature will be // written in request. // Default to "X-Karajo-Sign" if its empty. HeaderSign string `ini:"::header_sign" json:"header_sign,omitempty"` // HTTPMethod HTTP method to be used in request for job execution. // Its accept only GET, POST, PUT, or DELETE. // This field is optional, default to GET. HTTPMethod string `ini:"::http_method" json:"http_method"` // The HTTP URL where the job will be executed. // This field is required. HTTPURL string `ini:"::http_url" json:"http_url"` // HTTPRequestType The header Content-Type to be set on request. // // - (empty string): no header Content-Type set. // - query: no header Content-Type to be set, reserved for future // use. // - form: header Content-Type set to // "application/x-www-form-urlencoded". // - json: header Content-Type set to "application/json". // // The type "form" and "json" only applicable if the HTTPMethod is // POST or PUT. // This field is optional, default to query. HTTPRequestType string `ini:"::http_request_type" json:"http_request_type"` // Optional HTTP headers for HTTPURL, in the format of "K: V". HTTPHeaders []string `ini:"::http_header" json:"http_headers,omitempty"` JobBase // HTTPTimeout custom HTTP timeout for this job. // This field is optional, if not set default to global timeout in // Env.HTTPTimeout. // To make job run without timeout, set the value to negative. HTTPTimeout time.Duration `ini:"::http_timeout" json:"http_timeout"` // HTTPInsecure can be set to true if the http_url is HTTPS with // unknown Certificate Authority. HTTPInsecure bool `ini:"::http_insecure" json:"http_insecure,omitempty"` // contains filtered or unexported fields }
JobHTTP A JobHTTP is a periodic job that send HTTP request to external HTTP server (or to karajo Job itself).
See the JobBase's Interval and Schedule fields for more information on how to setup periodic time.
Each JobHTTP execution send the parameter named "_karajo_epoch" with value set to current server Unix timestamp. If the request type is "query" then the parameter is inside the query URL. If the request type is "form" then the parameter is inside the body. If the request type is "json" then the parameter is inside the body as JSON object, for example '{"_karajo_epoch":1656750073}'.
The job configuration in INI format,
[job "name"] secret = header_sign = http_method = http_url = http_request_type = http_header = http_timeout = http_insecure =
type JobHTTPRequest ¶ added in v0.9.0
type JobHTTPRequest struct { ID string `json:"id" form:"id"` Epoch int64 `json:"_karajo_epoch" form:"_karajo_epoch"` }
JobHTTPRequest define the base request for managing Job or JobHTTP using HTTP POST with JSON body.
type JobLog ¶ added in v0.6.0
type JobLog struct { JobID string `json:"job_id"` Name string `json:"name"` Status string `json:"status,omitempty"` Content []byte `json:"content,omitempty"` // Only used to transfrom from/to JSON. Counter int64 `json:"counter,omitempty"` sync.Mutex // contains filtered or unexported fields }
JobLog contains the content, status, and counter for job's log.
Each log file name is using the following format:
<job.ID>.<counter>.<status>
Counter is a number that unique between log, start from 1.
Status can be success or fail. If status is missing its considered fail.
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
cmd
|
|
karajo
Program karajo implements HTTP workers and manager, similar to cron but works and manageable with HTTP.
|
Program karajo implements HTTP workers and manager, similar to cron but works and manageable with HTTP. |
Package internal provides function for development, including converting ".adoc" files to HTML, embedding assets into Go code, and watching files for previewing documentation.
|
Package internal provides function for development, including converting ".adoc" files to HTML, embedding assets into Go code, and watching files for previewing documentation. |
cmd/karajo-build
Package karajo-build provide internal commands to build karajo for development.
|
Package karajo-build provide internal commands to build karajo for development. |
cmd/karajo-example
Program karajo-example example creating Karajo jobs by code.
|
Program karajo-example example creating Karajo jobs by code. |