asynctask

package
v0.0.0-...-ed9fe1d Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2018 License: BSD-3-Clause Imports: 18 Imported by: 0

Documentation

Overview

Package asynctask provides async task execution support on GAE apps

Index

Constants

View Source
const AsyncTaskKindLoggerKey = "ent.async_task"

AsyncTaskKindLoggerKey is a logger key name for the ent

View Source
const AsyncTaskQueryLoggerKey = "ent.query.async_task"

AsyncTaskQueryLoggerKey is a logger key name for the ent

View Source
const LoggerKey = "gae.service.asynctask"

LoggerKey is a xlog key for this package

Variables

View Source
var (
	ErrAlreadyExists     = fmt.Errorf("already exists")
	ErrNoTaskInstance    = fmt.Errorf("no task instance")
	ErrAlreadyProcessing = fmt.Errorf("already processing")
	ErrPushTaskFailed    = fmt.Errorf("pushtask failed")
)

errors

View Source
var DefaultAsyncTaskKind = &AsyncTaskKind{}

DefaultAsyncTaskKind is a default value of *AsyncTaskKind

View Source
var TaskIDContextKey = xcontext.NewKey("taskid")

TaskIDContextKey is a context key for AsyncTaskID

Functions

This section is empty.

Types

type AsyncTask

type AsyncTask struct {
	ID        string     `json:"id" ent:"id"`
	ConfigKey string     `json:"config_key"`
	Params    string     `json:"params" datastore:",noindex"`
	Status    Status     `json:"status"`
	Error     string     `json:"error" datastore:",noindex"`
	Progress  []Progress `json:"progress" datastore:",noindex"`
	TaskStore TaskStore  `json:"taskstore" datastore",noindex"`
	StartAt   time.Time  `json:"start_at"`
	FinishAt  time.Time  `json:"finish_at"`
	UpdatedAt time.Time  `json:"updated_at" ent:"timestamp"`

	// Deprecated
	Path  string `json:"path"  datastore:",noindex"`
	Query string `json:"query"  datastore:",noindex"`
}

AsyncTask is a record to track a task progress

func NewAsyncTask

func NewAsyncTask() *AsyncTask

NewAsyncTask returns a new *AsyncTask with default field values.

func (*AsyncTask) GetLogPrefix

func (t *AsyncTask) GetLogPrefix() string

GetLogPrefix returns a prefix string for logger

func (*AsyncTask) GetStatus

func (t *AsyncTask) GetStatus() *TaskStatus

GetStatus returns a new *TaskStatus exposed to clients.

func (*AsyncTask) IsStoreEmpty

func (t *AsyncTask) IsStoreEmpty() bool

IsStoreEmpty returns whether TaskStore field is empty or not

func (*AsyncTask) LastProgress

func (t *AsyncTask) LastProgress() *Progress

LastProgress returns the last progress of the task

func (*AsyncTask) LoadStore

func (t *AsyncTask) LoadStore(v interface{}) error

LoadStore updates the task store

func (*AsyncTask) NewKey

func (a *AsyncTask) NewKey(ctx context.Context) *datastore.Key

func (*AsyncTask) SaveStore

func (t *AsyncTask) SaveStore(v interface{}) error

SaveStore updates the task store

func (*AsyncTask) UpdateByForm

func (a *AsyncTask) UpdateByForm(form *keyvalue.GetProxy)

UpdateByForm updates the fields by form values. All values should be validated before calling this function.

type AsyncTaskKind

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

func (*AsyncTaskKind) Delete

func (k *AsyncTaskKind) Delete(ctx context.Context, key interface{}) (*datastore.Key, error)

Delete deletes the entity from datastore

func (*AsyncTaskKind) DeleteMatched

func (k *AsyncTaskKind) DeleteMatched(ctx context.Context, q *AsyncTaskQuery) (int, error)

DeleteMatched deletes the all ents that match with the query. This func modify Limit/StartKey condition in the query so that you should restore it if you want to reuse the query.

func (*AsyncTaskKind) DeleteMulti

func (k *AsyncTaskKind) DeleteMulti(ctx context.Context, keys interface{}) ([]*datastore.Key, error)

DeleteMulti do Delete with multiple keys

func (*AsyncTaskKind) EnforceNamespace

func (k *AsyncTaskKind) EnforceNamespace(ns string, b bool) *AsyncTaskKind

EnforceNamespace enforces namespace for Get/Put/Delete or not.

func (*AsyncTaskKind) Get

func (k *AsyncTaskKind) Get(ctx context.Context, key interface{}) (*datastore.Key, *AsyncTask, error)

Get gets the kind entity from datastore

func (*AsyncTaskKind) GetMulti

func (k *AsyncTaskKind) GetMulti(ctx context.Context, keys interface{}) ([]*datastore.Key, []*AsyncTask, error)

GetMulti do Get with multiple keys. keys must be []string, []*datastore.Key, or []interface{}

func (*AsyncTaskKind) MustDelete

func (k *AsyncTaskKind) MustDelete(ctx context.Context, key interface{}) *datastore.Key

MustDelete is like Delete but panic if an error occurs

func (*AsyncTaskKind) MustDeleteMulti

func (k *AsyncTaskKind) MustDeleteMulti(ctx context.Context, keys interface{}) []*datastore.Key

MustDeleteMulti is like DeleteMulti but panic if an error occurs

func (*AsyncTaskKind) MustGet

func (k *AsyncTaskKind) MustGet(ctx context.Context, key interface{}) *AsyncTask

MustGet is like Get but returns only values and panic if error happens.

func (*AsyncTaskKind) MustGetMulti

func (k *AsyncTaskKind) MustGetMulti(ctx context.Context, keys interface{}) []*AsyncTask

MustGetMulti is like GetMulti but returns only values and panic if error happens.

func (*AsyncTaskKind) MustPut

func (k *AsyncTaskKind) MustPut(ctx context.Context, ent *AsyncTask) *datastore.Key

MustPut is like Put and panic if an error occurrs.

func (*AsyncTaskKind) MustPutMulti

func (k *AsyncTaskKind) MustPutMulti(ctx context.Context, ents []*AsyncTask) []*datastore.Key

MustPutMulti is like PutMulti but panic if an error occurs

func (*AsyncTaskKind) MustReplace

func (k *AsyncTaskKind) MustReplace(ctx context.Context, ent *AsyncTask, replacer AsyncTaskKindReplacer) (*datastore.Key, *AsyncTask)

func (*AsyncTaskKind) MustReplaceMulti

func (k *AsyncTaskKind) MustReplaceMulti(ctx context.Context, ents []*AsyncTask, replacer AsyncTaskKindReplacer) ([]*datastore.Key, []*AsyncTask)

func (*AsyncTaskKind) Put

func (k *AsyncTaskKind) Put(ctx context.Context, ent *AsyncTask) (*datastore.Key, error)

Put puts the entity to datastore.

func (*AsyncTaskKind) PutMulti

func (k *AsyncTaskKind) PutMulti(ctx context.Context, ents []*AsyncTask) ([]*datastore.Key, error)

PutMulti do Put with multiple keys

func (*AsyncTaskKind) Replace

func (*AsyncTaskKind) ReplaceMulti

func (k *AsyncTaskKind) ReplaceMulti(ctx context.Context, ents []*AsyncTask, replacer AsyncTaskKindReplacer) ([]*datastore.Key, []*AsyncTask, error)

func (*AsyncTaskKind) UseDefaultIfNil

func (k *AsyncTaskKind) UseDefaultIfNil(b bool) *AsyncTaskKind

type AsyncTaskKindReplacer

type AsyncTaskKindReplacer interface {
	Replace(*AsyncTask, *AsyncTask) *AsyncTask
}

type AsyncTaskKindReplacerFunc

type AsyncTaskKindReplacerFunc func(*AsyncTask, *AsyncTask) *AsyncTask

func (AsyncTaskKindReplacerFunc) Replace

func (f AsyncTaskKindReplacerFunc) Replace(ent1 *AsyncTask, ent2 *AsyncTask) *AsyncTask

type AsyncTaskPagination

type AsyncTaskPagination struct {
	Start string           `json:"start"`
	End   string           `json:"end"`
	Count int              `json:"count,omitempty"`
	Data  []*AsyncTask     `json:"data"`
	Keys  []*datastore.Key `json:"-"`
}

type AsyncTaskQuery

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

AsyncTaskQuery helps to build and execute a query

func NewAsyncTaskQuery

func NewAsyncTaskQuery() *AsyncTaskQuery

func (*AsyncTaskQuery) Ancestor

func (q *AsyncTaskQuery) Ancestor(a lazy.Value) *AsyncTaskQuery

Ancestor sets the ancestor filter

func (*AsyncTaskQuery) Asc

func (q *AsyncTaskQuery) Asc(name string) *AsyncTaskQuery

Asc specifies ascending order on the given filed.

func (*AsyncTaskQuery) Count

func (q *AsyncTaskQuery) Count(ctx context.Context) (int, error)

Count returns the count of entities

func (*AsyncTaskQuery) Desc

func (q *AsyncTaskQuery) Desc(name string) *AsyncTaskQuery

Desc specifies descending order on the given filed.

func (*AsyncTaskQuery) End

func (q *AsyncTaskQuery) End(value lazy.Value) *AsyncTaskQuery

Limit specifies the numbe of limit returend by this query.

func (*AsyncTaskQuery) Eq

func (q *AsyncTaskQuery) Eq(name string, value lazy.Value) *AsyncTaskQuery

Eq sets the "=" filter on the name field.

func (*AsyncTaskQuery) Ge

func (q *AsyncTaskQuery) Ge(name string, value lazy.Value) *AsyncTaskQuery

Ge sets the ">=" filter on the "name" field.

func (*AsyncTaskQuery) GetAll

func (q *AsyncTaskQuery) GetAll(ctx context.Context) ([]*datastore.Key, []*AsyncTask, error)

GetAll returns all key and value of the query.

func (*AsyncTaskQuery) GetAllValues

func (q *AsyncTaskQuery) GetAllValues(ctx context.Context) ([]*AsyncTask, error)

GetAllValues is like GetAll but returns only values

func (*AsyncTaskQuery) Gt

func (q *AsyncTaskQuery) Gt(name string, value lazy.Value) *AsyncTaskQuery

Gt sets the ">" filter on the "name" field.

func (*AsyncTaskQuery) Le

func (q *AsyncTaskQuery) Le(name string, value lazy.Value) *AsyncTaskQuery

Le sets the "<=" filter on the "name" field.

func (*AsyncTaskQuery) Limit

func (q *AsyncTaskQuery) Limit(n lazy.Value) *AsyncTaskQuery

Limit specifies the numbe of limit returend by this query.

func (*AsyncTaskQuery) Lt

func (q *AsyncTaskQuery) Lt(name string, value lazy.Value) *AsyncTaskQuery

Lt sets the "<" filter on the "name" field.

func (*AsyncTaskQuery) MustCount

func (q *AsyncTaskQuery) MustCount(ctx context.Context) int

MustCount returns the count of entities

func (*AsyncTaskQuery) MustGetAll

func (q *AsyncTaskQuery) MustGetAll(ctx context.Context) ([]*datastore.Key, []*AsyncTask)

MustGetAll is like GetAll but panic if an error occurrs.

func (*AsyncTaskQuery) MustGetAllValues

func (q *AsyncTaskQuery) MustGetAllValues(ctx context.Context) []*AsyncTask

MustGetAllValues is like GetAllValues but panic if an error occurrs

func (*AsyncTaskQuery) MustRun

MustRun is like Run but panic if an error occurrs

func (*AsyncTaskQuery) Ne

func (q *AsyncTaskQuery) Ne(name string, value lazy.Value) *AsyncTaskQuery

Ne sets the "!=" filter on the "name" field.

func (*AsyncTaskQuery) Run

Run returns the a result as *AsyncTaskPagination object

func (*AsyncTaskQuery) Start

func (q *AsyncTaskQuery) Start(value lazy.Value) *AsyncTaskQuery

Limit specifies the numbe of limit returend by this query.

func (*AsyncTaskQuery) ViaKeys

ViaKeys optimize to execute keys-only query then call k.GetMulti() to fetch values. This would reduce the datastore query and maximize the memcache usage if the query called many times in a short time window.

type Config

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

Config is an configuration object to define AsyncTask endpoints

func NewConfig

func NewConfig(key string, queue *taskqueue.PushQueue, logic Logic) *Config

NewConfig returns a new *Config named by `key“ to execute the async logic on top of the queue.

func (*Config) GetDescription

func (c *Config) GetDescription() string

GetDescription returns the descripiton for the async task

func (*Config) GetKey

func (c *Config) GetKey() string

GetKey returns a key string for the async task

func (*Config) GetRecentTasks

func (c *Config) GetRecentTasks(ctx context.Context, n int) []*TaskStatus

GetRecentTasks returns a list of recent tasks ordered by StartAt

func (*Config) GetSchedule

func (c *Config) GetSchedule() string

GetSchedule returns the (gae cron formatted) schedule string for the async task

func (*Config) GetStatus

func (c *Config) GetStatus(ctx context.Context, taskID string) *TaskStatus

GetStatus returns a *TaskStatus for the given taskID

func (*Config) Prepare

func (c *Config) Prepare(ctx context.Context, taskID string, instancePath string, params url.Values) (*TaskStatus, error)

Prepare prepares a AsyncTask record and returns the associated task id.

func (*Config) Process

func (c *Config) Process(ctx context.Context, taskID string, instancePath string, params url.Values) (*Progress, error)

Process run a logic and update AsyncTask record.

func (*Config) WithDescription

func (c *Config) WithDescription(description string) *Config

WithDescription sets the description for the async task

func (*Config) WithSchedule

func (c *Config) WithSchedule(schedule string) *Config

WithSchedule sets the (gae cron formatted) schedule for the async task

type Func

Func is an function to implement Logic

func (Func) Run

func (f Func) Run(ctx context.Context, params *keyvalue.GetProxy, t *AsyncTask) (*Progress, error)

Run implements Logic#Run

type Logic

type Logic interface {
	Run(context.Context, *keyvalue.GetProxy, *AsyncTask) (*Progress, error)
}

Logic is an interface to execute a task

type Progress

type Progress struct {
	Total   int        `json:"total,omitempty"`
	Current int        `json:"current,omitempty"`
	Message string     `json:"message,omitempty"`
	Next    url.Values `json:"-" datastore:"-"`
}

Progress is a struct that represents the task progress

type Status

type Status int

Status is a value to represent the task status

const (
	StatusUnknown Status = iota
	StatusReady
	StatusRunning
	StatusSuccess
	StatusFailure
)

Available values of Status

func MustParseStatus

func MustParseStatus(s string) Status

func ParseStatus

func ParseStatus(s string) (Status, error)

func ParseStatusOr

func ParseStatusOr(s string, or Status) Status

func (Status) MarshalJSON

func (i Status) MarshalJSON() ([]byte, error)

func (Status) String

func (i Status) String() string

func (*Status) UnmarshalJSON

func (i *Status) UnmarshalJSON(b []byte) error

type TaskStatus

type TaskStatus struct {
	ID       string     `json:"id"`
	Status   Status     `json:"status"`
	StartAt  *time.Time `json:"start_at,omitempty"`
	FinishAt *time.Time `json:"finish_at,omitempty"`
	Error    *string    `json:"error,omitempty"`
	Progress *Progress  `json:"progress,omitempty"`
}

TaskStatus is a struct that can be used in task manager clients.

type TaskStore

type TaskStore []byte

TaskStore is a type alias for []byte

func (TaskStore) MarshalJSON

func (cs TaskStore) MarshalJSON() ([]byte, error)

MarshalJSON implements json.MarshalJSON()

func (*TaskStore) UnmarshalJSON

func (cs *TaskStore) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler#UnmarshalJSON([]byte)

Jump to

Keyboard shortcuts

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