예제 #1
0
        public virtual async Task <string> EnqueueAsync <TJob, TArgs>(TArgs args,
                                                                      BackgroundJobPriority priority = BackgroundJobPriority.Normal, TimeSpan?delay = null)
            where TJob : IBackgroundJobBase <TArgs>
        {
            string jobInfoId;

            using (var uow = UnitOfWorkManager.Begin())
            {
                var jobInfo = new BackgroundJobInfo
                {
                    JobType  = typeof(TJob).AssemblyQualifiedName,
                    JobArgs  = args.ToJsonString(),
                    Priority = priority
                };

                if (delay.HasValue)
                {
                    jobInfo.NextTryTime = Clock.Now.Add(delay.Value);
                }

                await _store.InsertAsync(jobInfo);

                await CurrentUnitOfWork.SaveChangesAsync();

                jobInfoId = jobInfo.Id.ToString();
                await uow.CompleteAsync();
            }

            return(jobInfoId);
        }
예제 #2
0
        /// <summary>
        /// 将工作插入到队列中
        /// </summary>
        /// <typeparam name="TJob"></typeparam>
        /// <typeparam name="TArgs"></typeparam>
        /// <param name="args"></param>
        /// <param name="priority"></param>
        /// <param name="delay"></param>
        /// <returns></returns>
        public async Task EnqueueAsync <TJob, TArgs>(TArgs args, BackgroundJobPriority priority = BackgroundJobPriority.Normal, TimeSpan?delay = null)
            where TJob : IBackgroundJob <TArgs>
        {
            var jobInfo = new BackgroundJobInfo
            {
                JobType  = typeof(TJob).AssemblyQualifiedName,
                JobArgs  = args.ToJsonString(),
                Priority = priority
            };

            if (delay.HasValue)
            {
                jobInfo.NextTryTime = Clock.Now.Add(delay.Value);
            }

            await _store.InsertAsync(jobInfo);
        }
예제 #3
0
        public async Task InsertAsync()
        {
            var jobInfo = new BackgroundJobInfo
            {
                Id      = Guid.NewGuid(),
                JobArgs = "args1",
                JobName = "name1"
            };

            await _backgroundJobStore.InsertAsync(
                jobInfo
                ).ConfigureAwait(false);

            var jobInfo2 = await _backgroundJobStore.FindAsync(jobInfo.Id).ConfigureAwait(false);

            jobInfo2.ShouldNotBeNull();
            jobInfo2.Id.ShouldBe(jobInfo.Id);
            jobInfo2.JobArgs.ShouldBe(jobInfo.JobArgs);
            jobInfo2.JobName.ShouldBe(jobInfo.JobName);
        }