2018-10-26 21:48:22 +08:00
|
|
|
// Manage background jobs that the rc is running
|
|
|
|
|
2019-07-18 18:13:54 +08:00
|
|
|
package jobs
|
2018-10-26 21:48:22 +08:00
|
|
|
|
|
|
|
import (
|
2019-06-17 16:34:30 +08:00
|
|
|
"context"
|
2019-07-18 18:13:54 +08:00
|
|
|
"fmt"
|
2019-07-30 19:22:06 +08:00
|
|
|
"runtime/debug"
|
2018-10-26 21:48:22 +08:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs/rc"
|
2019-07-18 18:13:54 +08:00
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs/accounting"
|
2019-07-18 18:13:54 +08:00
|
|
|
|
2018-10-26 21:48:22 +08:00
|
|
|
"github.com/pkg/errors"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
2018-10-26 21:48:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Job describes a asynchronous task started via the rc package
|
|
|
|
type Job struct {
|
|
|
|
mu sync.Mutex
|
2019-06-17 16:47:29 +08:00
|
|
|
ID int64 `json:"id"`
|
2019-07-18 18:13:54 +08:00
|
|
|
Group string `json:"group"`
|
2019-06-17 16:47:29 +08:00
|
|
|
StartTime time.Time `json:"startTime"`
|
|
|
|
EndTime time.Time `json:"endTime"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
Finished bool `json:"finished"`
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Duration float64 `json:"duration"`
|
2019-07-18 18:13:54 +08:00
|
|
|
Output rc.Params `json:"output"`
|
2019-06-17 16:47:29 +08:00
|
|
|
Stop func() `json:"-"`
|
2018-10-26 21:48:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Jobs describes a collection of running tasks
|
|
|
|
type Jobs struct {
|
|
|
|
mu sync.RWMutex
|
|
|
|
jobs map[int64]*Job
|
|
|
|
expireInterval time.Duration
|
|
|
|
expireRunning bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
running = newJobs()
|
|
|
|
jobID = int64(0)
|
|
|
|
)
|
|
|
|
|
|
|
|
// newJobs makes a new Jobs structure
|
|
|
|
func newJobs() *Jobs {
|
|
|
|
return &Jobs{
|
|
|
|
jobs: map[int64]*Job{},
|
2019-06-13 20:04:59 +08:00
|
|
|
expireInterval: fs.Config.RcJobExpireInterval,
|
2018-10-26 21:48:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// kickExpire makes sure Expire is running
|
|
|
|
func (jobs *Jobs) kickExpire() {
|
|
|
|
jobs.mu.Lock()
|
|
|
|
defer jobs.mu.Unlock()
|
|
|
|
if !jobs.expireRunning {
|
|
|
|
time.AfterFunc(jobs.expireInterval, jobs.Expire)
|
|
|
|
jobs.expireRunning = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expire expires any jobs that haven't been collected
|
|
|
|
func (jobs *Jobs) Expire() {
|
|
|
|
jobs.mu.Lock()
|
|
|
|
defer jobs.mu.Unlock()
|
|
|
|
now := time.Now()
|
|
|
|
for ID, job := range jobs.jobs {
|
|
|
|
job.mu.Lock()
|
2019-06-13 20:04:59 +08:00
|
|
|
if job.Finished && now.Sub(job.EndTime) > fs.Config.RcJobExpireDuration {
|
2018-10-26 21:48:22 +08:00
|
|
|
delete(jobs.jobs, ID)
|
|
|
|
}
|
|
|
|
job.mu.Unlock()
|
|
|
|
}
|
|
|
|
if len(jobs.jobs) != 0 {
|
|
|
|
time.AfterFunc(jobs.expireInterval, jobs.Expire)
|
|
|
|
jobs.expireRunning = true
|
|
|
|
} else {
|
|
|
|
jobs.expireRunning = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IDs returns the IDs of the running jobs
|
|
|
|
func (jobs *Jobs) IDs() (IDs []int64) {
|
|
|
|
jobs.mu.RLock()
|
|
|
|
defer jobs.mu.RUnlock()
|
|
|
|
IDs = []int64{}
|
|
|
|
for ID := range jobs.jobs {
|
|
|
|
IDs = append(IDs, ID)
|
|
|
|
}
|
|
|
|
return IDs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a job with a given ID or nil if it doesn't exist
|
|
|
|
func (jobs *Jobs) Get(ID int64) *Job {
|
|
|
|
jobs.mu.RLock()
|
|
|
|
defer jobs.mu.RUnlock()
|
|
|
|
return jobs.jobs[ID]
|
|
|
|
}
|
|
|
|
|
|
|
|
// mark the job as finished
|
2019-07-18 18:13:54 +08:00
|
|
|
func (job *Job) finish(out rc.Params, err error) {
|
2018-10-26 21:48:22 +08:00
|
|
|
job.mu.Lock()
|
|
|
|
job.EndTime = time.Now()
|
|
|
|
if out == nil {
|
2019-07-18 18:13:54 +08:00
|
|
|
out = make(rc.Params)
|
2018-10-26 21:48:22 +08:00
|
|
|
}
|
|
|
|
job.Output = out
|
|
|
|
job.Duration = job.EndTime.Sub(job.StartTime).Seconds()
|
|
|
|
if err != nil {
|
|
|
|
job.Error = err.Error()
|
|
|
|
job.Success = false
|
|
|
|
} else {
|
|
|
|
job.Error = ""
|
|
|
|
job.Success = true
|
|
|
|
}
|
|
|
|
job.Finished = true
|
|
|
|
job.mu.Unlock()
|
|
|
|
running.kickExpire() // make sure this job gets expired
|
|
|
|
}
|
|
|
|
|
|
|
|
// run the job until completion writing the return status
|
2019-07-18 18:13:54 +08:00
|
|
|
func (job *Job) run(ctx context.Context, fn rc.Func, in rc.Params) {
|
2018-10-26 21:48:22 +08:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2019-07-30 19:22:06 +08:00
|
|
|
job.finish(nil, errors.Errorf("panic received: %v \n%s", r, string(debug.Stack())))
|
2018-10-26 21:48:22 +08:00
|
|
|
}
|
|
|
|
}()
|
2019-06-17 16:47:29 +08:00
|
|
|
job.finish(fn(ctx, in))
|
2018-10-26 21:48:22 +08:00
|
|
|
}
|
|
|
|
|
2019-07-18 18:13:54 +08:00
|
|
|
func getGroup(in rc.Params) string {
|
|
|
|
// Check to see if the group is set
|
|
|
|
group, err := in.GetString("_group")
|
|
|
|
if rc.NotErrParamNotFound(err) {
|
|
|
|
fs.Errorf(nil, "Can't get _group param %+v", err)
|
|
|
|
}
|
|
|
|
delete(in, "_group")
|
|
|
|
return group
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAsyncJob start a new asynchronous Job off
|
|
|
|
func (jobs *Jobs) NewAsyncJob(fn rc.Func, in rc.Params) *Job {
|
|
|
|
id := atomic.AddInt64(&jobID, 1)
|
|
|
|
|
|
|
|
group := getGroup(in)
|
|
|
|
if group == "" {
|
|
|
|
group = fmt.Sprintf("job/%d", id)
|
|
|
|
}
|
|
|
|
ctx := accounting.WithStatsGroup(context.Background(), group)
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2019-06-17 16:47:29 +08:00
|
|
|
stop := func() {
|
|
|
|
cancel()
|
|
|
|
// Wait for cancel to propagate before returning.
|
|
|
|
<-ctx.Done()
|
|
|
|
}
|
2018-10-26 21:48:22 +08:00
|
|
|
job := &Job{
|
2019-07-18 18:13:54 +08:00
|
|
|
ID: id,
|
|
|
|
Group: group,
|
2018-10-26 21:48:22 +08:00
|
|
|
StartTime: time.Now(),
|
2019-06-17 16:47:29 +08:00
|
|
|
Stop: stop,
|
2018-10-26 21:48:22 +08:00
|
|
|
}
|
|
|
|
jobs.mu.Lock()
|
|
|
|
jobs.jobs[job.ID] = job
|
|
|
|
jobs.mu.Unlock()
|
2019-07-18 18:13:54 +08:00
|
|
|
go job.run(ctx, fn, in)
|
2018-10-26 21:48:22 +08:00
|
|
|
return job
|
2019-07-18 18:13:54 +08:00
|
|
|
}
|
2018-10-26 21:48:22 +08:00
|
|
|
|
2019-07-18 18:13:54 +08:00
|
|
|
// NewSyncJob start a new synchronous Job off
|
|
|
|
func (jobs *Jobs) NewSyncJob(ctx context.Context, in rc.Params) (*Job, context.Context) {
|
|
|
|
id := atomic.AddInt64(&jobID, 1)
|
|
|
|
group := getGroup(in)
|
|
|
|
if group == "" {
|
|
|
|
group = fmt.Sprintf("job/%d", id)
|
|
|
|
}
|
|
|
|
ctxG := accounting.WithStatsGroup(ctx, fmt.Sprintf("job/%d", id))
|
|
|
|
ctx, cancel := context.WithCancel(ctxG)
|
|
|
|
stop := func() {
|
|
|
|
cancel()
|
|
|
|
// Wait for cancel to propagate before returning.
|
|
|
|
<-ctx.Done()
|
|
|
|
}
|
|
|
|
job := &Job{
|
|
|
|
ID: id,
|
|
|
|
Group: group,
|
|
|
|
StartTime: time.Now(),
|
|
|
|
Stop: stop,
|
|
|
|
}
|
|
|
|
jobs.mu.Lock()
|
|
|
|
jobs.jobs[job.ID] = job
|
|
|
|
jobs.mu.Unlock()
|
|
|
|
return job, ctx
|
2018-10-26 21:48:22 +08:00
|
|
|
}
|
|
|
|
|
2019-07-18 18:13:54 +08:00
|
|
|
// StartAsyncJob starts a new job asynchronously and returns a Param suitable
|
|
|
|
// for output.
|
|
|
|
func StartAsyncJob(fn rc.Func, in rc.Params) (rc.Params, error) {
|
|
|
|
job := running.NewAsyncJob(fn, in)
|
|
|
|
out := make(rc.Params)
|
2018-10-26 21:48:22 +08:00
|
|
|
out["jobid"] = job.ID
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2019-07-18 18:13:54 +08:00
|
|
|
// ExecuteJob executes new job synchronously and returns a Param suitable for
|
|
|
|
// output.
|
|
|
|
func ExecuteJob(ctx context.Context, fn rc.Func, in rc.Params) (rc.Params, int64, error) {
|
|
|
|
job, ctx := running.NewSyncJob(ctx, in)
|
|
|
|
job.run(ctx, fn, in)
|
|
|
|
var err error
|
|
|
|
if !job.Success {
|
|
|
|
err = errors.New(job.Error)
|
|
|
|
}
|
|
|
|
return job.Output, job.ID, err
|
|
|
|
}
|
|
|
|
|
2018-10-26 21:48:22 +08:00
|
|
|
func init() {
|
2019-07-18 18:13:54 +08:00
|
|
|
rc.Add(rc.Call{
|
2018-10-26 21:48:22 +08:00
|
|
|
Path: "job/status",
|
|
|
|
Fn: rcJobStatus,
|
|
|
|
Title: "Reads the status of the job ID",
|
|
|
|
Help: `Parameters
|
|
|
|
- jobid - id of the job (integer)
|
|
|
|
|
|
|
|
Results
|
|
|
|
- finished - boolean
|
|
|
|
- duration - time in seconds that the job ran for
|
|
|
|
- endTime - time the job finished (eg "2018-10-26T18:50:20.528746884+01:00")
|
|
|
|
- error - error from the job or empty string for no error
|
|
|
|
- finished - boolean whether the job has finished or not
|
|
|
|
- id - as passed in above
|
|
|
|
- startTime - time the job started (eg "2018-10-26T18:50:20.528336039+01:00")
|
|
|
|
- success - boolean - true for success false otherwise
|
|
|
|
- output - output of the job as would have been returned if called synchronously
|
2019-06-17 16:34:30 +08:00
|
|
|
- progress - output of the progress related to the underlying job
|
2018-10-26 21:48:22 +08:00
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the status of a job
|
2019-07-18 18:13:54 +08:00
|
|
|
func rcJobStatus(ctx context.Context, in rc.Params) (out rc.Params, err error) {
|
2018-10-26 21:48:22 +08:00
|
|
|
jobID, err := in.GetInt64("jobid")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
job := running.Get(jobID)
|
|
|
|
if job == nil {
|
|
|
|
return nil, errors.New("job not found")
|
|
|
|
}
|
|
|
|
job.mu.Lock()
|
|
|
|
defer job.mu.Unlock()
|
2019-07-18 18:13:54 +08:00
|
|
|
out = make(rc.Params)
|
|
|
|
err = rc.Reshape(&out, job)
|
2019-03-18 19:23:00 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "reshape failed in job status")
|
2018-10-26 21:48:22 +08:00
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2019-07-18 18:13:54 +08:00
|
|
|
rc.Add(rc.Call{
|
2018-10-26 21:48:22 +08:00
|
|
|
Path: "job/list",
|
|
|
|
Fn: rcJobList,
|
|
|
|
Title: "Lists the IDs of the running jobs",
|
|
|
|
Help: `Parameters - None
|
|
|
|
|
|
|
|
Results
|
|
|
|
- jobids - array of integer job ids
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-17 16:47:29 +08:00
|
|
|
// Returns list of job ids.
|
2019-07-18 18:13:54 +08:00
|
|
|
func rcJobList(ctx context.Context, in rc.Params) (out rc.Params, err error) {
|
|
|
|
out = make(rc.Params)
|
2018-10-26 21:48:22 +08:00
|
|
|
out["jobids"] = running.IDs()
|
|
|
|
return out, nil
|
|
|
|
}
|
2019-06-17 16:47:29 +08:00
|
|
|
|
|
|
|
func init() {
|
2019-07-18 18:13:54 +08:00
|
|
|
rc.Add(rc.Call{
|
2019-06-17 16:47:29 +08:00
|
|
|
Path: "job/stop",
|
|
|
|
Fn: rcJobStop,
|
|
|
|
Title: "Stop the running job",
|
|
|
|
Help: `Parameters
|
|
|
|
- jobid - id of the job (integer)
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stops the running job.
|
2019-07-18 18:13:54 +08:00
|
|
|
func rcJobStop(ctx context.Context, in rc.Params) (out rc.Params, err error) {
|
2019-06-17 16:47:29 +08:00
|
|
|
jobID, err := in.GetInt64("jobid")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
job := running.Get(jobID)
|
|
|
|
if job == nil {
|
|
|
|
return nil, errors.New("job not found")
|
|
|
|
}
|
|
|
|
job.mu.Lock()
|
|
|
|
defer job.mu.Unlock()
|
2019-07-18 18:13:54 +08:00
|
|
|
out = make(rc.Params)
|
2019-06-17 16:47:29 +08:00
|
|
|
job.Stop()
|
|
|
|
return out, nil
|
|
|
|
}
|