job_dispatcher

package
v0.0.0-...-c93f736 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNo20XStatus = errors.New("no 20X status")

ErrNo20XStatus is returned if there is no 200 status code

View Source
var ErrNoJobSet = errors.New("no current job set")
View Source
var (
	// ErrNoNewJobs is the error returned when there are no new jobs
	ErrNoNewJobs = errors.New("can not find a job")
)
View Source
var ErrPayloadHasNoName = errors.New("payload has no name")

ErrPayloadHasNoName is returned if the payload has no name

View Source
var ErrWorkerInstanceNotSet = errors.New("worker instance not set")

ErrWorkerInstanceNotSet is returned if the worker instance is not set

Functions

This section is empty.

Types

type Client

type Client struct {
	Endpoint       string     // endpoint of the job dispatcher
	UUID           uuid.UUID  // uuid of the client
	WorkerInstance string     // worker instance
	CurrentJob     *Job       // current job
	Bulk           bool       // use bulk endpoint
	Logger         *log.Entry // logger
}

Client is the client for the job dispatcher

func NewClient

func NewClient(endpoint string, workerInstance string, uuid uuid.UUID) Client

NewClient inits a new client

func (*Client) CreateJob

func (c *Client) CreateJob(newJob NewJobDTO) (job *Job, err error)

CreateJob creates a single job

func (*Client) CreateJobs

func (c *Client) CreateJobs(newJobs []NewJobDTO) (results []*Job, err error)

CreateJobs creates multiple job at once

func (*Client) GetEndpoint

func (c *Client) GetEndpoint() string

func (*Client) GetJob

func (c *Client) GetJob(additionalInstances []string) (err error)

GetJob gets the latest job with the highest priority

func (*Client) HeartBeat

func (c *Client) HeartBeat(status map[string]interface{}) (err error)

HeartBeat sends the heartbeat to the backend

func (*Client) MarkCurrentJobAsCompleted

func (c *Client) MarkCurrentJobAsCompleted() (err error)

MarkCurrentJobAsCompleted marks the current job as completed

func (*Client) ReleaseCurrentJob

func (c *Client) ReleaseCurrentJob() (err error)

ReleaseCurrentJob releases the current job

func (*Client) StartCurrentJob

func (c *Client) StartCurrentJob() (err error)

StartCurrentJob marks the current job as started

func (*Client) UseBulk

func (c *Client) UseBulk() *Client

UseBulk uses the bulk endpoint

func (*Client) UseDefault

func (c *Client) UseDefault() *Client

UseDefault uses the default endpoint

type Job

type Job struct {
	UUID uuid.UUID `gorm:"type:varchar(36); primaryKey" json:"uuid"`
	// Metadata
	CreatedAt time.Time  `json:"createdAt"`
	UpdatedAt time.Time  `json:"updatedAt"`
	DeletedAt *time.Time `gorm:"index" json:"deletedAt"`
	// UUIDs
	MandateUID uuid.UUID `gorm:"index" gorm:"type:varchar(36);" json:"mandateUID"`
	ClientUID  uuid.UUID `gorm:"index" gorm:"type:varchar(36);" json:"clientUID"`
	OwnerUID   uuid.UUID `gorm:"index" gorm:"type:varchar(36);" json:"ownerUID"`
	// Attributes
	StartedAt         *time.Time             `gorm:"index" json:"startedAt"`                                              // the timestamp when the worker started on this job
	CompletedAt       *time.Time             `gorm:"index" json:"completedAt"`                                            // the timestamp when the worker finished the job
	LastHeartBeat     *time.Time             `gorm:"index" json:"lastHeartBeat"`                                          // the last time the worker was working on this job
	Name              string                 `json:"name" gorm:"type:varchar(100);"`                                      // a short and optional name
	Priority          uint                   `gorm:"index" json:"priority" gorm:"default:1;"`                             // the priority of the job (1 lowest priority, ... 10 high priority)
	Attempts          uint                   `gorm:"index" json:"attempts" gorm:"default:0;"`                             // the attempts that have been tried in the past
	CurrentWorkerUID  *uuid.UUID             `gorm:"index" json:"currentWorkerUID" gorm:"type:varchar(36); default:null"` // the uuid of the the worker bot that is currently working in this job
	WorkerInstance    string                 `gorm:"index" json:"workerInstance" gorm:"index:worker_instance;"`           // the type of worker that should work on this job
	Parameters        map[string]string      `json:"parameters" gorm:"-"`                                                 // the search parameters the worker should use (e.g. company names ... )
	ParametersJSON    datatypes.JSON         `json:"-" gorm:"type:jsonb;"`                                                // json
	Tasks             []JobTask              `json:"tasks" gorm:"-"`                                                      // the way the worker should interact (e.g. click on that, extract this, store it in this bucket, ...)
	TasksJSON         datatypes.JSON         `json:"-" gorm:"type:jsonb;"`                                                // json
	CurrentStatus     map[string]interface{} `json:"currentStatus" gorm:"-"`                                              // the current status of the job
	CurrentStatusJSON datatypes.JSON         `json:"-" gorm:"type:jsonb;"`                                                // json
}

func (*Job) String

func (wj *Job) String() string

type JobService

type JobService interface {
	New(*Job) error
	BulkNew([]*Job) error
	GetJobByUUID(uuid uuid.UUID) (*Job, error)
	Start(*Job) error
	HeartBeat(*Job, map[string]interface{}) error
	Release(*Job) error
	Complete(*Job) error
	GetLatestJob(workerInstances []string, workerUUID uuid.UUID) (*Job, error)
	GetCurrentJobOfWorker(workerInstances []string, workerUUID uuid.UUID) (*Job, bool, error)
	Clean() error
	GetStats() ([]Stats, error)
}

type JobTask

type JobTask struct {
	Version string      `json:"version"`
	Name    string      `json:"name"`
	Type    string      `json:"type"`
	Execute interface{} `json:"execute"`
}

JobTask is a task the worker should perform

type NewJobDTO

type NewJobDTO struct {
	Name           string            `json:"name"`
	WorkerInstance string            `json:"workerInstance"`
	Priority       uint              `json:"priority"`
	Params         map[string]string `json:"params"`
	Tasks          []JobTask         `json:"tasks"`
}

NewJobDTO is the dto for creating a new job

func (*NewJobDTO) GenerateJob

func (dto *NewJobDTO) GenerateJob() *Job

GenerateJob transforms a dto into a job metadata is added new uuid is generated

func (*NewJobDTO) Validate

func (dto *NewJobDTO) Validate() (err error)

Validate checks if a job is valid

type ResponseDTO

type ResponseDTO struct {
	Status string `json:"status"`
	Job    *Job   `json:"job"`
}

ResponseDTO is the response for a single job

type ResponseMultipleDTO

type ResponseMultipleDTO struct {
	Status string `json:"status"`
	Jobs   []*Job `json:"jobs"`
}

ResponseMultipleDTO is the response for multiple jobs

type Stats

type Stats struct {
	WorkerInstance string
	Total          int
	Todo           int
	Done           int
	Active         int
}

Jump to

Keyboard shortcuts

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