Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Luke 110 posts 256 karma points
    Dec 04, 2014 @ 10:43
    Luke
    0

    Umbraco and Background Tasks

    Good Morning

    I am thinking of using http://hangfire.io/ as a means to run daily/weekly tasks. Has anybody used this or anything similar with Umbraco? Be good to get some feedback!

    I thought this may be a better solution to

    Any other thoughts on running daily tasks would be warmly appreciated!

    Regards L

  • Adrian 38 posts 117 karma points
    Dec 04, 2014 @ 12:56
    Adrian
    0

    No experience with HangFire, but you can block requests to the URLs based on various request parameters, and only allow your scheduled calls.

    On IIS http://www.iis.net/configreference/system.webserver/security/requestfiltering

    If you have a firewall or loadbalancer infront of your servers you might be better off having them configured to do this.

    Hangfire looks a pretty solid tool though, not sure what would be involved in using it within Umbraco.

  • Luke 110 posts 256 karma points
    Dec 11, 2014 @ 13:18
    Luke
    0

    Hello To All Interested in background/scheduled tasks,

    I added Hangfire through NuGet and so far not had any issues with it with Umbraco. This is the code that is required to initialize.

        using System.Web;
        using Umbraco.Core;
        using Hangfire;
        using Hangfire.SqlServer;
        using Microsoft.Owin;
        using Owin;
    
        [assembly: OwinStartup(typeof(AppNamespace.AppStart))]
    
    namespace AppNamespace
    {
        public class AppStart : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                System.Diagnostics.Debug.WriteLine("Some Text");
            }
    
            public void Configuration(IAppBuilder app)
            {
                app.UseHangfire(config =>
                {
                    config.UseSqlServerStorage("name or connection string");
                    config.UseServer();
                });
                BackgroundJob.Enqueue(() => System.Diagnostics.Debug.WriteLine("Text From Background Queue"));
                BackgroundJob.Schedule(() => System.Diagnostics.Debug.WriteLine("Text From Background Queue 1 Minute Later"), TimeSpan.FromMinutes(1));
            }
        }
    }
    
  • Louise Ryan 1 post 21 karma points
    Jul 28, 2015 @ 08:36
    Louise Ryan
    0

    This all worked for me - thanks! I did have to update the bit in the Configuration method to work happily with the latest version:

     var cs = Umbraco.Core.ApplicationContext.Current.DatabaseContext.ConnectionString;
    
            GlobalConfiguration.Configuration.UseSqlServerStorage(cs);
    
            app.UseHangfireDashboard();
            app.UseHangfireServer();
    

    I also needed to add ~/hangfire/ to the umbracoReservedPaths in the web.config so the routing didn't get upset. The css and js wasn't loading correctly until I did that step.

    Cheers!

  • Robert Ghafoor 33 posts 95 karma points
    Nov 13, 2015 @ 17:08
    Robert Ghafoor
    0

    for those who want to know how to create a IAuthorizationFilter for hangfire i created one like this:

    [UmbracoAuthorize]
    public class UmbracoAuthorizationFilter :  IAuthorizationFilter
    {
        public bool Authorize(IDictionary<string, object> owinEnvironment)
        {
            // Ensure umbraco user if possible.
            var http = new HttpContextWrapper(HttpContext.Current);
            var ticket = http.GetUmbracoAuthTicket();
            http.AuthenticateCurrentRequest(ticket, true);
    
            var user = UmbracoContext.Current.Security.CurrentUser;
    
            if (user != null && user.AllowedSections.Contains("developer"))
            {
                return true;
            }
    
            return false;
        }
    }
    

    and then configured it:

    public override void Configuration(IAppBuilder app)
        {
            base.Configuration(app);
    
            var cs = ApplicationContext.Current.DatabaseContext.ConnectionString;
            GlobalConfiguration.Configuration.UseSqlServerStorage(cs);
    
            app.UseHangfireDashboard("/jobs", new DashboardOptions
            {
                AuthorizationFilters = new[] { new UmbracoAuthorizationFilter() },
                AppPath = "/umbraco/"
            });
            app.UseHangfireServer();
        }
    
  • David Armitage 508 posts 2078 karma points
    Mar 21, 2016 @ 11:56
    David Armitage
    1

    Hi,

    Just to help anyone out there. I just installed and configured Hangfire on my Umbraco 7.1 website using mostly from the information above and a big from elsewhere.

    So here is the steps involved

    1. Installed Hangfire using Nuget PM> Install-Package HangFire

    2. When I run the project. I encountered issues. Something about a missing startup file.

      • To fix this add a new file to your AppCode/AppStart folder. If you are using visual studio OWIN Startup Class. It should exist if you installed the Nuget package above.
      • Add the file and call it OWINStartup.cs or something like that
    3. Once this file is added this will fix the problem and your project should run. Now we need to edit this file and add some configuration for Hangfire.

      • Open up the new cs file you just added and edit the Configuration method. Add the following...

      var cs = Umbraco.Core.ApplicationContext.Current.DatabaseContext.ConnectionString; GlobalConfiguration.Configuration.UseSqlServerStorage(cs);

      //some code will be added here later

      app.UseHangfireServer();

    4. Now go to your main web.config and add ~/hangfire to the umbracoReservedPaths configuration setting.

      • Example

        <key="umbracoReservedPaths" value="~/umbraco,~/install/,~/hangfire/"/>

    5. Now if you run the project and set a break point on the OWINStartup.cs file you will notice this runs fine. If you check your database you will also see that a load of Hangfire tables have been created. Great!

    6. Navigate to the url http://your-domain/hangfire. If will now see the Hangfire dashboard. Great. This is now working locally.

    7. I then deployed to a live environment and encountered problems. Basically every time I navigated to http://your-domain/hangfire I was redirected to the login screen. After a little research I could see that a few more things need to be configured in terms of Authorization. Hangfire by default doesn't let unauthorized users view the dashboard. This makes sense since there is some sensitive information displayed here plus you can operate a number of functions. To fix this do the following

      • Create a new cs file. For this example I called it HangFireAuthorizationFilter.cs
      • Use the following code for your new class. You will need to change "developer" to whichever user section you want to authorize access. Comment most of the code out and simply return true if you want to enable access for everyone (not recommended!).

      public class HangFireAuthorizationFilter : IAuthorizationFilter { public bool Authorize(IDictionary

          var user = UmbracoContext.Current.Security.CurrentUser;
      
      
      
      if (user != null &amp;&amp; user.AllowedSections.Contains("developer"))         {           return true;        }
      
      
      return false;   }
      

      }

      • Now go back to your OWINStartus.cs file and add the following code where I commented //some code will go here later.

      app.UseHangfireDashboard("/hangfire", new DashboardOptions { AuthorizationFilters = new[] { new HangFireAuthorizationFilter() }, AppPath = "/umbraco/" });

    8. If you deploy to your live environment and login to your website with an authorized user type then the /hangfire dashboard should now work fine.

    Hope this saves some people a bit of time.

  • David Armitage 508 posts 2078 karma points
    Apr 26, 2016 @ 11:33
    David Armitage
    0

    Hi,

    I just went through the same process on Umbraco version 7.4.3 assembly: 1.0.5948.18141 and encountered a number of problems which took me a while to work out.

    I thought I would document here for myself and anyone else setting with a similar version.

    1. I followed the process above but when I run the project I started getting errors.

    2. The first lot I had to deal with are referencing issues. It looks like this version of Umbraco have updated the OWIN dlls and the newtonsoft dlls

    3. To get around this I backed up my old version of the site. Run the Hangfire nuget installation (1.5.6 from memory). Following this the nuget also installs unwanted stuff which creates the problems. Basically you don't need any of the OWIN or Newtonsoft stuff since we already have a later version with Umbraco.

    4. To fix the issues open packages.config and remove everything in there apart from

    5. Now go back to your backed up version of the site and copy all the OWIN and Newtonsoft dlls to the new version. This will fix these issue.

    Following this I noticed that the OWIN startup class was never running. To fix this I had to make some changes to both the startup class and umbraco web.config.

    1. In your OWIN startup class change the assembly to be [assembly: OwinStartup("UmbracoStandardOwinStartup", typeof(OWINStartupConfiguration))]

    2. Add override on the method public override void Configuration(IAppBuilder app)

    3. Add this as your first line of code to the configuration method base.Configuration(app);

    4. Now go to your web.config and replace For

    We are now good to go!

  • Kasper Skov 6 posts 76 karma points
    Sep 09, 2016 @ 12:58
    Kasper Skov
    0

    Hi David.

    Was your js/css on "/hangfire/" still working after 7.4.x install?

  • Kasper Skov 6 posts 76 karma points
    Sep 12, 2016 @ 20:17
    Kasper Skov
    0

    Btw your two bullet point 4's are incomplete. Do you remember what is was?

  • Kasper Skov 6 posts 76 karma points
    Sep 09, 2016 @ 12:56
    Kasper Skov
    0

    Hi all

    Even though I added "~/hangfire/" to the reserveredPaths tag it still can't find the js/css.

    enter image description here

        <add key="umbracoReservedPaths" value="~/umbraco,~/install/,~/hangfire/,~/hangfire,/hangfire,/hangfire/" />
    

    Any ideas what might go wrong?

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Sep 14, 2016 @ 11:33
    Simon Dingley
    0

    Adding ~/hangfire was enough to correct the issue for me.

  • David Armitage 508 posts 2078 karma points
    Sep 16, 2016 @ 07:28
    David Armitage
    0

    Sorry the changes to the web.config i left off the end was....

    <!--<add key="owin:appStartup" value="UmbracoDefaultOwinStartup" />--> <add key="owin:appStartup" value="UmbracoStandardOwinStartup" /> <!--<add key="owin:AutomaticAppStartup" value="true" />-->
    

    Basically comment out the name of the default umbraco startup and replace with the one we have created.

    Oh and the css works fine for me. I just run through this whole tutorial again on Umbraco 7.5.3

    One slight change I did that made it much easier.

    Instead of installing from nuget I downloaded all the binaries and within visual studio just browsed to the references of the hangfire and the hangfire slqserver dlls. When I added these visual studios asks you if you want to overwrite OWIN and Newtonsoft. Click no and there will be no issues with referencing versions.

    Hope this helps

    Kind Regards

    David

  • Q-ten 16 posts 116 karma points
    Jan 09, 2018 @ 09:42
    Q-ten
    0

    Hi,

    I'm posting here because this thread is generally about getting Hangfire to work with Umbraco and clearly people have gotten past where I find myself.

    I have a problem with this line:

    var cs = ApplicationContext.Current.DatabaseContext.ConnectionString;
            GlobalConfiguration.Configuration.UseSqlServerStorage(cs);
    

    The connection string is fine. It's the same as in my Web.Config. But Hangfire seems to fail at connecting to the DB. The connection string is:

    Data Source=|DataDirectory|\Umbraco.sdf;Flush Interval=1;
    

    The exception says that Flush Interval is not a valid parameter. Removing it doesn't help - complains more generally about not being able to connect.

    I'm using the standard local db (SQL CE), which I think is the problem.

    What I don't get is how everyone else seems to be using it ok with no problems. If the CE DB is not supported, how could anyone use it? Is there a way round it like an additional DLL I could get or something?

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Jan 09, 2018 @ 09:58
    Nik
    0

    Hi Q-ten,

    That is exactly the issue, Hangfire doesn't support SQL CE so it throws an error there. If you migrate to full SQL or to a localDB I believe it should work for you.

    Nik

Please Sign in or register to post replies

Write your reply to:

Draft