public void SetJob(JobDescription jobdef, JobHandlerBase job)
 {
     if (_capture != null)
     {
         _capture.SetJob(jobdef, job);   
     }
 }
 public WorkServiceStatus(RunnerStatus runnerStatus, Guid currentInvocationId, Guid lastInvocationId, JobDescription currentJob, JobDescription lastJob, Exception error)
 {
     RunnerStatus = runnerStatus;
     CurrentInvocationId = currentInvocationId;
     LastInvocationId = lastInvocationId;
     CurrentJob = currentJob;
     LastJob = lastJob;
     Error = error;
 }
        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;
            }
        }
            public async Task GivenJobWithName_ItInvokesTheJobAndReturnsTheResult()
            {
                // Arrange
                var host = new TestServiceHost();
                host.Initialize();

                var job = new JobDescription("test", typeof(TestJob));

                var dispatcher = new JobDispatcher(new[] { job }, host.Container);
                var invocation = TestHelpers.CreateInvocation(Guid.NewGuid(), "Test", "test", new Dictionary<string, string>());
                var context = new InvocationContext(invocation, queue: null);
                var expected = InvocationResult.Completed();
                TestJob.SetTestResult(expected);

                // Act
                var actual = await dispatcher.Dispatch(context);

                // Assert
                Assert.Same(expected, actual);
            }
            public async Task GivenJobWithConstructorArgs_ItResolvesThemFromTheContainer()
            {
                // Arrange
                var expected = new SomeService();
                var host = new TestServiceHost(
                    componentRegistrations: b => {
                        b.RegisterInstance(expected).As<SomeService>();
                    });
                host.Initialize();

                var job = new JobDescription("test", typeof(TestJobWithService));

                var dispatcher = new JobDispatcher(new[] { job }, host.Container);
                var invocation = TestHelpers.CreateInvocation(Guid.NewGuid(), "Test", "test", new Dictionary<string, string>());
                var context = new InvocationContext(invocation, queue: null);
                var slot = new ContextSlot();
                TestJobWithService.SetContextSlot(slot);
                
                // Act
                await dispatcher.Dispatch(context);
                
                // Assert
                Assert.Same(expected, slot.Value);
            }
Beispiel #6
0
 public WorkServiceStatus(RunnerStatus runnerStatus, Guid currentInvocationId, Guid lastInvocationId, JobDescription currentJob, JobDescription lastJob, Exception error)
 {
     RunnerStatus        = runnerStatus;
     CurrentInvocationId = currentInvocationId;
     LastInvocationId    = lastInvocationId;
     CurrentJob          = currentJob;
     LastJob             = lastJob;
     Error = error;
 }
Beispiel #7
0
 public void GivenAJobWithAttribute_ItReturnsTheNameFromTheAttribute()
 {
     Assert.Equal("ATestJob", JobDescription.Create(typeof(ATestJorb)).Name);
 }
Beispiel #8
0
 public void GivenAJobWithClassNameNotEndingJob_ItReturnsTheWholeTypeName()
 {
     Assert.Equal("ATestJerb", JobDescription.Create(typeof(ATestJerb)).Name);
 }
Beispiel #9
0
 public void GivenAJobWithClassNameEndingJob_ItReturnsThePartBeforeTheWordJob()
 {
     Assert.Equal("ATest", JobDescription.Create(typeof(ATestJob)).Name);
 }
            public async Task GivenAContinuationRequest_ItCallsTheInvokeContinuationMethod()
            {
                // Arrange
                var host = new TestServiceHost();
                host.Initialize();

                var job = new JobDescription("test", typeof(TestAsyncJob));

                var dispatcher = new JobDispatcher(new[] { job }, host.Container);
                var invocation = TestHelpers.CreateInvocation(Guid.NewGuid(), "Test", "test", new Dictionary<string, string>(), isContinuation: true);
                var context = new InvocationContext(invocation, queue: null);
                
                // Act
                var result = await dispatcher.Dispatch(context);

                // Assert
                Assert.Equal(ExecutionResult.Completed, result.Result);
            }
            public async Task GivenAContinuationRequestToANonAsyncJob_ItThrows()
            {
                // Arrange
                var host = new TestServiceHost();
                 host.Initialize();

                var job = new JobDescription("test", typeof(TestJob));

                var dispatcher = new JobDispatcher(new[] { job }, host.Container);
                var invocation = TestHelpers.CreateInvocation(Guid.NewGuid(), "Test", "test", new Dictionary<string, string>(), isContinuation: true);
                var context = new InvocationContext(invocation, queue: null);

                // Act/Assert
                var ex = await AssertEx.Throws<InvalidOperationException>(() => dispatcher.Dispatch(context));
                Assert.Equal(String.Format(
                    Strings.JobDispatcher_AsyncContinuationOfNonAsyncJob,
                    job.Name), ex.Message);
            }