2022-08-28 19:21:57 +08:00
|
|
|
// Package jobs manages 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"
|
2021-11-04 18:12:57 +08:00
|
|
|
"errors"
|
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"
|
|
|
|
|
2023-06-29 00:06:36 +08:00
|
|
|
"github.com/google/uuid"
|
2020-12-13 01:40:29 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
2019-08-21 18:01:39 +08:00
|
|
|
"github.com/rclone/rclone/fs/accounting"
|
2023-06-11 22:25:14 +08:00
|
|
|
"github.com/rclone/rclone/fs/cache"
|
2021-02-23 18:34:58 +08:00
|
|
|
"github.com/rclone/rclone/fs/filter"
|
2019-08-21 18:01:39 +08:00
|
|
|
"github.com/rclone/rclone/fs/rc"
|
2018-10-26 21:48:22 +08:00
|
|
|
)
|
|
|
|
|
2023-06-11 22:25:14 +08:00
|
|
|
// Fill in these to avoid circular dependencies
|
|
|
|
func init() {
|
|
|
|
cache.JobOnFinish = OnFinish
|
|
|
|
cache.JobGetJobID = GetJobID
|
|
|
|
}
|
|
|
|
|
2020-05-20 18:39:20 +08:00
|
|
|
// Job describes an asynchronous task started via the rc package
|
2018-10-26 21:48:22 +08:00
|
|
|
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:"-"`
|
2020-01-23 19:44:56 +08:00
|
|
|
listeners []*func()
|
2019-08-21 20:50:18 +08:00
|
|
|
|
|
|
|
// realErr is the Error before printing it as a string, it's used to return
|
|
|
|
// the real error to the upper application layers while still printing the
|
|
|
|
// string error message.
|
|
|
|
realErr error
|
2018-10-26 21:48:22 +08:00
|
|
|
}
|
|
|
|
|
2020-01-23 19:44:56 +08:00
|
|
|
// mark the job as finished
|
|
|
|
func (job *Job) finish(out rc.Params, err error) {
|
|
|
|
job.mu.Lock()
|
|
|
|
job.EndTime = time.Now()
|
|
|
|
if out == nil {
|
|
|
|
out = make(rc.Params)
|
|
|
|
}
|
|
|
|
job.Output = out
|
|
|
|
job.Duration = job.EndTime.Sub(job.StartTime).Seconds()
|
|
|
|
if err != nil {
|
|
|
|
job.realErr = err
|
|
|
|
job.Error = err.Error()
|
|
|
|
job.Success = false
|
|
|
|
} else {
|
|
|
|
job.realErr = nil
|
|
|
|
job.Error = ""
|
|
|
|
job.Success = true
|
|
|
|
}
|
|
|
|
job.Finished = true
|
|
|
|
|
|
|
|
// Notify listeners that the job is finished
|
|
|
|
for i := range job.listeners {
|
|
|
|
go (*job.listeners[i])()
|
|
|
|
}
|
|
|
|
|
|
|
|
job.mu.Unlock()
|
|
|
|
running.kickExpire() // make sure this job gets expired
|
|
|
|
}
|
|
|
|
|
|
|
|
func (job *Job) addListener(fn *func()) {
|
|
|
|
job.mu.Lock()
|
|
|
|
defer job.mu.Unlock()
|
|
|
|
job.listeners = append(job.listeners, fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (job *Job) removeListener(fn *func()) {
|
|
|
|
job.mu.Lock()
|
|
|
|
defer job.mu.Unlock()
|
|
|
|
for i, ln := range job.listeners {
|
|
|
|
if ln == fn {
|
|
|
|
job.listeners = append(job.listeners[:i], job.listeners[i+1:]...)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-11 22:23:39 +08:00
|
|
|
// OnFinish adds listener to job that will be triggered when job is finished.
|
|
|
|
// It returns a function to cancel listening.
|
|
|
|
func (job *Job) OnFinish(fn func()) func() {
|
|
|
|
if job.Finished {
|
|
|
|
fn()
|
|
|
|
} else {
|
|
|
|
job.addListener(&fn)
|
|
|
|
}
|
|
|
|
return func() { job.removeListener(&fn) }
|
|
|
|
}
|
|
|
|
|
2020-01-23 19:44:56 +08:00
|
|
|
// run the job until completion writing the return status
|
|
|
|
func (job *Job) run(ctx context.Context, fn rc.Func, in rc.Params) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
job.finish(nil, fmt.Errorf("panic received: %v \n%s", r, string(debug.Stack())))
|
2020-01-23 19:44:56 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
job.finish(fn(ctx, in))
|
|
|
|
}
|
|
|
|
|
2018-10-26 21:48:22 +08:00
|
|
|
// Jobs describes a collection of running tasks
|
|
|
|
type Jobs struct {
|
2019-08-11 00:12:22 +08:00
|
|
|
mu sync.RWMutex
|
|
|
|
jobs map[int64]*Job
|
|
|
|
opt *rc.Options
|
|
|
|
expireRunning bool
|
2018-10-26 21:48:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2023-06-29 00:06:36 +08:00
|
|
|
running = newJobs()
|
2023-08-18 22:56:26 +08:00
|
|
|
jobID atomic.Int64
|
2023-06-29 00:06:36 +08:00
|
|
|
executeID = uuid.New().String()
|
2018-10-26 21:48:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// newJobs makes a new Jobs structure
|
|
|
|
func newJobs() *Jobs {
|
|
|
|
return &Jobs{
|
2019-08-11 00:12:22 +08:00
|
|
|
jobs: map[int64]*Job{},
|
|
|
|
opt: &rc.DefaultOpt,
|
2018-10-26 21:48:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-11 00:12:22 +08:00
|
|
|
// SetOpt sets the options when they are known
|
|
|
|
func SetOpt(opt *rc.Options) {
|
|
|
|
running.opt = opt
|
|
|
|
}
|
|
|
|
|
2019-08-21 18:01:39 +08:00
|
|
|
// SetInitialJobID allows for setting jobID before starting any jobs.
|
|
|
|
func SetInitialJobID(id int64) {
|
2023-08-18 22:56:26 +08:00
|
|
|
if !jobID.CompareAndSwap(0, id) {
|
2019-08-21 18:01:39 +08:00
|
|
|
panic("Setting jobID is only possible before starting any jobs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-08-11 00:12:22 +08:00
|
|
|
time.AfterFunc(jobs.opt.JobExpireInterval, jobs.Expire)
|
2018-10-26 21:48:22 +08:00
|
|
|
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-08-11 00:12:22 +08:00
|
|
|
if job.Finished && now.Sub(job.EndTime) > jobs.opt.JobExpireDuration {
|
2018-10-26 21:48:22 +08:00
|
|
|
delete(jobs.jobs, ID)
|
|
|
|
}
|
|
|
|
job.mu.Unlock()
|
|
|
|
}
|
|
|
|
if len(jobs.jobs) != 0 {
|
2019-08-11 00:12:22 +08:00
|
|
|
time.AfterFunc(jobs.opt.JobExpireInterval, jobs.Expire)
|
2018-10-26 21:48:22 +08:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2020-12-12 23:35:30 +08:00
|
|
|
// Check to see if the group is set
|
|
|
|
func getGroup(ctx context.Context, in rc.Params, id int64) (context.Context, string, error) {
|
2019-07-18 18:13:54 +08:00
|
|
|
group, err := in.GetString("_group")
|
|
|
|
if rc.NotErrParamNotFound(err) {
|
2020-12-12 23:35:30 +08:00
|
|
|
return ctx, "", err
|
2019-07-18 18:13:54 +08:00
|
|
|
}
|
|
|
|
delete(in, "_group")
|
2020-12-12 23:35:30 +08:00
|
|
|
if group == "" {
|
|
|
|
group = fmt.Sprintf("job/%d", id)
|
|
|
|
}
|
|
|
|
ctx = accounting.WithStatsGroup(ctx, group)
|
|
|
|
return ctx, group, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if _async is set returning a boolean and a possible new context
|
|
|
|
func getAsync(ctx context.Context, in rc.Params) (context.Context, bool, error) {
|
|
|
|
isAsync, err := in.GetBool("_async")
|
|
|
|
if rc.NotErrParamNotFound(err) {
|
|
|
|
return ctx, false, err
|
|
|
|
}
|
|
|
|
delete(in, "_async") // remove the async parameter after parsing
|
|
|
|
if isAsync {
|
|
|
|
// unlink this job from the current context
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
|
|
|
return ctx, isAsync, nil
|
2019-07-18 18:13:54 +08:00
|
|
|
}
|
|
|
|
|
2020-12-13 01:40:29 +08:00
|
|
|
// See if _config is set and if so adjust ctx to include it
|
|
|
|
func getConfig(ctx context.Context, in rc.Params) (context.Context, error) {
|
|
|
|
if _, ok := in["_config"]; !ok {
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
ctx, ci := fs.AddConfig(ctx)
|
|
|
|
err := in.GetStruct("_config", ci)
|
|
|
|
if err != nil {
|
|
|
|
return ctx, err
|
|
|
|
}
|
|
|
|
delete(in, "_config") // remove the parameter
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
|
2021-02-23 18:34:58 +08:00
|
|
|
// See if _filter is set and if so adjust ctx to include it
|
|
|
|
func getFilter(ctx context.Context, in rc.Params) (context.Context, error) {
|
|
|
|
if _, ok := in["_filter"]; !ok {
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
// Copy of the current filter options
|
|
|
|
opt := filter.GetConfig(ctx).Opt
|
|
|
|
// Update the options from the parameter
|
|
|
|
err := in.GetStruct("_filter", &opt)
|
|
|
|
if err != nil {
|
|
|
|
return ctx, err
|
|
|
|
}
|
|
|
|
fi, err := filter.NewFilter(&opt)
|
|
|
|
if err != nil {
|
|
|
|
return ctx, err
|
|
|
|
}
|
|
|
|
ctx = filter.ReplaceConfig(ctx, fi)
|
|
|
|
delete(in, "_filter") // remove the parameter
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
|
2023-06-11 22:23:39 +08:00
|
|
|
type jobKeyType struct{}
|
|
|
|
|
|
|
|
// Key for adding jobs to ctx
|
|
|
|
var jobKey = jobKeyType{}
|
|
|
|
|
2020-12-12 23:35:30 +08:00
|
|
|
// NewJob creates a Job and executes it, possibly in the background if _async is set
|
|
|
|
func (jobs *Jobs) NewJob(ctx context.Context, fn rc.Func, in rc.Params) (job *Job, out rc.Params, err error) {
|
2023-08-18 22:56:26 +08:00
|
|
|
id := jobID.Add(1)
|
2020-12-12 23:35:30 +08:00
|
|
|
in = in.Copy() // copy input so we can change it
|
2019-07-18 18:13:54 +08:00
|
|
|
|
2020-12-12 23:35:30 +08:00
|
|
|
ctx, isAsync, err := getAsync(ctx, in)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2019-07-18 18:13:54 +08:00
|
|
|
}
|
2020-12-12 23:35:30 +08:00
|
|
|
|
2020-12-13 01:40:29 +08:00
|
|
|
ctx, err = getConfig(ctx, in)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-23 18:34:58 +08:00
|
|
|
ctx, err = getFilter(ctx, in)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-12 23:35:30 +08:00
|
|
|
ctx, group, err := getGroup(ctx, in, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-18 18:13:54 +08:00
|
|
|
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()
|
|
|
|
}
|
2020-12-12 23:35:30 +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
|
|
|
}
|
2023-06-11 22:23:39 +08:00
|
|
|
|
2018-10-26 21:48:22 +08:00
|
|
|
jobs.mu.Lock()
|
|
|
|
jobs.jobs[job.ID] = job
|
|
|
|
jobs.mu.Unlock()
|
2023-06-11 22:23:39 +08:00
|
|
|
|
|
|
|
// Add the job to the context
|
|
|
|
ctx = context.WithValue(ctx, jobKey, job)
|
|
|
|
|
2020-12-12 23:35:30 +08:00
|
|
|
if isAsync {
|
|
|
|
go job.run(ctx, fn, in)
|
|
|
|
out = make(rc.Params)
|
|
|
|
out["jobid"] = job.ID
|
|
|
|
err = nil
|
|
|
|
} else {
|
|
|
|
job.run(ctx, fn, in)
|
|
|
|
out = job.Output
|
|
|
|
err = job.realErr
|
|
|
|
}
|
|
|
|
return job, out, err
|
2018-10-26 21:48:22 +08:00
|
|
|
}
|
|
|
|
|
2020-12-12 23:35:30 +08:00
|
|
|
// NewJob creates a Job and executes it on the global job queue,
|
|
|
|
// possibly in the background if _async is set
|
|
|
|
func NewJob(ctx context.Context, fn rc.Func, in rc.Params) (job *Job, out rc.Params, err error) {
|
|
|
|
return running.NewJob(ctx, fn, in)
|
2019-07-18 18:13:54 +08:00
|
|
|
}
|
|
|
|
|
2020-01-23 19:44:56 +08:00
|
|
|
// OnFinish adds listener to jobid that will be triggered when job is finished.
|
|
|
|
// It returns a function to cancel listening.
|
|
|
|
func OnFinish(jobID int64, fn func()) (func(), error) {
|
|
|
|
job := running.Get(jobID)
|
|
|
|
if job == nil {
|
|
|
|
return func() {}, errors.New("job not found")
|
|
|
|
}
|
2023-06-11 22:23:39 +08:00
|
|
|
return job.OnFinish(fn), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetJob gets the Job from the context if possible
|
|
|
|
func GetJob(ctx context.Context) (job *Job, ok bool) {
|
|
|
|
job, ok = ctx.Value(jobKey).(*Job)
|
|
|
|
return job, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetJobID gets the Job from the context if possible
|
|
|
|
func GetJobID(ctx context.Context) (jobID int64, ok bool) {
|
|
|
|
job, ok := GetJob(ctx)
|
|
|
|
if !ok {
|
|
|
|
return -1, ok
|
2020-01-23 19:44:56 +08:00
|
|
|
}
|
2023-06-11 22:23:39 +08:00
|
|
|
return job.ID, true
|
2020-01-23 19:44:56 +08:00
|
|
|
}
|
|
|
|
|
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",
|
2021-08-16 17:30:01 +08:00
|
|
|
Help: `Parameters:
|
2019-10-27 18:32:56 +08:00
|
|
|
|
2021-08-16 17:30:01 +08:00
|
|
|
- jobid - id of the job (integer).
|
2018-10-26 21:48:22 +08:00
|
|
|
|
2021-08-16 17:30:01 +08:00
|
|
|
Results:
|
2019-10-27 18:32:56 +08:00
|
|
|
|
2018-10-26 21:48:22 +08:00
|
|
|
- finished - boolean
|
|
|
|
- duration - time in seconds that the job ran for
|
2020-10-14 05:49:58 +08:00
|
|
|
- endTime - time the job finished (e.g. "2018-10-26T18:50:20.528746884+01:00")
|
2018-10-26 21:48:22 +08: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
|
2020-10-14 05:49:58 +08:00
|
|
|
- startTime - time the job started (e.g. "2018-10-26T18:50:20.528336039+01:00")
|
2018-10-26 21:48:22 +08: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 {
|
2021-11-04 18:12:57 +08:00
|
|
|
return nil, fmt.Errorf("reshape failed in job status: %w", err)
|
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",
|
2021-08-16 17:30:01 +08:00
|
|
|
Help: `Parameters: None.
|
2018-10-26 21:48:22 +08:00
|
|
|
|
2021-08-16 17:30:01 +08:00
|
|
|
Results:
|
2019-10-27 18:32:56 +08:00
|
|
|
|
2023-06-29 00:06:36 +08:00
|
|
|
- executeId - string id of rclone executing (change after restart)
|
|
|
|
- jobids - array of integer job ids (starting at 1 on each restart)
|
2018-10-26 21:48:22 +08:00
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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()
|
2023-06-29 00:06:36 +08:00
|
|
|
out["executeId"] = executeID
|
2018-10-26 21:48:22 +08:00
|
|
|
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",
|
2021-08-16 17:30:01 +08:00
|
|
|
Help: `Parameters:
|
2019-10-27 18:32:56 +08:00
|
|
|
|
2021-08-16 17:30:01 +08:00
|
|
|
- jobid - id of the job (integer).
|
2019-06-17 16:47:29 +08:00
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2022-07-10 22:42:04 +08:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
rc.Add(rc.Call{
|
|
|
|
Path: "job/stopgroup",
|
|
|
|
Fn: rcGroupStop,
|
|
|
|
Title: "Stop all running jobs in a group",
|
|
|
|
Help: `Parameters:
|
|
|
|
|
|
|
|
- group - name of the group (string).
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stops all running jobs in a group
|
|
|
|
func rcGroupStop(ctx context.Context, in rc.Params) (out rc.Params, err error) {
|
|
|
|
group, err := in.GetString("group")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
running.mu.RLock()
|
|
|
|
defer running.mu.RUnlock()
|
|
|
|
for _, job := range running.jobs {
|
|
|
|
if job.Group == group {
|
|
|
|
job.mu.Lock()
|
|
|
|
job.Stop()
|
|
|
|
job.mu.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out = make(rc.Params)
|
|
|
|
return out, nil
|
|
|
|
}
|