public virtual async Task <InvocationResult> Dispatch(InvocationContext context) { JobDescription jobdef; if (!_jobMap.TryGetValue(context.Invocation.Job, out jobdef)) { throw new UnknownJobException(context.Invocation.Job); } _currentJob = jobdef; ILifetimeScope scope = null; using (scope = _container.BeginLifetimeScope(b => { b.RegisterType(jobdef.Implementation).As(jobdef.Implementation); b.RegisterInstance(context).As <InvocationContext>(); b.Register(ctx => scope) .As <ILifetimeScope>(); })) { var job = (JobHandlerBase)scope.Resolve(jobdef.Implementation); Func <Task <InvocationResult> > invocationThunk = () => job.Invoke(context); if (context.Invocation.IsContinuation) { IAsyncJob asyncJob = job as IAsyncJob; if (asyncJob == null) { // Just going to be caught below, but that's what we want :). throw new InvalidOperationException(String.Format( CultureInfo.CurrentCulture, Strings.JobDispatcher_AsyncContinuationOfNonAsyncJob, jobdef.Name)); } invocationThunk = () => asyncJob.InvokeContinuation(context); } InvocationEventSource.Log.Invoking(jobdef); InvocationResult result = null; try { context.SetJob(jobdef, job); result = await invocationThunk(); } catch (Exception ex) { result = InvocationResult.Faulted(ex); } _currentJob = null; _lastJob = jobdef; return(result); } }
protected internal override async Task <InvocationResult> Invoke() { try { await Execute(); return(InvocationResult.Completed()); } catch (Exception ex) { return(InvocationResult.Faulted(ex)); } }
protected internal override async Task <InvocationResult> Invoke() { // Invoke the job. When it returns, it means there's no more data to process // So go to sleep until the wait period elapses try { await Execute(); return(InvocationResult.Completed(WaitPeriod)); } catch (Exception ex) { return(InvocationResult.Faulted(ex, WaitPeriod)); } }
protected virtual InvocationResult BindContext(InvocationContext context) { // Bind invocation information Context = context; TempDirectory = Path.Combine( Path.GetTempPath(), "NuGetService", "InvocationTemp", context.Invocation.Id.ToString("N").ToLowerInvariant()); try { BindProperties(Invocation.Payload); } catch (Exception ex) { InvocationEventSource.Log.BindingError(ex); return(InvocationResult.Faulted(ex)); } return(null); }
private async Task <InvocationResult> InvokeCore(Func <Task <JobContinuation> > invoker) { try { var continuation = await invoker(); if (continuation != null) { return(InvocationResult.Suspended(continuation)); } else { return(InvocationResult.Completed()); } } catch (Exception ex) { return(InvocationResult.Faulted(ex)); } }
public virtual async Task <InvocationResult> Invoke(InvocationContext context) { InvocationResult result = BindContext(context); if (result != null) { return(result); } // Invoke the job try { result = await Invoke(); } catch (Exception ex) { result = InvocationResult.Faulted(ex); } // Return the result return(result); }
protected internal override Task <InvocationResult> Invoke() { return(Task.FromResult(InvocationResult.Faulted(new Exception("Supposed to be invoked as a continuation!")))); }