postgresql

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package postgresql implements tmanager.Storage interface using Postgresql database.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidDataJSONB = errors.New("invalid data for JSONB type")

ErrInvalidDataJSONB returns when database returns wrong type for JSON field.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Schema with tables in database, postgres current_schema by default.
	Schema string
	// TableName table name with tasks, "tmanager_tasks" by default.
	TableName string
}

Config optional config of the postgresql connection. Feel free to skip any of fields.

type Storage

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

Storage contains methods to manage tasks in database.

func New

func New(conn *sql.DB, config *Config) (*Storage, error)

New creates new schema and task table if not exists. Takes std sql connection and optional (may be nil) config with schema and table name.

func (*Storage) CreateOneActiveTask

func (s *Storage) CreateOneActiveTask(ctx context.Context, task tmanager.Task) error

CreateOneActiveTask creates table constraint to control that DB contains only one active task and creates task with ignoring conflict.

func (*Storage) CreateTasks

func (s *Storage) CreateTasks(ctx context.Context, tasks ...tmanager.Task) error

CreateTasks creates list of the tasks in the DB.

func (*Storage) DeleteCompletedTasks

func (s *Storage) DeleteCompletedTasks(ctx context.Context, olderThen time.Time) error

DeleteCompletedTasks delete completed tasks completed before olderThen param.

func (*Storage) DeleteNamedCompletedTasks added in v0.0.5

func (s *Storage) DeleteNamedCompletedTasks(ctx context.Context, olderThen time.Time, names ...string) error

DeleteNamedCompletedTasks delete completed tasks with special handler names and completed before olderThen param.

func (*Storage) GetFailedTasks

func (s *Storage) GetFailedTasks(ctx context.Context) ([]inspector.Task, error)

GetFailedTasks returns failed tasks. Failed task it is undone task with error. Returns empty slcie when storage can't found failed tasks.

func (*Storage) GetNextTask

func (s *Storage) GetNextTask(ctx context.Context, now time.Time) (tmanager.Task, error)

GetNextTask returns the oldest task that the worker has not yet taken to work. Value of the StartAfter must be less then "now" argument. Returns error ErrTaskNotFound when can't found task.

func (*Storage) GetStuckedTasks

func (s *Storage) GetStuckedTasks(ctx context.Context, inWorkBefore time.Time) ([]inspector.Task, error)

GetStuckedTasks returns stucked tasks. Stuck task it is task without error and taked in work before inWorkBefore. Returns empty slcie when storage can't found stucked tasks.

func (*Storage) RestartTask

func (s *Storage) RestartTask(ctx context.Context, task tmanager.Task, restartAfter time.Time) error

RestartTask makes the task free to re-taking in GetNextTask method. You have to set time to re-taking.

func (*Storage) SaveTaskWithError

func (s *Storage) SaveTaskWithError(ctx context.Context, task tmanager.Task, taskErr error) error

SaveTaskWithError saves task with error. Saved previous error text in the column.

func (*Storage) SaveTaskWithSuccess

func (s *Storage) SaveTaskWithSuccess(ctx context.Context, task tmanager.Task) error

SaveTaskWithSuccess saves success done task.

type Task

type Task struct {
	ID        int64
	CreatedAt time.Time
	UpdatedAt time.Time

	InWorkAt *time.Time // time when the task was taken to work, will be nil in the new task
	DoneAt   *time.Time // time when the task was done
	Error    *string    // error text of the task

	StartAfter time.Time // time after which the task should be started.
	Handler    string    // name of the task handler
	Data       jsonb     // data of the handler
}

Task is a database model of the task.

func (*Task) GetData

func (t *Task) GetData() json.RawMessage

GetData returns data of the handler.

func (*Task) GetError

func (t *Task) GetError() string

GetError returns saved error of the task execution.

func (*Task) GetHandler

func (t *Task) GetHandler() string

GetHandler returns name of the task handler.

func (*Task) GetID

func (t *Task) GetID() string

GetID returns unique identification of the task.

func (*Task) GetStartAfter

func (t *Task) GetStartAfter() time.Time

GetStartAfter returns time after which the task should be started.

func (*Task) String

func (t *Task) String() string

String returns json representation of the task model.

Jump to

Keyboard shortcuts

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