// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); app.UseHangfireDashboard() .UseHangfireServer(); // Removes all recurring jobs using (var connection = JobStorage.Current.GetConnection()) { StorageConnectionExtensions.GetRecurringJobs(connection) .ForEach(job => RecurringJob.RemoveIfExists(job.Id)); } RecurringJob.AddOrUpdate <HangfireJobs>(nameof(HangfireJobs.GoodMorning), x => x.GoodMorning(), Cron.Daily(8)); }
private void removeAllJobs() { using (var connection = JobStorage.Current.GetConnection()) { foreach (var job in StorageConnectionExtensions.GetRecurringJobs(connection)) { RecurringJob.RemoveIfExists(job.Id); } } }
public void ScheduleSynchronization() { using (var connection = JobStorage.Current.GetConnection()) { foreach (var recurringJob in StorageConnectionExtensions.GetRecurringJobs(connection)) { RecurringJob.RemoveIfExists(recurringJob.Id); } } RecurringJob.AddOrUpdate(() => SynchronizeSync(), Cron.Daily(0, 30), TimeZoneInfo.Local); }
public static void Main(string[] args) { CreateHostBuilder(args) .Build() .Run(); using IStorageConnection connection = JobStorage.Current.GetConnection(); foreach (RecurringJobDto rJob in StorageConnectionExtensions.GetRecurringJobs(connection)) { RecurringJob.RemoveIfExists(rJob.Id); } }
/// <summary> /// 获取job任务 /// </summary> /// <param name="name"></param> /// <returns></returns> public string GetJobdata(string name) { using (var connection = JobStorage.Current.GetConnection()) { var RecurringJob = StorageConnectionExtensions.GetRecurringJobs(connection). Where(p => p.Id == name).FirstOrDefault(); if (RecurringJob != null) { return(JsonConvert.SerializeObject( JsonConvert.DeserializeObject <RecurringJobItem>(RecurringJob.Job.Args.FirstOrDefault().ToString()) )); } } return(""); }
public virtual async Task <ResultPagedList <jobdto> > GetJobItems(JobListPagedRequestDto listPagedRequestDto) { var joblist = new List <jobdto>(); var totalcount = 0; using (var connection = JobStorage.Current.GetConnection()) { var RecurringJob = StorageConnectionExtensions.GetRecurringJobs(connection); if (RecurringJob != null) { RecurringJob.ForEach(job => { var jobitem = JsonConvert.DeserializeObject <RecurringJobItem>(job.Job.Args.FirstOrDefault().ToString()); var jobmodel = new jobdto(); jobmodel.BasicPassword = jobitem.BasicPassword; jobmodel.BasicUserName = jobitem.BasicUserName; jobmodel.ContentType = jobitem.ContentType; jobmodel.Corn = jobitem.Corn; jobmodel.CreatedAt = job.CreatedAt; jobmodel.Data = jobitem.Data; jobmodel.IsRetry = jobitem.IsRetry; jobmodel.JobName = jobitem.JobName; jobmodel.LastExecution = job.LastExecution; jobmodel.LastJobState = job.LastJobState; jobmodel.NextExecution = job.NextExecution; jobmodel.QueueName = jobitem.QueueName; jobmodel.Removed = job.Removed; jobmodel.Timeout = jobitem.Timeout; jobmodel.LastJobId = job.LastJobId; jobmodel.Method = jobitem.Method; jobmodel.Url = jobitem.Url; joblist.Add(jobmodel); }); totalcount = joblist.Count(); if (!string.IsNullOrEmpty(listPagedRequestDto.Vague)) { joblist = joblist.Where(p => p.JobName.Contains(listPagedRequestDto.Vague) || p.QueueName.Contains(listPagedRequestDto.Vague) || (p.Method == listPagedRequestDto.Vague)).ToList(); totalcount = joblist.Count(); } var jobreslut = joblist.OrderByDescending(p => p.QueueName); joblist = jobreslut.Skip((listPagedRequestDto.SkipPage - 1) * listPagedRequestDto.PagedCount).Take(listPagedRequestDto.PagedCount).ToList(); return(ResultPagedList <jobdto> .ReSuccess(joblist, totalcount)); } } return(new ResultPagedList <jobdto>()); }
static void Main(string[] args) { GlobalConfiguration.Configuration .UseSqlServerStorage(theConnection); var jobStorage = new Hangfire.SqlServer.SqlServerStorage(theConnection, new Hangfire.SqlServer.SqlServerStorageOptions { PrepareSchemaIfNecessary = false }); //GlobalConfiguration.Configuration.UseAutofacActivator(provider); RecurringJobManager manager = new RecurringJobManager(jobStorage); List <scheduledReports> theReports = GetReports(); //*********Code to clear out jobs if needed*********// using (var connection = jobStorage.GetConnection()) { foreach (var recurringJob in StorageConnectionExtensions.GetRecurringJobs(connection)) { RecurringJob.RemoveIfExists(recurringJob.Id); } } int jobid = 0; foreach (scheduledReports sr in theReports) { RecurringJob.AddOrUpdate("reportjob_" + jobid.ToString(), () => sendReport(sr.apiCalls, sr.description, sr.emailRecipients), sr.cron); jobid++; } //foreach (scheduledReports sr in theReports) //{ // sendReport(sr.apiCalls, sr.description, sr.emailRecipients).Wait(); //} GlobalConfiguration.Configuration.UseSqlServerStorage(theConnection); using (var server = new BackgroundJobServer()) { Console.WriteLine("Hangfire Server started. Press any key to exit..."); Console.ReadKey(); } }
/// <summary> /// 获取已经暂停的任务 /// </summary> /// <returns></returns> public List <PauseRecurringJob> GetRecurringJobs() { var pauselist = new List <PauseRecurringJob>(); using (var connection = JobStorage.Current.GetConnection()) { var joblist = StorageConnectionExtensions.GetRecurringJobs(connection); joblist.ForEach(k => { var conts = connection.GetAllItemsFromSet($"JobPauseOf:{k.Id}"); if (conts.Contains("true")) { var pauseinfo = new PauseRecurringJob() { Id = k.Id }; pauselist.Add(pauseinfo); } }); } return(pauselist); }
public List <CustomJobModel> GetJobs() { try { var conn = Hangfire.JobStorage.Current.GetConnection(); var jobs = StorageConnectionExtensions.GetRecurringJobs(conn); //cast jobs to WAMCustomJob type List <CustomJobModel> retJobs = new List <CustomJobModel>(); foreach (var job in jobs) { string jString = job.Job.ToString().ToUpper(); string scriptType = ""; if (jString.Contains("VBS")) { scriptType = "VBS"; } else if (jString.Contains("BAT")) { scriptType = "BAT"; } else if (jString.Contains("PS")) { scriptType = "PS"; } else { scriptType = "OTHER"; } CustomJobModel cjm = new CustomJobModel(job.Id, scriptType, job.Cron, job.LastJobState, job.Job.Args[1].ToString()); retJobs.Add(cjm); } return(retJobs); } catch (Exception ex) { throw new Exception($"Failed to retrieve jobs. Error: {ex.Message}"); } }
public void ConfigureHangfireJob(IRecurringJobManager recurringJobManager, IServiceProvider serviceProvider) { using (var connection = JobStorage.Current.GetConnection()) { foreach (var recurringJob in StorageConnectionExtensions.GetRecurringJobs(connection)) { RecurringJob.RemoveIfExists(recurringJob.Id); } } string recurringJobs = _config.GetSection("HangfireJob").GetValue <string>("RecurringJobs"); foreach (string recurringJob in recurringJobs.Split(';')) { HangfireJob hangfireJob; string jobName = recurringJob.Split(':')[0].Trim(); string jobCron = recurringJob.Split(':')[1].Trim(); if (Enum.TryParse(jobName, true, out hangfireJob)) { switch (hangfireJob) { case HangfireJob.ScrapeNewReleasedMovie: recurringJobManager.AddOrUpdate(jobName, () => serviceProvider.GetService <IJavScrapeService>().ScrapeNewReleasedMovie(), jobCron); break; case HangfireJob.ScrapeMovieMagnet: recurringJobManager.AddOrUpdate(jobName, () => serviceProvider.GetService <IMagnetScrapeService>().DailyDownloadMovieMagnet(), jobCron); break; case HangfireJob.MonitorMovieDownload: recurringJobManager.AddOrUpdate(jobName, () => serviceProvider.GetService <IDownloadService>().MonitorMovieDownload(), jobCron); break; } } } }
public async Task RefreshJobs() { // delete all old schedules // todo можливо, варто не видаляти усі роботи using (var connection = JobStorage.Current.GetConnection()) { foreach (var recurringJob in StorageConnectionExtensions.GetRecurringJobs(connection)) { RecurringJob.RemoveIfExists(recurringJob.Id); } } // Problem : every time ,when sending request for notificationController, // all schedules delete and new will be write, but we have only replace jobs var jobSchedules = await _schedulerService.GetJobSchedules(); foreach (var jobSchedule in jobSchedules) { _recurringJobManager.AddOrUpdate(jobSchedule.Cron, () => _notificationJob.Execute(jobSchedule.FireTime), jobSchedule.Cron); } _jobSchedules = jobSchedules; }