job

package
v0.0.0-...-d723c4e Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const CommandWaitDelay = 10 * time.Second

CommandWaitDelay is the amount of time to wait for a canceled Job to shut down before sending a SIGKILL

Variables

View Source
var ErrJobNotFound = fmt.Errorf("job not found")
View Source
var ErrOutputStreamerClosed = errors.New("output streamer is closed")

Functions

This section is empty.

Types

type Job

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

func StartNewJob

func StartNewJob(shutdownCtx context.Context, cgroupFD int, name string, args ...string) (*Job, error)

StartNewJob creates a new job, starts it, and returns a reference to it If the underlying cmd.Start() call fails, an error is returned as well as a nil pointer to ensure that the job is thrown away. This ensures that callers cannot call exported methods on jobs that cannot be started.

func (*Job) OutputStream

func (j *Job) OutputStream(ctx context.Context) <-chan []byte

OutputStream returns a channel that streams the output of the job

func (*Job) Status

func (j *Job) Status() jogv1.Status

Status returns the current status of the job

func (*Job) Stop

func (j *Job) Stop()

Stop calls the cancel function on the exec.Cmd internal context. Jobs are stopped asynchronously, and will be sent a SIGKILL after the CommandWaitDelay has passed.

func (*Job) Wait

func (j *Job) Wait()

Wait blocks until the job is done

type Manager

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

Manager is a job manager that keeps track of jobs by username and jobID. It also holds a context that the server uses to stop all jobs when during shut down

func NewManager

func NewManager(shutdownCtx context.Context) *Manager

NewManager creates a new Manager

func (*Manager) OutputStream

func (m *Manager) OutputStream(ctx context.Context, username string, jobID string) (<-chan []byte, error)

func (*Manager) Start

func (m *Manager) Start(ctx context.Context, username string, cmd string, args ...string) (string, error)

Start starts a new job and returns the jobID

func (*Manager) Status

func (m *Manager) Status(ctx context.Context, username string, jobID string) (jogv1.Status, error)

Status gets the status of a job Because stop signals are eventually respected, the internal state of a job process may not yet be reflected in the status. Eventually consistency is guaranteed, though, and delays mostly depend on the CommandWaitDelay constant in the job package.

func (*Manager) Stop

func (m *Manager) Stop(ctx context.Context, username string, jobID string) error

Stop sends a stop signal to a job that will eventually be respected

type OutputStreamer

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

A OutputStreamer is an io.Writer that collects data written to it and fans it out to clients who want to read that data as a stream. Callers of NewStream() are provided a channel that will receive all data written since the streamer was created.

When the context passed to NewStream() is canceled, the channel will be closed immediately without writing any further data.

OutputStreamer also implements the io.Closer interface. Closing an OutputStreamer means that we don't expect any more data to be written to it. After an OutputStreamer instance is closed, any calls to Write() will return an error. And channels returned from NewStream() will be closed after all data has been written to them.

func NewOutputStreamer

func NewOutputStreamer(options ...OutputStreamerOption) *OutputStreamer

func (*OutputStreamer) CloseWriter

func (o *OutputStreamer) CloseWriter()

func (*OutputStreamer) NewStream

func (o *OutputStreamer) NewStream(ctx context.Context) <-chan []byte

NewStream returns a channel that will receive all data written to the OutputStreamer. When a job is running and writing data to the OutputStreamer, the channel will receive data in chunks of, at most, streamMessageSize bytes.

The reader is configured to check for new data at least once per second. When there is new data, it catches up to the end of stream without waiting.

When the job exits, the OutputStreamer is closed to writes, but the data remains available to NewStream() callers until the server is shutdown.

func (*OutputStreamer) Next

func (o *OutputStreamer) Next(index int) []byte

Next returns the next chunk of data to be read from the OutputStreamer. Note: no copies of the data are made, so the caller should not modify the returned slice. This design enables large output buffers to be read by many clients without incurring the cost of copying the data.

func (*OutputStreamer) Write

func (o *OutputStreamer) Write(b []byte) (int, error)

Write appends data to the internal buffer. This implements the io.Writer interface, making an instance of OutputStreamer usable as the STDOUT and STDERR fields in an exec.Cmd.

type OutputStreamerOption

type OutputStreamerOption func(*OutputStreamer)

func WithStreamMessageSize

func WithStreamMessageSize(size int) OutputStreamerOption

Jump to

Keyboard shortcuts

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