Documentation
¶
Index ¶
- Variables
- type Job
- type JobManager
- func (m *JobManager) AddPriority(p Priority)
- func (m *JobManager) Hook(trigger func(ctx context.Context, job JobWrapper))
- func (m *JobManager) RefreshEvery(interval time.Duration)
- func (m *JobManager) Run(ctx context.Context, numThreads int) error
- func (m *JobManager) RunOne(ctx context.Context) error
- func (m *JobManager) Schedule(priority Priority, job *Job) error
- func (m *JobManager) SetDefaultJobAging(timeslice time.Duration)
- type JobResult
- func (r JobResult) Get(key any) any
- func (r JobResult) GetBool(key any) bool
- func (r JobResult) GetFloat32(key any) float32
- func (r JobResult) GetFloat64(key any) float64
- func (r JobResult) GetInt(key any) int
- func (r JobResult) GetInt32(key any) int32
- func (r JobResult) GetInt64(key any) int64
- func (r JobResult) GetString(key any) string
- func (r JobResult) GetUint(key any) uint
- func (r JobResult) GetUint32(key any) uint32
- func (r JobResult) GetUint64(key any) uint64
- func (r JobResult) Has(key any) bool
- func (r *JobResult) Set(key, value any) *JobResult
- type JobWrapper
- type Priority
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrExistedJob = errors.New("existed job")
Functions ¶
This section is empty.
Types ¶
type Job ¶
type Job struct {
// contains filtered or unexported fields
}
Job is the wrapper of a function which allows to wait the result asynchronously.
func (*Job) Exec ¶
Exec executes the job and returns the result. Do not call this method multiple times.
func (*Job) GetResult ¶
GetResult returns the JobResult if the job already completed. Otherwise, it returns nil.
func (*Job) IsCompleted ¶
IsCompleted returns true if the job already completed.
type JobManager ¶
type JobManager struct {
// contains filtered or unexported fields
}
JobManager allows to schedule job based on its priority.
Example ¶
// Setup priority
jm := gojm.New()
jm.AddPriority(Urgent)
jm.AddPriority(Necessary)
jm.AddPriority(Background)
backgroundJob := gojm.NewJob(func(ctx context.Context) *gojm.JobResult {
fmt.Println("background done")
return gojm.Result("background")
})
// In other goroutines, schedule your jobs.
go func() {
time.Sleep(20 * time.Millisecond)
jm.Schedule(Background, backgroundJob)
time.Sleep(20 * time.Millisecond)
jm.Schedule(Urgent, gojm.NewJob(func(ctx context.Context) *gojm.JobResult {
fmt.Println("urgent done")
return gojm.EmptyResult()
}))
time.Sleep(20 * time.Millisecond)
jm.Schedule(Necessary, gojm.NewJob(func(ctx context.Context) *gojm.JobResult {
fmt.Println("necessary done")
return gojm.EmptyResult()
}))
}()
// Because this is an example, we need to stop this function after one
// second.
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 1000*time.Millisecond)
defer cancel()
// Run the manager with the ability of handling 2 jobs in a time. In
// reality, you could run this function forever.
jm.Run(ctx, 2)
// You also wait for the result of a job.
result := backgroundJob.WaitResult(ctx)
fmt.Println("Result of background job:", result.Get(nil))
Output: background done urgent done necessary done Result of background job: background
func (*JobManager) AddPriority ¶
func (m *JobManager) AddPriority(p Priority)
AddPriority sets a new Priority to JobManager.
func (*JobManager) Hook ¶
func (m *JobManager) Hook(trigger func(ctx context.Context, job JobWrapper))
Hook sets a trigger function to be executed when the job has just completed. This method should be called before calling Run() or RunOne().
func (*JobManager) RefreshEvery ¶
func (m *JobManager) RefreshEvery(interval time.Duration)
RefreshEvery sets an interval which refreshes the priority of jobs every time interval passed. If you do not call this method, this interval will be chosen automatically (equal to the least aging timeslice of all priorities).
func (*JobManager) Run ¶
func (m *JobManager) Run(ctx context.Context, numThreads int) error
Run starts the JobManager. The parameter numThreads specifies the number of Jobs which could be executed concurrently.
func (*JobManager) RunOne ¶
func (m *JobManager) RunOne(ctx context.Context) error
RunOne starts the JobManager which only one Job could be executed at a time.
func (*JobManager) Schedule ¶
func (m *JobManager) Schedule(priority Priority, job *Job) error
Schedule adds a Job to JobManager, the job will be scheduled to execute later.
func (*JobManager) SetDefaultJobAging ¶
func (m *JobManager) SetDefaultJobAging(timeslice time.Duration)
SetDefaultJobAging sets a timeslice. When the job has existed for more than this timeslice, it will be moved to the higher priority. This timeslice is only applied when the priority hasn't its own aging.
type JobResult ¶
JobResult is the returned value of Job.
func (JobResult) GetFloat32 ¶
GetFloat32 returns the float32 value with a given key.
func (JobResult) GetFloat64 ¶
GetFloat64 returns the float64 value with a given key.
type JobWrapper ¶
type JobWrapper struct {
Priority Priority
OriginalPriority Priority
// contains filtered or unexported fields
}
func (JobWrapper) Unwrap ¶
func (w JobWrapper) Unwrap() *Job
type Priority ¶
type Priority struct {
// contains filtered or unexported fields
}