models

package
v0.0.0-...-6ed339e Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2015 License: MPL-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultMaxInFlight is the maximum number of tasks executed in parallel.
	DefaultMaxInFlight = 10
)

Variables

View Source
var (
	// ErrDeleteDefaultApplication is returned when trying to delete the default application.
	ErrDeleteDefaultApplication = errors.New("can not delete default application")
	// ErrApplicationNotFound is returned when the application does not exist.
	ErrApplicationNotFound = errors.New("application does not exist")
)
View Source
var (
	// ModelsBaseDebug ...
	ModelsBaseDebug = debug.Debug("hooky.models.base")

	ErrDatabase = fmt.Errorf("Database Error")
)
View Source
var (
	// ErrDeleteDefaultQueue is returned when trying to delete the default queue.
	ErrDeleteDefaultQueue = errors.New("can not delete default queue")
	// ErrQueueNotFound is returned when the queue does not exist.
	ErrQueueNotFound = errors.New("queue does not exist")
)
View Source
var AttemptStatuses = map[string]bool{
	"pending": true,
	"running": true,
	"success": true,
	"error":   true,
}

AttemptStatuses

View Source
var (
	// ErrMaxAttemptsExceeded indicates that the maximum retries has been
	// excedeed. Usually to consider a service unreachable/unavailable.
	ErrMaxAttemptsExceeded = errors.New("maximum of attempts exceeded")
)
View Source
var (

	// ModelsAttemptDebug ...
	ModelsAttemptDebug = debug.Debug("hooky.models.attempt")
)
View Source
var (
	// ModelsTaskDebug ...
	ModelsTaskDebug = debug.Debug("hooky.models.task")
)
View Source
var TaskStatuses = map[string]bool{
	"pending":  true,
	"retrying": true,
	"canceled": true,
	"success":  true,
	"error":    true,
}

TaskStatuses are the differents statuses that a Task can have.

Functions

This section is empty.

Types

type Account

type Account struct {
	// ID is the ID of the Account.
	ID bson.ObjectId `bson:"_id"`

	// Name is display name for the Account.
	Name *string `bson:"name,omitempty"`

	// Key is the secret key to authenticate the Account ID.
	Key string `bson:"key"`

	// Deleted
	Deleted bool `bson:"deleted"`
}

Account is an account to access the service.

type Application

type Application struct {
	// ID is the ID of the Application.
	ID bson.ObjectId `bson:"_id"`

	// Account is the ID of the Account owning the Application.
	Account bson.ObjectId `bson:"account"`

	// Name is the Application's name.
	Name string `bson:"name"`

	// Deleted
	Deleted bool `bson:"deleted"`
}

Application is a list of recurring Tasks.

type Attempt

type Attempt struct {
	// ID is the ID of the attempt.
	ID bson.ObjectId `bson:"_id"`

	// Account is the ID of the Account owning the Task.
	Account bson.ObjectId `bson:"account"`

	// Application is the name of the parent Application.
	Application string `bson:"application"`

	// Task is the task's name.
	Task string `bson:"task"`

	// TaskID is the ID of the parent Task of this attempt.
	TaskID bson.ObjectId `bson:"task_id"`

	// Queue is the name of the parent Queue.
	Queue string `bson:"queue"`

	// QueueID is the ID of the parent Queue
	QueueID bson.ObjectId `bson:"queue_id"`

	// URL is the URL that the worker with requests.
	URL string `bson:"url"`

	// HTTPAuth is the HTTP authentication to use if any.
	HTTPAuth HTTPAuth `json:"auth"`

	// Method is the HTTP method that will be used to execute the request.
	Method string `bson:"method"`

	// Headers are the HTTP headers that will be used when executing the request.
	Headers map[string]string `bson:"headers"`

	// Payload is a arbitrary data that will be POSTed on the URL.
	Payload string `bson:"payload"`

	// Reserved is a Unix timestamp until when the attempt is reserved by a worker.
	Reserved int64 `bson:"reserved"`

	// At is a Unix timestamp representing the time a request must be performed.
	At int64 `bson:"at"`

	// Finished is a Unix timestamp representing the time the attempt finished.
	Finished int64 `bson:"finished,omitempty"`

	// Status is either `pending`, `running`, `success` or `error`
	Status string `bson:"status"`

	// StatusCode is the HTTP status code.
	StatusCode int32 `bson:"status_code,omitempty"`

	// StatusMessage is a human readable message related to the Status.
	StatusMessage string `bson:"status_message,omitempty"`

	// Acked
	Acked bool `bson:"acked"`

	// Deleted
	Deleted bool `bson:"deleted"`
}

Attempt describes a HTTP request that must be perform for a task.

type Base

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

func NewBase

func NewBase(db *mgo.Database) *Base

func (*Base) AckAttempt

func (b *Base) AckAttempt(attemptID bson.ObjectId) (err error)

AckAttempt marks the attempt as acknowledged.

func (*Base) AuthenticateAccount

func (b *Base) AuthenticateAccount(account bson.ObjectId, key string) (bool, error)

AuthenticateAccount authenticates an Account.

func (*Base) Bootstrap

func (b *Base) Bootstrap() error

func (*Base) CleanDeletedRessources

func (b *Base) CleanDeletedRessources() error

CleanDeletedRessources cleans ressources that have been deleted.

func (*Base) CleanFinishedAttempts

func (b *Base) CleanFinishedAttempts(seconds int64) (deleted int, err error)

CleanFinishedAttempts cleans attempts that are finished since more than X seconds.

func (*Base) DeQueue

func (b *Base) DeQueue(queueID bson.ObjectId, attemptID bson.ObjectId) (err error)

DeQueue increases the available_in_flight by one.

func (*Base) DeleteAccount

func (b *Base) DeleteAccount(account bson.ObjectId) (err error)

DeleteAccount deletes an Account given its ID.

func (*Base) DeleteApplication

func (b *Base) DeleteApplication(account bson.ObjectId, name string) (err error)

DeleteApplication deletes an Application and all its children.

func (*Base) DeleteApplications

func (b *Base) DeleteApplications(account bson.ObjectId) (err error)

DeleteApplications deletes all Applications owns by an Account.

func (*Base) DeletePendingAttempts

func (b *Base) DeletePendingAttempts(taskID bson.ObjectId) (bool, error)

DeletePendingAttempts deletes all pending Attempts for a given Task ID.

func (*Base) DeleteQueue

func (b *Base) DeleteQueue(account bson.ObjectId, application string, name string) (err error)

DeleteQueue deletes an Queue and all its children.

func (*Base) DeleteQueues

func (b *Base) DeleteQueues(account bson.ObjectId, application string) (err error)

DeleteQueues deletes all Queues owns by an Account.

func (*Base) DeleteTask

func (b *Base) DeleteTask(account bson.ObjectId, application string, name string) (err error)

DeleteTask deletes a Task.

func (*Base) DeleteTasks

func (b *Base) DeleteTasks(account bson.ObjectId, application string) (err error)

DeleteTasks deletes all Tasks from an Application.

func (*Base) DoAttempt

func (b *Base) DoAttempt(attempt *Attempt) error

DoAttempt executes the attempt.

func (*Base) EnQueue

func (b *Base) EnQueue(queueID bson.ObjectId, attemptID bson.ObjectId) (full bool, err error)

EnQueue checks if a queue reached its max_in_flight.

func (*Base) EnsureApplicationIndex

func (b *Base) EnsureApplicationIndex() (err error)

EnsureApplicationIndex creates mongo indexes for Application.

func (*Base) EnsureAttemptIndex

func (b *Base) EnsureAttemptIndex() (err error)

EnsureAttemptIndex creates mongo indexes for Application.

func (*Base) EnsureQueueIndex

func (b *Base) EnsureQueueIndex() (err error)

EnsureQueueIndex creates mongo indexes for Queue.

func (*Base) EnsureTaskIndex

func (b *Base) EnsureTaskIndex() (err error)

EnsureTaskIndex creates mongo indexes for Application.

func (*Base) FixIntegrity

func (b *Base) FixIntegrity() error

FixIntegrity ...

func (*Base) FixQueues

func (b *Base) FixQueues() (err error)

FixQueues fixes unconsistencies with available_in_flight.

func (*Base) ForceAttemptForTask

func (b *Base) ForceAttemptForTask(account bson.ObjectId, application string, name string) (attempt *Attempt, err error)

ForceAttemptForTask ...

func (*Base) GetAccount

func (b *Base) GetAccount(accountID bson.ObjectId) (account *Account, err error)

GetAccount returns an Account given its ID.

func (*Base) GetAccounts

func (b *Base) GetAccounts(lp ListParams, lr *ListResult) (err error)

GetAccounts returns a list of Accounts.

func (*Base) GetApplication

func (b *Base) GetApplication(account bson.ObjectId, name string) (application *Application, err error)

GetApplication returns an Application.

func (*Base) GetApplications

func (b *Base) GetApplications(account bson.ObjectId, lp ListParams, lr *ListResult) (err error)

GetApplications returns a list of Applications.

func (*Base) GetAttempt

func (b *Base) GetAttempt(attemptID bson.ObjectId) (*Attempt, error)

GetAttempt returns an Attempt.

func (*Base) GetAttemptByID

func (b *Base) GetAttemptByID(attemptID bson.ObjectId) (attempt *Attempt, err error)

GetAttemptByID returns a Attempt given its ID.

func (*Base) GetAttempts

func (b *Base) GetAttempts(account bson.ObjectId, application string, task string, lp ListParams, lr *ListResult) (err error)

GetAttempts returns a list of Attempts.

func (*Base) GetQueue

func (b *Base) GetQueue(account bson.ObjectId, application string, name string) (queue *Queue, err error)

GetQueue returns a Queue.

func (*Base) GetQueues

func (b *Base) GetQueues(account bson.ObjectId, application string, lp ListParams, lr *ListResult) (err error)

GetQueues returns a list of Queues.

func (*Base) GetTask

func (b *Base) GetTask(account bson.ObjectId, application string, name string) (task *Task, err error)

GetTask returns a Task.

func (*Base) GetTaskByID

func (b *Base) GetTaskByID(taskID bson.ObjectId) (task *Task, err error)

GetTaskByID returns a Task given its ID.

func (*Base) GetTasks

func (b *Base) GetTasks(account bson.ObjectId, application string, lp ListParams, lr *ListResult) (err error)

GetTasks returns a list of Tasks.

func (*Base) NewAccount

func (b *Base) NewAccount(name *string) (account *Account, err error)

NewAccount creates a new Account.

func (*Base) NewApplication

func (b *Base) NewApplication(account bson.ObjectId, name string) (application *Application, err error)

NewApplication creates a new Application.

func (*Base) NewAttempt

func (b *Base) NewAttempt(task *Task, deletePending bool, force bool) (*Attempt, error)

NewAttempt creates a new Attempt.

func (*Base) NewQueue

func (b *Base) NewQueue(account bson.ObjectId, applicationName string, name string, retry *Retry, maxInFlight int) (queue *Queue, err error)

NewQueue creates a new Queue.

func (*Base) NewTask

func (b *Base) NewTask(account bson.ObjectId, applicationName string, name string, queueName string, URL string, auth HTTPAuth, method string, headers map[string]string, payload string, schedule string, retry *Retry, active bool) (task *Task, err error)

NewTask creates a new Task.

func (*Base) NextAttempt

func (b *Base) NextAttempt(ttr int64) (*Attempt, error)

NextAttempt reserves and returns the next Attempt.

func (*Base) NextAttemptForTask

func (b *Base) NextAttemptForTask(attempt *Attempt) (nextAttempt *Attempt, err error)

NextAttemptForTask enqueue the next Attempt if any and returns it.

func (*Base) SetAttemptQueuedForTask

func (b *Base) SetAttemptQueuedForTask(task *Task) (err error)

SetAttemptQueuedForTask marks the attempt as queued for a given task ID.

func (*Base) ShouldRefreshSession

func (b *Base) ShouldRefreshSession(err error) (bool, error)

ShouldRefreshSession checks if we should refresh the mongo session

func (*Base) TouchAttempt

func (b *Base) TouchAttempt(attemptID bson.ObjectId, seconds int64) error

TouchAttempt reserves an attemptsttempt for more time.

func (*Base) UpdateAccount

func (b *Base) UpdateAccount(accountID bson.ObjectId, name *string) (account *Account, err error)

UpdateAccount updates an Account.

type HTTPAuth

type HTTPAuth struct {
	Username string `json:"username,omitempty" bson:"username,omitempty"`
	Password string `json:"password,omitempty" bson:"password,omitempty"`
}

HTTPAuth is used for HTTP Basic Auth.

type ListParams

type ListParams struct {
	Fields  []string
	Filters map[string]string
	Sort    map[string]string
	Page    int
	Limit   int
}

type ListResult

type ListResult struct {
	List    interface{} `json:"list"`
	HasMore bool        `json:"hasMore"`
	Total   int         `json:"total"`
	Count   int         `json:"count"`
	Page    int         `json:"page"`
	Pages   int         `json:"pages"`
}

ListResult is the structure used for listing collections.

type Queue

type Queue struct {
	// ID is the ID of the Queue.
	ID bson.ObjectId `bson:"_id"`

	// Account is the ID of the Account owning the Queue.
	Account bson.ObjectId `bson:"account"`

	// Application is the name of the parent Application.
	Application string `bson:"application"`

	// Name is the Queue's name.
	Name string `bson:"name"`

	// Retry is the retry strategy parameters in case of errors.
	Retry *Retry `bson:"retry"`

	// Deleted
	Deleted bool `bson:"deleted"`

	// MaxInFlight is the maximum number of attempts executed in parallel.
	MaxInFlight int `bson:"max_in_flight"`

	// AvailableInFlight is the available number of slots to execute tasks in parallel.
	AvailableInFlight int `bson:"available_in_flight"`

	// AttemptsInFlight is the list of attempts currently in flight.
	AttemptsInFlight []bson.ObjectId `bson:"attempts_in_flight"`
}

Queue ...

type Retry

type Retry struct {
	// Attempts is the current number of attempts we did.
	Attempts int `bson:"attempts" json:"attempts"`
	// MaxAttempts is the maximum number of attempts we will try.
	MaxAttempts int `bson:"max_attempts" json:"maxAttempts"`
	// Factor is factor to increase the duration between each attempts.
	Factor float64 `bson:"factor" json:"factor"`
	// Min is the minimum duration between each attempts in seconds.
	Min int `bson:"min" json:"min"`
	// Max is the maximum duration between each attempts in seconds.
	Max int `bson:"max" json:"max"`
}

func (*Retry) NextAttempt

func (r *Retry) NextAttempt(now int64) (int64, error)

func (*Retry) SetDefault

func (r *Retry) SetDefault()

type Task

type Task struct {
	// ID is the ID of the Task.
	ID bson.ObjectId `bson:"_id"`

	// Account is the ID of the Account owning the Task.
	Account bson.ObjectId `bson:"account"`

	// Application is the name of the parent Application.
	Application string `bson:"application"`

	// Name is the task's name.
	Name string `bson:"name"`

	// Queue is the name of the parent Queue.
	Queue string `bson:"queue"`

	// QueueID is the ID of the parent Queue
	QueueID bson.ObjectId `bson:"queue_id"`

	// URL is the URL that the worker with requests.
	URL string `bson:"url"`

	// HTTPAuth is the HTTP authentication to use if any.
	HTTPAuth HTTPAuth `bson:"auth"`

	// Method is the HTTP method that will be used to execute the request.
	Method string `bson:"method"`

	// Headers are the HTTP headers that will be used when executing the request.
	Headers map[string]string `bson:"headers,omitempty"`

	// Payload is arbitrary data that will be POSTed on the URL.
	Payload string `bson:"payload,omitempty"`

	// Schedule is a cron specification describing the recurrency if any.
	Schedule string `bson:"schedule"`

	// At is a Unix timestamp representing the next time a request must be performed.
	At int64 `bson:"at"`

	// Status is either `pending`, `retrying`, `canceled`, `success` or `error`
	Status string `bson:"status"`

	// Executed is the timestamp of the last time a attempt was executed.
	Executed int64 `bson:"executed,omitempty"`

	// Active is the task active.
	Active bool `bson:"active"`

	// Errors counts the number of attempts that failed.
	Errors int `bson:"errors,omitempty"`

	// LastError is the timestamp of the last attempt in error status
	LastError int64 `bson:"last_error,omitempty"`

	// LastSuccess is the timestamp of the last attempt in success status
	LastSuccess int64 `bson:"last_success,omitempty"`

	// Executions counts the number of attempts that were executed.
	Executions int `bson:"executions,omitempty"`

	// Retry is the retry strategy parameters in case of errors.
	Retry *Retry `bson:"retry"`

	// CurrentAttempt is the current attempt ID for this task.
	CurrentAttempt bson.ObjectId `bson:"current_attempt"`

	// AttemptQueued is set to true when the attempt as been successfully created.
	AttemptQueued bool `bson:"attempt_queued"`

	// AttemptUpdated is the Unix timestamp of the last update of the current attempt.
	AttemptUpdated int64 `bson:"attempt_updated"`

	// Deleted
	Deleted bool `bson:"deleted"`
}

Task describes a Task.

func (*Task) ErrorRate

func (h *Task) ErrorRate() int

ErrorRate is the error rate of the task from 0 to 100 percent.

Jump to

Keyboard shortcuts

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