Documentation ¶
Overview ¶
Package asynctask provides async task execution support on GAE apps
Index ¶
- Constants
- Variables
- type AsyncTask
- func (t *AsyncTask) GetLogPrefix() string
- func (t *AsyncTask) GetStatus() *TaskStatus
- func (t *AsyncTask) IsStoreEmpty() bool
- func (t *AsyncTask) LastProgress() *Progress
- func (t *AsyncTask) LoadStore(v interface{}) error
- func (a *AsyncTask) NewKey(ctx context.Context) *datastore.Key
- func (t *AsyncTask) SaveStore(v interface{}) error
- func (a *AsyncTask) UpdateByForm(form *keyvalue.GetProxy)
- type AsyncTaskKind
- func (k *AsyncTaskKind) Delete(ctx context.Context, key interface{}) (*datastore.Key, error)
- func (k *AsyncTaskKind) DeleteMatched(ctx context.Context, q *AsyncTaskQuery) (int, error)
- func (k *AsyncTaskKind) DeleteMulti(ctx context.Context, keys interface{}) ([]*datastore.Key, error)
- func (k *AsyncTaskKind) EnforceNamespace(ns string, b bool) *AsyncTaskKind
- func (k *AsyncTaskKind) Get(ctx context.Context, key interface{}) (*datastore.Key, *AsyncTask, error)
- func (k *AsyncTaskKind) GetMulti(ctx context.Context, keys interface{}) ([]*datastore.Key, []*AsyncTask, error)
- func (k *AsyncTaskKind) MustDelete(ctx context.Context, key interface{}) *datastore.Key
- func (k *AsyncTaskKind) MustDeleteMulti(ctx context.Context, keys interface{}) []*datastore.Key
- func (k *AsyncTaskKind) MustGet(ctx context.Context, key interface{}) *AsyncTask
- func (k *AsyncTaskKind) MustGetMulti(ctx context.Context, keys interface{}) []*AsyncTask
- func (k *AsyncTaskKind) MustPut(ctx context.Context, ent *AsyncTask) *datastore.Key
- func (k *AsyncTaskKind) MustPutMulti(ctx context.Context, ents []*AsyncTask) []*datastore.Key
- func (k *AsyncTaskKind) MustReplace(ctx context.Context, ent *AsyncTask, replacer AsyncTaskKindReplacer) (*datastore.Key, *AsyncTask)
- func (k *AsyncTaskKind) MustReplaceMulti(ctx context.Context, ents []*AsyncTask, replacer AsyncTaskKindReplacer) ([]*datastore.Key, []*AsyncTask)
- func (k *AsyncTaskKind) Put(ctx context.Context, ent *AsyncTask) (*datastore.Key, error)
- func (k *AsyncTaskKind) PutMulti(ctx context.Context, ents []*AsyncTask) ([]*datastore.Key, error)
- func (k *AsyncTaskKind) Replace(ctx context.Context, ent *AsyncTask, replacer AsyncTaskKindReplacer) (*datastore.Key, *AsyncTask, error)
- func (k *AsyncTaskKind) ReplaceMulti(ctx context.Context, ents []*AsyncTask, replacer AsyncTaskKindReplacer) ([]*datastore.Key, []*AsyncTask, error)
- func (k *AsyncTaskKind) UseDefaultIfNil(b bool) *AsyncTaskKind
- type AsyncTaskKindReplacer
- type AsyncTaskKindReplacerFunc
- type AsyncTaskPagination
- type AsyncTaskQuery
- func (q *AsyncTaskQuery) Ancestor(a lazy.Value) *AsyncTaskQuery
- func (q *AsyncTaskQuery) Asc(name string) *AsyncTaskQuery
- func (q *AsyncTaskQuery) Count(ctx context.Context) (int, error)
- func (q *AsyncTaskQuery) Desc(name string) *AsyncTaskQuery
- func (q *AsyncTaskQuery) End(value lazy.Value) *AsyncTaskQuery
- func (q *AsyncTaskQuery) Eq(name string, value lazy.Value) *AsyncTaskQuery
- func (q *AsyncTaskQuery) Ge(name string, value lazy.Value) *AsyncTaskQuery
- func (q *AsyncTaskQuery) GetAll(ctx context.Context) ([]*datastore.Key, []*AsyncTask, error)
- func (q *AsyncTaskQuery) GetAllValues(ctx context.Context) ([]*AsyncTask, error)
- func (q *AsyncTaskQuery) Gt(name string, value lazy.Value) *AsyncTaskQuery
- func (q *AsyncTaskQuery) Le(name string, value lazy.Value) *AsyncTaskQuery
- func (q *AsyncTaskQuery) Limit(n lazy.Value) *AsyncTaskQuery
- func (q *AsyncTaskQuery) Lt(name string, value lazy.Value) *AsyncTaskQuery
- func (q *AsyncTaskQuery) MustCount(ctx context.Context) int
- func (q *AsyncTaskQuery) MustGetAll(ctx context.Context) ([]*datastore.Key, []*AsyncTask)
- func (q *AsyncTaskQuery) MustGetAllValues(ctx context.Context) []*AsyncTask
- func (q *AsyncTaskQuery) MustRun(ctx context.Context) *AsyncTaskPagination
- func (q *AsyncTaskQuery) Ne(name string, value lazy.Value) *AsyncTaskQuery
- func (q *AsyncTaskQuery) Run(ctx context.Context) (*AsyncTaskPagination, error)
- func (q *AsyncTaskQuery) Start(value lazy.Value) *AsyncTaskQuery
- func (q *AsyncTaskQuery) ViaKeys(k *AsyncTaskKind) *AsyncTaskQuery
- type Config
- func (c *Config) GetDescription() string
- func (c *Config) GetKey() string
- func (c *Config) GetRecentTasks(ctx context.Context, n int) []*TaskStatus
- func (c *Config) GetSchedule() string
- func (c *Config) GetStatus(ctx context.Context, taskID string) *TaskStatus
- func (c *Config) Prepare(ctx context.Context, taskID string, instancePath string, params url.Values) (*TaskStatus, error)
- func (c *Config) Process(ctx context.Context, taskID string, instancePath string, params url.Values) (*Progress, error)
- func (c *Config) WithDescription(description string) *Config
- func (c *Config) WithSchedule(schedule string) *Config
- type Func
- type Logic
- type Progress
- type Status
- type TaskStatus
- type TaskStore
Constants ¶
const AsyncTaskKindLoggerKey = "ent.async_task"
AsyncTaskKindLoggerKey is a logger key name for the ent
const AsyncTaskQueryLoggerKey = "ent.query.async_task"
AsyncTaskQueryLoggerKey is a logger key name for the ent
const LoggerKey = "gae.service.asynctask"
LoggerKey is a xlog key for this package
Variables ¶
var ( ErrAlreadyExists = fmt.Errorf("already exists") ErrNoTaskInstance = fmt.Errorf("no task instance") ErrAlreadyProcessing = fmt.Errorf("already processing") ErrPushTaskFailed = fmt.Errorf("pushtask failed") )
errors
var DefaultAsyncTaskKind = &AsyncTaskKind{}
DefaultAsyncTaskKind is a default value of *AsyncTaskKind
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 ¶
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 ¶
IsStoreEmpty returns whether TaskStore field is empty or not
func (*AsyncTask) LastProgress ¶
LastProgress returns the last progress of the task
func (*AsyncTask) UpdateByForm ¶
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) 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) MustPutMulti ¶
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) Replace ¶
func (k *AsyncTaskKind) Replace(ctx context.Context, ent *AsyncTask, replacer AsyncTaskKindReplacer) (*datastore.Key, *AsyncTask, error)
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 AsyncTaskPagination ¶
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) 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 ¶
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 ¶
func (q *AsyncTaskQuery) MustRun(ctx context.Context) *AsyncTaskPagination
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 ¶
func (q *AsyncTaskQuery) Run(ctx context.Context) (*AsyncTaskPagination, error)
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 ¶
func (q *AsyncTaskQuery) ViaKeys(k *AsyncTaskKind) *AsyncTaskQuery
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 ¶
NewConfig returns a new *Config named by `key“ to execute the async logic on top of the queue.
func (*Config) GetDescription ¶
GetDescription returns the descripiton 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 ¶
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 ¶
WithDescription sets the description for the async task
func (*Config) WithSchedule ¶
WithSchedule sets the (gae cron formatted) schedule for the async 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
Available values of Status
func MustParseStatus ¶
func ParseStatus ¶
func ParseStatusOr ¶
func (Status) MarshalJSON ¶
func (*Status) UnmarshalJSON ¶
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 ¶
MarshalJSON implements json.MarshalJSON()
func (*TaskStore) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler#UnmarshalJSON([]byte)