func (js *JobScheduler) RunAsyncContext(ctx context.Context) (*JobInvocation, <-chan struct{}, error) {
	if !js.IsIdle() {
		return nil, nil, ex.New(ErrJobAlreadyRunning, ex.OptMessagef("job: %s", js.Name()))
	}

	ctx = js.withBaseContext(ctx)
	ctx, ji := js.withInvocationContext(ctx)
	done := make(chan struct{})
	js.SetCurrent(ji)

	var err error
	var tracer TraceFinisher
	go func() {
		defer func() {
			switch {
			case err != nil && IsJobCanceled(err):
				js.onJobCompleteCanceled(ctx) // the job was canceled, either manually or by a timeout
			case err != nil:
				js.onJobCompleteError(ctx, err) // the job completed with an error
			default:
				js.onJobCompleteSuccess(ctx) // the job completed without error
			}

			if tracer != nil {
				tracer.Finish(ctx, err) // call the trace finisher if one was started
			}
			ji.Cancel() // if the job was created with a timeout, end the timeout

			close(done)              // signal callers the job is done
			js.assignCurrentToLast() // rotate in the current to the last result
		}()

		if js.Tracer != nil {
			ctx, tracer = js.Tracer.Start(ctx, js.Name())
		}
		js.onJobBegin(ctx) // signal the job is starting

		select {
		case <-ctx.Done(): // if the timeout or cancel is triggered
			err = ErrJobCanceled // set the error to a known error
			return
		case err = <-js.safeBackgroundExec(ctx): // run the job in a background routine and catch pancis
			return
		}
	}()
	return ji, done, nil
}
// RunAsyncContext starts a job invocation with a given context.
RunAsyncContext starts a job invocation with a given context.
// RunAsyncContext starts a job invocation with a given context .
func (js *JobScheduler) RunAsyncContext(ctx context.Context) (*JobInvocation, <-chan struct{}, error) {
	if !js.IsIdle() {
		return nil, nil, ex.New(ErrJobAlreadyRunning, ex.OptMessagef("job: %s", js.Name()))
	}
	ctx = js.withBaseContext(ctx)
	ctx, ji := js.withInvocationContext(ctx)
	done := make(chan struct{})
	js.SetCurrent(ji)
	var err error
	var tracer TraceFinisher
	go func() {
		defer func() {
			switch {
			case err != nil && IsJobCanceled(err):
				js.onJobCompleteCanceled(ctx) 
			case err != nil:
				js.onJobCompleteError(ctx, err) 
			default:
				js.onJobCompleteSuccess(ctx) 
			}
			if tracer != nil {
				tracer.Finish(ctx, err) 
			}
			ji.Cancel() 
			close(done)              
			js.assignCurrentToLast() 
		}()
		if js.Tracer != nil {
			ctx, tracer = js.Tracer.Start(ctx, js.Name())
		}
		js.onJobBegin(ctx) 
		select {
		case <-ctx.Done(): 
			err = ErrJobCanceled 
			return
		case err = <-js.safeBackgroundExec(ctx): 
			return
		}
	}()
	return ji, done, nil
}
func ( js * JobScheduler ) RunAsyncContext ( ctx context . Context ) ( * JobInvocation , <- chan struct { } , error ) { if ! js . IsIdle ( ) { return nil , nil , ex . New ( ErrJobAlreadyRunning , ex . OptMessagef ( " " , js . Name ( ) ) ) } ctx = js . withBaseContext ( ctx ) ctx , ji := js . withInvocationContext ( ctx ) done := make ( chan struct { } ) js . SetCurrent ( ji ) var err error var tracer TraceFinisher go func ( ) { defer func ( ) { switch { case err != nil && IsJobCanceled ( err ) : js . onJobCompleteCanceled ( ctx ) case err != nil : js . onJobCompleteError ( ctx , err ) default : js . onJobCompleteSuccess ( ctx ) } if tracer != nil { tracer . Finish ( ctx , err ) } ji . Cancel ( ) close ( done ) js . assignCurrentToLast ( ) } ( ) if js . Tracer != nil { ctx , tracer = js . Tracer . Start ( ctx , js . Name ( ) ) } js . onJobBegin ( ctx ) select { case <- ctx . Done ( ) : err = ErrJobCanceled return case err = <- js . safeBackgroundExec ( ctx ) : return } } ( ) return ji , done , nil }
RunAsyncContext starts a job invocation with a given context.
RunAsyncContext starts a job invocation with a given context .
// the job was canceled, either manually or by a timeout // the job completed with an error // the job completed without error // call the trace finisher if one was started // if the job was created with a timeout, end the timeout // signal callers the job is done // rotate in the current to the last result // signal the job is starting // if the timeout or cancel is triggered // set the error to a known error // run the job in a background routine and catch pancis