public static async Task <Guid> AddOrEditEntryAsync(string tenant, LoginView meta, CalendarEvent calendarEvent)
        {
            if (calendarEvent == null)
            {
                throw new CalendarEventException(I18N.CannotAddNullCalendarEvent);
            }

            var candidate = calendarEvent.ToEvent();

            candidate.AuditUserId = meta.UserId;
            candidate.UserId      = meta.UserId;
            candidate.AuditTs     = DateTimeOffset.UtcNow;
            candidate.TimeZone    = TimeZone.CurrentTimeZone.StandardName;

            if (calendarEvent.EventId == null)
            {
                var eventId = await Events.AddEventAsync(tenant, candidate).ConfigureAwait(false);

                candidate.EventId = eventId;

                JobHelper.CreateJob(tenant, candidate);
                return(eventId);
            }

            JobHelper.DeleteJob(calendarEvent.EventId.Value);
            JobHelper.CreateJob(tenant, candidate);

            await Events.UpdateEventAsync(tenant, candidate.EventId, candidate).ConfigureAwait(false);

            return(candidate.EventId);
        }
        static void CreateJob(string poolId, string jobId)
        {
            var batchAccountUrl  = ConfigurationManager.AppSettings["BatchAccountUrl"];
            var batchAccountName = ConfigurationManager.AppSettings["BatchAccountName"];
            var batchAccountKey  = ConfigurationManager.AppSettings["BatchAccountKey"];


            var jobHelper = new JobHelper(batchAccountUrl, batchAccountName, batchAccountKey);

            jobHelper.CreateJob(poolId, jobId);
        }
Beispiel #3
0
        private void TestScheduledJobs()
        {
            // 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
            SetTime0();                                                                     //Set fixed current time

            var jobThreadType = JobThreadType.Background;                                   //just to play with it, try it both ways; Background is default

            // We use user-bound context and session (as if user is logged in), to check that retries are executed as
            // the same user
            var context = GetUserBoundContext();
            var session = context.OpenSecureSession();

            var     utcNow      = _app.TimeService.UtcNow;
            var     halfHourFwd = utcNow.AddMinutes(30);
            var     oneHourFwd  = utcNow.AddHours(1);
            IJob    job;
            IJobRun jobRun;

            // 1. Create and run job at certain time
            jobRun = JobHelper.ScheduleJobRunOn(session, "4. Scheduled Job 4",
                                                (jobCtx) => ScheduledJob(jobCtx, 0, "123", 5), halfHourFwd, threadType: jobThreadType);
            session.SaveChanges();
            _callCount = 0;
            // After 25 minutes job should NOT be executed
            SetTimeOffsetFireTimers(25);
            //Thread.Sleep(50);
            AssertCallCount(0, "Expected job not executed.");
            // After 31 minutes job should be executed
            SetTimeOffsetFireTimers(31);
            AssertCallCount(1, "Expected callCount 1 after 31 minutes");

            // 2. Another approach - create job entity, and then schedule it later, to run at certain time
            var ScheduledJobName2 = "5. Scheduled Job";

            job = JobHelper.CreateJob(session, ScheduledJobName2, (jobCtx) => ScheduledJob(jobCtx, 0, "abc", 10), threadType: jobThreadType);

            // 2.a. Now set the job to run at certain time. For each invocation we can provide some custom data;
            // pass custom Guid and string; this data is available inside job method in jobRunContext.Data, DataId
            _callCount = 0;
            var guidData   = Guid.NewGuid();
            var stringData = "SomeString";

            jobRun = JobHelper.ScheduleJobRunOn(job, runOnUtc: oneHourFwd, dataId: guidData, data: stringData);
            session.SaveChanges();

            SetTimeOffsetFireTimers(30); // Move forwad 30 minutes - job should not fire yet
            AssertCallCount(0, "Expected scheduled job not activated.");

            SetTimeOffsetFireTimers(61); // Move forwad
            AssertCallCount(1, "Expected scheduled call count to increment.");
            Assert.AreEqual(guidData, _receivedDataId.Value, "DataId does not match.");
            Assert.AreEqual(stringData, _receivedData, "String data does not match.");

            // 2.b. Let's schedule one more run of the same job
            // After job was saved, we can find it by JobName. Note that in this case JobName must be unique
            session         = context.OpenSecureSession();
            job             = session.GetJobByUniqueName(ScheduledJobName2);
            _callCount      = 0;
            _receivedData   = null;
            _receivedDataId = null;
            var twoHourFwd = oneHourFwd.AddHours(1);

            jobRun = JobHelper.ScheduleJobRunOn(job, runOnUtc: twoHourFwd, dataId: guidData, data: stringData);
            session.SaveChanges();
            //Move to the future and fire timers
            SetTimeOffsetFireTimers(125);
            AssertCallCount(1, "Expected scheduled call count to increment.");
            Assert.AreEqual(guidData, _receivedDataId.Value, "DataId does not match.");
            Assert.AreEqual(stringData, _receivedData, "String data does not match.");


            // 3. Let's schedule a job with initial failures; start time 10 minutes forward
            SetTime0();
            _callCount = 0;
            jobRun     = JobHelper.ScheduleJobRunOn(session, "6. Scheduled Job, 2 fails",
                                                    (jobCtx) => ScheduledJob(jobCtx, 2, "456", 5), utcNow.AddMinutes(10), threadType: jobThreadType);
            job = jobRun.Job;
            session.SaveChanges();
            // After 9 minutes job should NOT be executed
            SetTimeOffsetFireTimers(9);
            // Thread.Sleep(50); //just an extra time to finish
            AssertCallCount(0, "Expected job not executed.");
            // After 12 minutes job should be executed and fail
            SetTimeOffsetFireTimers(12);
            // Thread.Sleep(100); //just an extra time to finish
            AssertCallCount(1, "Expected job to execute and fail after 12 minutes");
            jobRun = job.GetLastFinishedJobRun();
            Assert.AreEqual(JobRunStatus.Error, jobRun.Status, "Expected error status.");
            //execute 2 more times, total 3 - finally should succeed
            SetTimeOffsetFireTimers(15);
            // Thread.Sleep(100); //just an extra time to finish
            SetTimeOffsetFireTimers(18);
            // Thread.Sleep(100);
            AssertCallCount(3, "Expected job tried 3 times");
            jobRun = job.GetLastFinishedJobRun();
            Assert.AreEqual(JobRunStatus.Completed, jobRun.Status, "Expected completed status.");

            // 4. Long running job
            SetTime0();
            _callCount = 0;
            job        = JobHelper.CreateJob(session, "7. Long running job", (ctx) => LongRunningJobMethod(ctx, "xyz", 42), JobThreadType.Background);
            jobRun     = job.ScheduleJobRunOnSaveChanges();
            session.SaveChanges();
            Thread.Sleep(50);
            // Job must be started
            AssertCallCount(1, "Expected long job to start");
            EntityHelper.RefreshEntity(jobRun);
            Assert.AreEqual(JobRunStatus.Executing, jobRun.Status, "Expecte long job status Executing");
            SetTimeOffsetFireTimers(5, waitForJobFinish: false); //move forward 5 minutes, should still be executing
            EntityHelper.RefreshEntity(jobRun);
            Assert.AreEqual(JobRunStatus.Executing, jobRun.Status, "Expecte long job status Executing");
            SetTimeOffsetFireTimers(11); //move forward 11 minutes, should be completed
            EntityHelper.RefreshEntity(jobRun);
            Assert.AreEqual(JobRunStatus.Completed, jobRun.Status, "Expecte long job status Completed");

            ResetTimeOffset();
        }