Help with Hangfire in Umbraco 10
# help-with-umbraco
c
Hi I am looking to do another proof of concept. I would like to set up an Umbraco 10 site with Hangfire installed and some simple tasks to run. Is anyone here able to give me some pointers and gotchas please?
i
It's been a long time but what I used to do is just install it and configure all my jobs etc. using a composer. I used the same connection string as for Umbraco, but you could create a seperate login and user with only access to the Hangfire schema, if you don't need to access Umbraco data.
a
You can try @Sebastiaan 's Hangfire package: https://marketplace.umbraco.com/package/cultiv.hangfire
I haven't tried it my self, but I think it installs the package. You can see a task example on the link I posted.
c
Thanks, that's where I'm at so far. I've also just found this repo by @Jason https://github.com/JasonElkin/Hangfire-Umbraco-Example
s
I fully endorse Anders' recommendation of that package 😉
a
@CodeSharePaul yes, as tasks are running in the background, you need to use IUmbracoContextFactory to create an Umbraco context (if you need that)
s
c
Thanks both. I am planning to use it for imports. Get the data from an third party system and then import it into Umbraco. Do you have an example task or approach?
s
One tip: Please only make it an Umbraco node if the content is actually going to be edited by actual editors. We often see the mistake of just indiscriminately importing things so it can be dropped into a template, but it's a LOT of overhead making something into an actual node and it doesn't scale well.
Other than that, I think you have all the building blocks.
d
@Nurhak Kaya can you provide any guidance as you've used Hangfire recently
c
Thanks @Sebastiaan , we have thought about importing the data to a custom table and the client has said they might want to edit some of the fields and they want the flexibility to do so. The fact that content version clean up is baked in will help a lot here.
Ooh yes please @Nurhak Kaya , thanks @Deleted User
j
h
We do this a lot for our CRM integrations, mostly for products if you need any pointers
n
Hello @CodeSharePaul , @Deleted User has just told me that you were asking for some help about Hangfire
shall we do a screen share?
and see if I can help?
I can definitely help with this
I can recommend @Sebastiaan 's package for Umbraco v10+ websites. It simplifies things a lot.
All details are in my skrift article btw, but happy to do a screen share and help @CodeSharePaul if you still need help. Just give me a shout.
c
Yes please @Nurhak Kaya
m
if it's minimal editing, custom tables rather than nodes.. UiOmatic or Konstruct?
a
@CodeSharePaul if you do go with importing content as content, my old Skrift article might also be relevant, although it was written for Umbraco 7: https://skrift.io/issues/importing-external-data-as-content-in-umbraco/ Might be a bit different though if your client wants to edit the imported data. We typically import some basic data the client can't change, and then have some additional properties that they can edit - eg. block list content in U10.
c
Thanks for the article @Anders Bjerner and thanks for the screenshare to help me get setup for success @Nurhak Kaya
We have considered this too. Currently happy with creating content items. We will make the import clever enough to check if anything has actually changed before importing it too
@Anders Bjerner that article is still relevant and has some great tips such as the content service and to dictionary.
n
You're very welcome Paul. I am glad I could help.
a
Yes - I use that all the time. The article was based on an Umbraco 7 site I did with losts of imported content. I just did a rewrite of the site for Umbraco 10, and use the same principles in the import.
n
Another great Skrift article, thanks @Anders Bjerner .
m
another thought if you are going nodes.. cmsImport.. for €199 has scheduled imports (and adds json/sql datasource) you can extend via skip notification if you wanted to do something clever on matching primary key for subsequent import.. https://soetemansoftware.nl/cmsimport you can also add custom field convertors, for sanitising import fields too.. stripping html for instance.
Copy code
csharp
 public void Handle(RecordSkippedNotification<IContent> notification)
 {
     if (notification.ProviderAlias == "ContentImportProvider")
     {
         var scopeProps = new Dictionary<string, object> { { "@ImportFields", notification.Items } };
         using (_logger.BeginScope(scopeProps))
         {
             _logger.LogInformation("RecordSkipped content {Ref} {PrimaryKey}", notification.Item.Name, notification.PrimaryKeyValue);
         }
     }
 }
Copy code
csharp
 [FieldProvider(PropertyEditorAlias = "Umbraco.TextArea")]
 public class TextAreaFieldProvider : IFieldProvider
 {
     public object Parse(object value, IContentBase importedItem, ImportPropertyInfo property, FieldProviderOptions fieldProviderOptions)
     {
         if (property?.PropertyName == "Contact")
         {
             var stripped = value.ToString()?.StripHtml().Trim("\r\n");                
             return stripped ?? string.Empty;
         }
         else
         {
             return value;
         }
     }
 }
c
Thanks for sharing @Mike Chambers We are sticking to our own custom import at this stage but hopefully others who find this thread will find your tip useful
If anyone is interested I created a proof of concept project here. https://github.com/prjseal/Hangfire-Import-Proof-Of-Concept/
2 Views