Open Closed

Background Services using hangfire #3943


User avatar
0
Dicky.tech@gmail.com created

Have implemented hangfire for background jobs.

Firstly being wondering if this reduces resources utilization since my iis cpu and ram usage is high.

Am using recurring jobs. Every 5 minutes.

Seems every time I run the project, a new recurring job is created , how can I avoid this ?

Markdown supported.
Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)

11 Answer(s)
  • User Avatar
    0
    berkansasmaz created
    Support Team .NET Developer

    Hello, I don't think this question is related to ABP but still, there is some information I want to share with you ✌️

    Firstly being wondering if this reduces resources utilization since my iis cpu and ram usage is high.

    Unfortunately, I don't know much about the ISS.

    Seems every time I run the project, a new recurring job is created , how can I avoid this ?

    Hangfire methods allow calling with a delay. For instance:

    BackgroundJob.Schedule(
        () => Console.WriteLine("Hello, world"),
        TimeSpan.FromDays(1));
    

    See more: https://docs.hangfire.io/en/latest/background-methods/calling-methods-with-delay.html

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    Dicky.tech@gmail.com created

    The issue is each time the app re starts it add a new recurring jobs instead of updating existing

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    liangshiwei created
    Fullstack Developer

    Hi,

    I think you need background worker instead of background jobs

    https://docs.abp.io/en/abp/latest/Background-Workers-Hangfire

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    Dicky.tech@gmail.com created

    Below is my class worker class and how am calling it

    public override void OnApplicationInitialization(ApplicationInitializationContext context)
    { 
    context.AddBackgroundWorkerAsync<SyncLeavePlans>();
    }
    
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Volo.Abp.BackgroundJobs;
    using Volo.Abp.BackgroundWorkers;
    using Volo.Abp.Domain.Repositories;
    using Volo.Abp.Modularity;
    using Volo.Abp.MultiTenancy;
    using Volo.Abp.Threading;
    using Volo.Saas.Tenants;
    using System.Linq;
    using System.Linq.Dynamic.Core;
    using System.Linq.Expressions; 
    using Volo.Abp.BackgroundWorkers.Hangfire;
    using Hangfire;
    using Volo.Abp.DependencyInjection;
    using Volo.Abp.Uow;
    using System.Threading;
    
    namespace API.Integrations
    {
        public interface ISyncLeavePlans : IHangfireBackgroundWorker
        {
        }    
    
        [DependsOn(
              typeof(AbpBackgroundWorkersModule),
              typeof(AbpMultiTenancyModule),
              typeof(AbpBackgroundJobsModule)
              )]
        [ExposeServices(typeof(ISyncLeavePlans))]
        public class SyncLeavePlans : HangfireBackgroundWorkerBase, ISyncLeavePlans
        { 
            [Obsolete]
            public SyncLeavePlans(       ) 
            {
                RecurringJobId = "ISyncLeavePlans";
                CronExpression = Cron.MinuteInterval(5);
            }
            [UnitOfWork]
            public override async Task DoWorkAsync(CancellationToken cancellationToken)
            {
                Logger.LogInformation("Starting:...");
                 
                Logger.LogInformation("Completed:...");
            }
        }
    
    }
    

    After running the project, we see the below two jobs. Executed in milliseconds.

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    liangshiwei created
    Fullstack Developer

    Hi,

    • BackgroundJobWorker is a built-in worker in ABP that is used for BackgroundJob system.
    • TokenCleanupBackgroundWorkers is a built-in worker in ABP that is used to clean the old tokens.

    And the SyncLeavePlans works as expected.

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    Dicky.tech@gmail.com created

    @liangshiwei as per the logs you've shared. Every seconds the recurring job is running and executing.

    It should every 5 minutes only. That's the issue.

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    liangshiwei created
    Fullstack Developer

    Hi,

    Sorry but it's already every 5 minutes.

    11:40:08 Starting...
    11:40:08 Completed...
    ...
    
    11:45:08 Starting...
    11:45:08 Completed...
    

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    Dicky.tech@gmail.com created

    Let me run some tests

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    liangshiwei created
    Fullstack Developer

    ok

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    Dicky.tech@gmail.com created

    By the way am using this to push data to a 3rd party system. Is it the recommended way ?

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    liangshiwei created
    Fullstack Developer

    Hi,

    I think there is no problem.

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.8.0-preview. Updated on September 23, 2026, 09:57
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.