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 ?


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

  • 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

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I think you need background worker instead of background jobs

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

  • 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.

  • User Avatar
    0
    liangshiwei created
    Support Team 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.

  • 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.

  • User Avatar
    0
    liangshiwei created
    Support Team 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...
    

  • User Avatar
    0
    Dicky.tech@gmail.com created

    Let me run some tests

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    ok

  • 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 ?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I think there is no problem.

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11