Copied to clipboard

Flag this post as spam?

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


  • David Armitage 505 posts 2073 karma points
    Nov 26, 2020 @ 04:41
    David Armitage
    0

    Umbraco V8 uSync trigger import via url or api endpoint

    Hi All,

    I wondered if there was some way of triggering uSync import via a public url or if not is there an API I could use to create my own endpoint to do this?

    What I am trying to achieve is adding a Url to my deployment script. The idea is once I've done a deployment I would trigger a url which would then automatically run the update.

    I understand there are some settings to set this going at start up but I don't really want to do this every time there is an IIS restart etc. I would be great if I could just trigger this the once without having to log into the backend and trigger this manually.

    Thanks in advanced.

    Kind Regards

    David

  • Steve Morgan 1346 posts 4453 karma points c-trib
    Nov 26, 2020 @ 06:13
    Steve Morgan
    1

    Hi David,

    What I've done successfully in the past is to add a usync.stop file after the deployment is successful.

    Then your lovely usync changes are imported but for future restarts the site skips any overhead.

    It's just a simple text file in the usync directory. Worked a treat!

    HTH

    Steve

  • Steve Morgan 1346 posts 4453 karma points c-trib
    Nov 26, 2020 @ 06:16
    Steve Morgan
    2

    There was also usync.once - add this and it runs once and renames to .stop

    https://github.com/KevinJump/uSync/issues/29

    I think this is still in the current version.

  • Kevin Jump 2310 posts 14695 karma points MVP 7x c-trib
    Nov 26, 2020 @ 06:49
    Kevin Jump
    100

    As Steve says usync.once will do a single sync, so you can have it happen only the first time.

    There is no public (unauthenticated) end point, (you don't want random's resyncing your site!). but you could add one,

    You could take code for the authenticated API controller (the one the dashboard uses). and put this in your own controller. ?

    https://github.com/KevinJump/uSync8/blob/v8/8.7-main/uSync8.BackOffice/Controllers/uSyncDashboardApiController.cs#L127

        public IEnumerable<uSyncAction> Import(uSyncOptions options)
        {
            var hubClient = new HubClientService(options.ClientId);
            return uSyncService.Import(settings.RootFolder, options.Force, new SyncHandlerOptions()
            {
                Group = options.Group
            },
            callbacks: hubClient.Callbacks());
        }
    

    of the options here:

    • callbacks can be null, its uses as part of the signalR UI updating - not needed
    • group can also be omitted (so it becomes new SyncHandlerOptions()) group lets you only sync either settings ,or content - if omitted it does all
    • force : if true everything is imported even if it hasn't changed - this is much slower, so unless you have a real reason, force = false.

    I would personally put some authentication around that (maybe a shared key/id or something in the api call?).

  • David Armitage 505 posts 2073 karma points
    Nov 26, 2020 @ 07:52
    David Armitage
    0

    Hey Guys,

    Thanks for getting back to me so quickly. Some good solutions.

    Kevin I was actually thinking the same. I sort of found this endpoint in the packages folder in one of the angularJs files. I was planning on jumping on the top of that. But yeah I was unsure what the options were.

    Thanks for explaining.

    I get you about the public URL. I was thinking the same. I would create some kind of auth. Even if just an encrypted query string or something.

    Do you know where I might find the client Id? I think this is the only bit I am unsure about.

    Thanks again.

    David

  • Kevin Jump 2310 posts 14695 karma points MVP 7x c-trib
    Nov 26, 2020 @ 08:11
    Kevin Jump
    0

    Hi,

    you don't need the clientId it is the internal id signalR uses when connecting to a browser to send messages - so unless you have a signalR end point you want to get the update messages for, you can choose not to create the hub and pass null for the callbacks.

    if you do want signalR - well it gets complex but uSync's code is here. https://github.com/KevinJump/uSync8/blob/v8/8.7-main/uSync8.BackOffice/App_Plugins/uSync8/settings/uSyncController.js#L337

    and a slightly tidier implimentation for reference is here. https://github.com/KevinJump/DoStuffWithUmbraco/tree/master/Src/DoStuff.Core/SignalR

  • David Armitage 505 posts 2073 karma points
    Nov 26, 2020 @ 09:18
    David Armitage
    0

    Perfect. I will give this a go.

    Thanks Kevin, Steve

  • Steve Morgan 1346 posts 4453 karma points c-trib
    Nov 26, 2020 @ 08:37
    Steve Morgan
    1

    What I probably wasn't clear about is that usync.once file is added as part of the deployment. So no messing about having to remember to add this file to sourcecode, just create it in your deployment script - it's so simple I forgot we'd even did it.

    I think you're over complicating things with API calls.. unless I'm missing something complicated about your setup? When you do a deployment you'd want your usync changes to run on startup before your new code starts firing...?

    And, as usual, great stuff from Kevin here.

    Steve

  • David Armitage 505 posts 2073 karma points
    Nov 26, 2020 @ 09:20
    David Armitage
    0

    Hi Steve,

    In that case your solution sounds great. If its just a point of adding this into the deployment then that is fine.

    I will also give this a go.

  • Steve Morgan 1346 posts 4453 karma points c-trib
    Nov 26, 2020 @ 10:47
    Steve Morgan
    0

    Thanks - not really my solution. It's Kevin's - he built the logic after all :)

  • JeanetteDavis 1 post 71 karma points
    Feb 04, 2021 @ 05:09
    JeanetteDavis
    0

    As S

    teve says usync.once will do a single sync, so you can have it happen only the first time.

    There is no public (unauthenticated) end point, (you don't want random's resyncing your site!). but you could add one,

    You could take code for the authenticated API controller (the one the dashboard uses). and put this in your own controller. ?

    https://github.com/KevinJump/uSync8/blob/v8/8.7-main/uSync8.BackOffice/Controllers/uSyncDashboardApiController.cs#L127

    public IEnumerable<uSyncAction> Import(uSyncOptions options)
    {
        var hubClient = new HubClientService(options.ClientId);
        return uSyncService.Import(settings.RootFolder, options.Force, new SyncHandlerOptions()
        {
            Group = options.Group
        },
        callbacks: hubClient.Callbacks());
    } of the options here:
    

    callbacks can be null, its uses as part of the signalR UI updating - not needed group can also be omitted (so it becomes new SyncHandlerOptions()) group lets you only sync either settings ,or content - if omitted it does all force : if true everything is imported even if it hasn't changed - this is much slower, so unless you have a real reason, force = false. I would personally put some authentication around that (maybe a shared key/id or something in the api call?).

    I got really good information from this content. thanks for sharing.

    Read more

Please Sign in or register to post replies

Write your reply to:

Draft