Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Hangfire Job #5755


User avatar
0
muhittincelik created

Hi,

Can you give me an example code for job creation with hangfire. Which triggered every minute and log "Hello World" to log file. And starting with application startup.

Thanks


13 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    You can take a look at this demo: AbpHangfireConsoleApp

  • User Avatar
    0
    muhittincelik created

    I dont understand that code. There are some integrations at Zero Modula with Hangfire and i want to use that.

    I need simple code.

    Muhittin

  • User Avatar
    0
    muhittincelik created

    I have an action named Scan which is triggered from a button. I want to schedule this action.

    [AbpAuthorize(AppPermissions.Pages_TrackedHosts_Scan)] public async Task Scan(EntityDto input) { .. .. .. }

  • User Avatar
    0
    ryancyq created
    Support Team

    you can create periodic background job and add to the background queue via BackgroundJobManager.

    See aspnetboilerplate#background-works

    To make your background jobs run using Hangfire Integration.

    See aspnetboilerplate#hangfire-integration

  • User Avatar
    0
    ryancyq created
    Support Team

    To enable hangfire integration for AspNetZero.

    Configure these hangfire setup.

    Web.Core Project
    https://github.com/aspnetzero/aspnet-zero-core/blob/29ce1d485ad7955591fa8a97d1d446340c5986e4/aspnet-core/src/MyCompanyName.AbpZeroTemplate.Web.Core/AbpZeroTemplateWebCoreModule.cs#L78

    Web.Host Project
    https://github.com/aspnetzero/aspnet-zero-core/blob/29ce1d485ad7955591fa8a97d1d446340c5986e4/aspnet-core/src/MyCompanyName.AbpZeroTemplate.Web.Host/Startup/Startup.cs#L185

    Web.Mvc Project
    https://github.com/aspnetzero/aspnet-zero-core/blob/f72d4bf9ced778e06265f1c415a8553b10cbaf3d/aspnet-core/src/MyCompanyName.AbpZeroTemplate.Web.Mvc/Startup/Startup.cs#L161

  • User Avatar
    0
    muhittincelik created

    Settings are ok. I added the following lines to web.mvc startup file.

    string cronExp = "1 * * * *"; EntityDto a = new EntityDto(); a.Id = 1; RecurringJob.AddOrUpdate<ITrackedHostsAppService>(x => x.Scan(a), cronExp);

    It is running every minute but got this error. Not user logged in. Because this Scan method is need authorization.

  • User Avatar
    1
    ryancyq created
    Support Team

    Limitations

    More than one background jobs in a single transaction isn't supported by Hangfire. Because, Hangfire does not participate the current transaction. It does not use the ambient transaction (TransactionScope).

    The background job will not be able to retrieve current user id & tenant id. You need to pass these as the parameters to the background job

  • User Avatar
    0
    muhittincelik created

    Is there any example about how to pass login parameters to background job.

  • User Avatar
    0
    muhittincelik created

    All background and hangfire jobs failed due to current user not logged error. I need a solution for background jobs. Background job must call thefollowing service method which has need authorization like below;

    [AbpAuthorize(AppPermissions.Pages_TrackedHosts_Scan)] public async Task Scan(EntityDto input) {

  • User Avatar
    1
    alper created
    Support Team

    so don't use the method Scan() because it's made to be executed by a user which has Pages_TrackedHosts_Scan permission. Create a new method wich doesn't have any permissions required.

  • User Avatar
    0
    huntethan89 created

    Rather than creating a new method without AbpAuthorization, can we make the hangfire jobs to run under a particular role always e.g. Admin or User?

  • User Avatar
    0
    rajalla created

    Any luck on this?

  • User Avatar
    0
    BobIngham created

    @rajalla, see here: getting Hangfire to work in Zero