Exemplo n.º 1
0
        private async Task TestImmediateJobs()
        {
            SetTime0();
            // Use session associated with particular user. Jobs are retried with the same user context as original attempt.
            var context = GetUserBoundContext();
            var session = context.OpenSecureSession();

            // We use this arg to test deserialization of arguments
            var listArg = new List <string>()
            {
                "a", "b", "c", "d", "e", "f"
            };
            // Set explicitly the default RetryPolicy
            var jobExecConfig = _app.GetConfig <JobModuleSettings>();

            jobExecConfig.DefaultRetryPolicy = new RetryPolicy(new[] { 2, 2, 2, 2, 2, 2 }); //repeat 6 times with 2 minute intervals

            JobRunContext jobRunContext;
            IJobRun       jobRun;

            // 1.a Async job executing successfully 1st time     -------------------------------------------
            jobRunContext = await JobHelper.ExecuteWithRetriesAsync(session.Context, "1a: Sample async job, no failures",
                                                                    (ctx) => JobMethodAsync(ctx, 0, "Some string argument", listArg));

            Assert.AreEqual(JobRunStatus.Completed, jobRunContext.Status, "Expected Completed for async job");
            // Check that job was not persisted - because it ended successfully first time
            jobRun = session.GetLastFinishedJobRun(jobRunContext.JobId);
            Assert.IsNull(jobRun, "Expected async job succeed and never be persisted.");

            // 1.b Async job initially failing     -------------------------------------------
            jobRunContext = await JobHelper.ExecuteWithRetriesAsync(session.Context, "1b: Sample async job, 3 failures",
                                                                    (ctx) => JobMethodAsync(ctx, 3, "Some string argument", listArg));

            Assert.AreEqual(JobRunStatus.Error, jobRunContext.Status, "Expected Error");
            // Check that job is persisted - because it ended with error
            jobRun = session.GetLastFinishedJobRun(jobRunContext.JobId);
            Assert.IsNotNull(jobRun, "Expected JobRun record in db.");
            //fast-forward time by 10 minutes and fire timers on the way;
            FastForwardTimeFireTimers(12);
            // RetryPolicy states to repeat with 2 minutes intervals,
            // so after 3 failures final run should succeed
            jobRun = session.GetLastFinishedJobRun(jobRunContext.JobId);
            Assert.AreEqual(JobRunStatus.Completed, jobRun.Status, "Expected status Completed");
            //attemptNumber is 1-based
            Assert.AreEqual(4, jobRun.AttemptNumber, "Wrong attempt number for successful run.");
            SetTime0();

            // 2.a NoWait job executing successfully 1st time     -------------------------------------------
            jobRunContext = JobHelper.ExecuteWithRetriesNoWait(session.Context, "2a: Sample no-wait job, no failures",
                                                               (ctx) => JobMethod(ctx, 0, "Some string argument", listArg));
            Thread.Sleep(100); //make sure job finished
            Assert.AreEqual(JobRunStatus.Completed, jobRunContext.Status, "Expected completed status");
            // Check that job was not persisted - because it ended successfully first time
            jobRun = session.GetLastFinishedJobRun(jobRunContext.JobId);
            Assert.IsNull(jobRun, "Expected no-wait job succeed and never be persisted.");

            // 2.b NoWait job failing initially     -------------------------------------------
            jobRunContext = JobHelper.ExecuteWithRetriesNoWait(session.Context, "2b: Sample no-wait job, 3 failures",
                                                               (ctx) => JobMethod(ctx, 3, "Some string argument", listArg));
            Thread.Sleep(100); //make sure job finished
            Assert.AreEqual(JobRunStatus.Error, jobRunContext.Status, "Expected Error status");
            // Check that job is persisted - because it failed
            jobRun = session.GetLastFinishedJobRun(jobRunContext.JobId);
            Assert.IsNotNull(jobRun, "Expected no-wait jobRun record in db.");
            //fast-forward time by 10 minutes;
            FastForwardTimeFireTimers(12);
            // RetryPolicy states to repeat with 2 minutes intervals,
            // so after 3 failures final run should succeed
            jobRun = session.GetLastFinishedJobRun(jobRunContext.JobId);
            Assert.AreEqual(JobRunStatus.Completed, jobRun.Status, "Expected status Completed");
            //attemptNumber is 1-based
            Assert.AreEqual(4, jobRun.AttemptNumber, "Wrong attempt number for successful run.");
            SetTime0();

            // 3.a Job executed on SaveChanges, executed successfully the first time ------------------------------------
            // This is useful in some cases - job executes ONLY if SaveChanges succeeds;
            // ex: we send confirmation email in the job, it will be sent only the whole thing succeeds.
            var dataId     = new Guid("79C01818-0E1E-4CA1-8D8A-B6E8E6A6897F");
            var data       = "Some Job Data";
            var threadType = JobThreadType.Background; // try changing it to pool, just for experiment

            jobRun = JobHelper.ScheduleJobRunOnSaveChanges(session, "3a: Sample on-save job, no failures",
                                                           (ctx) => JobMethod(ctx, 0, "Sample string arg", listArg), dataId, data, threadType);
            session.SaveChanges();
            Thread.Sleep(100); //make sure job finished
            // this should be the same as jobRun returned by StartJobOnSaveChanges
            var jobRun2 = jobRun.Job.GetLastFinishedJobRun();

            Assert.IsTrue(jobRun2 == jobRun, "Expected the same job run");
            Assert.AreEqual(JobRunStatus.Completed, jobRun.Status, "Expected Completed status.");

            // 3.b Job executed on SaveChanges, executed with initial failures ------------------------------------
            jobRun = JobHelper.ScheduleJobRunOnSaveChanges(session, "3b: Sample on-save job, 3 failures",
                                                           (ctx) => JobMethod(ctx, 3, "Sample string arg", listArg), dataId, data, threadType);
            session.SaveChanges();
            Thread.Sleep(100); //make sure job finished (with error)
            // get this failed run
            jobRun = jobRun.Job.GetLastFinishedJobRun();
            Assert.AreEqual(JobRunStatus.Error, jobRun.Status, "Expected Error status.");
            //fast-forward time by 10 minutes;
            FastForwardTimeFireTimers(12);
            jobRun = session.GetLastFinishedJobRun(jobRunContext.JobId);
            Assert.AreEqual(JobRunStatus.Completed, jobRun.Status, "Expected status Completed");
            //attemptNumber is 1-based
            Assert.AreEqual(4, jobRun.AttemptNumber, "Wrong attempt number for successful run.");
            SetTime0();
        } //method