Ejemplo n.º 1
0
        public Task <bool> RemoveRecurringJobAsync <TJob, TArgs>() where TJob : IBackgroundJob <TArgs>
        {
            string recurringJobId = typeof(TJob).Name + "." + "Execute";

            HangfireRecurringJob.RemoveIfExists(recurringJobId);
            return(Task.FromResult(true));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 添加与更新周期Job
        /// 这个方法适合给泛型执行的Job使用,如果是独立周期Job,建议在Syinpo.Unity.Hangfire.HangfireRegister中维护。
        /// 泛型使用场景: 轮询发送朋友圈功能,后台两次新增任务
        ///     recurringJobId : 发送朋友圈_SendFriendCircle_Id1
        ///     recurringJobId : 发送朋友圈_SendFriendCircle_Id2
        ///
        ///     TJob :  SendFriendCircleJob.cs
        ///
        ///     TArgs : SendFriendCircleJobArgs.cs { public int Id{get;set;}   public string Other {get;set;} }
        ///
        ///     cronExpression :
        /// </summary>
        /// <typeparam name="TJob"></typeparam>
        /// <typeparam name="TArgs"></typeparam>
        /// <param name="recurringJobId"></param>
        /// <param name="args"></param>
        /// <param name="cronExpression"></param>
        /// <returns></returns>
        public Task <string> AddOrUpdateRecurringJobAsync <TJob, TArgs>(string recurringJobId, TArgs args, string cronExpression) where TJob : IBackgroundJob <TArgs>
        {
            string jobUniqueIdentifier = string.Empty;

            if (!_hangfireOptions.UseHangfire)
            {
                return(Task.FromResult(jobUniqueIdentifier));
            }

            HangfireRecurringJob.AddOrUpdate <TJob>(recurringJobId, a => a.Execute(args), cronExpression);
            jobUniqueIdentifier = recurringJobId;
            return(Task.FromResult(jobUniqueIdentifier));
        }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IBackgroundJobClient backgroundJobs)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHangfireDashboard();
            app.UseHangfireServer();

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello Hangfire! - Goto https://localhost:5001/hangfire");
                });
            });

            while (true)
            {
                try
                {
                    using var service = app.ApplicationServices.GetService <IRecurringJob>();
                    // Thread.Sleep(3000);
                    RecurringJob.AddOrUpdate("Recurring Random", () => service.GenerateGuid(), Cron.Minutely);

                    return;
                }
                catch (Exception e)
                {
                    Thread.Sleep(3000);
                }
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 删除周期性Job
 /// 适合泛型周期Job删除
 /// </summary>
 /// <returns></returns>
 public Task <bool> RemoveRecurringJobAsync(string recurringJobId)
 {
     HangfireRecurringJob.RemoveIfExists(recurringJobId);
     return(Task.FromResult(true));
 }