8 Replies Latest reply on Feb 2, 2014 10:04 AM by Serhit

    i'd like to poll a website regularly. what's a good way?

    victorvodka Level 1

      I tried using Rob Cole's example in Cooksmarks to send data to lightroom://[plugin_id] but it just never worked for me. so now i'd just like to do some regular polling of a website from within a plugin. what might be a good way to do that?  i tried running an asynchronous task calling a function with a sleep in it that kept hitting a site every second, but i got yelled at by the Lightroom Lua interpreter about trying to sleep while not in a task (i thought i was in a task, but that's a separate issue).

        • 1. Re: i'd like to poll a website regularly. what's a good way?
          areohbee Level 5

          victorvodka wrote:

           

          I tried using Rob Cole's example in Cooksmarks to send data to lightroom://[plugin_id] but it just never worked for me.

          Are you saying cookmarks didn't work, or your cookmarks-based derivative didn't work?

           

          Anyway, the other approach should work too: in async task, use LrHttp.get periodically.

           

          PS - Wrap things in task at the outermost level, (e.g. button handlers, menu handlers, ... ) and you'll never have to worry about tasking (well, except for re-entrancy I mean), i.e. you'll only need to spin tasks for concurrent activitiy, but not just to satisfy the Lr/Lua interpreter..

           

          I recommend LrFunctionContext.postAsyncTaskWithContext instead of LrTasks.startAsyncTask for such cases, so you can more easily deal with error handling and re-entrancy, and make property tables if you need them...

           

          (if using Elare Plugin Framework, just use a call and set async=true)

           

          R

          • 2. Re: i'd like to poll a website regularly. what's a good way?
            victorvodka Level 1

            I only tested my derivative.  If I navigated to a lightroom:// url, it would open Lightroom even if it was closed, but a logger placed inside URLHandler never logged anything.  here was the content of urlHandler.php, which was declared in Info.lua as URLHandler = "urlHandler.lua",  :

            return {
                URLHandler = function(url)
                 local url=""
                    if url:sub( 1, 1 ) == '"' then -- double-quote wrapped format.
                        url =url:sub( 2, -2 ) ) -- strip double-quotes.
                    else
                    
                    end
                      local LrLogger = import 'LrLogger'
            
                      local myLogger = LrLogger( 'stinkermanstank' ) 
                      myLogger:enable( "logfile" )
                      myLogger:trace("url:" .. url)
                end
            }
            
            

            Now, as for polling, do i just have do loop that calls an asynchronous task every time, say, the seconds advance by one?

            • 3. Re: i'd like to poll a website regularly. what's a good way?
              areohbee Level 5

              You need to delete this line:

               

              local url = "" -- hides url parameter

               

              Still, I would have expected a log.

              Did you try app:log( "asdf" ), or

              Debug.pause(), or

              LrDialogs.message( "hi" )

               

              Does cookmarks plugin work?

               

              To be clear: reloading plugin does not terminate background tasks: you need to take explicit responsibility for background task termination.

               

              A LrShutdownPlugin module is essential when you have persistent background tasks in your plugin. In it, set a global shutdown flag, and in your long-running tasks, check the flag and return if set.

               

              PS - One way to have a one-second periodic task (error protected and recursion-guarded, vocally):

               

              app:call( Call:new{ name="My Periodic Task", async=true, guard=App.guardVocal, main=function( call )

                   app:sleep( math.huge, 1, function() -- sleep until shutdown, e.g. plugin reloaded - very important.

                        -- do something.

                   end )

              end, finale=function( call )

                   app:log( "^1 returned, status: ^1, message: ^2.", call.name, call.status, call.message )

              end } )

               

              or (not error protected nor recursion guarded):

               

              LrFunctionContext.postAsyncTaskWithContext( "My Periodic Task", function( context )

                    while not _G.shutdown do -- you'd better make sure global shutdown flag is set when plugin reloaded, otherwise these tasks will gang up, along with the entire plugin context (environment) which they're affiliated with, i.o.w. your plugin will load another copy, instead of completely unloading previous instance.

                         -- do something

                         LrTasks.sleep( 1 )

                     end

              end )

               

              Note: if Adobe improved task cleanup in Lr5 SDK, I'd not know, but don't assume if no task gang up in Lr5 that they wouldn't in Lr4 or Lr3...

              • 4. Re: i'd like to poll a website regularly. what's a good way?
                Serhit Level 1

                According to my experience lightroom://[plugin_id] may not work due to issues with installation of Lightroom - namely, protocol is not registered properly. Check if you have a key HKEY_CLASSES_ROOT\lightroom If not, protocol lightroom:// will not work.

                 

                Below is the except from the Registry from my PC.

                 

                ------------------------

                Windows Registry Editor Version 5.00

                 

                 

                [HKEY_CLASSES_ROOT\lightroom]

                "URL Protocol"=""

                @="\"URL:Lightroom Protocol\""

                 

                 

                [HKEY_CLASSES_ROOT\lightroom\DefaultIcon]

                @="\"lightroom.exe,1\""

                 

                 

                [HKEY_CLASSES_ROOT\lightroom\shell]

                 

                 

                [HKEY_CLASSES_ROOT\lightroom\shell\open]

                 

                 

                [HKEY_CLASSES_ROOT\lightroom\shell\open\command]

                @="C:\\Program Files\\Adobe\\Adobe Photoshop Lightroom 5.2\\Lightroom.exe \"%1\""

                ------------------------

                • 5. Re: i'd like to poll a website regularly. what's a good way?
                  areohbee Level 5

                  Hi Serhit,

                  Serhit wrote:

                   

                  According to my experience..

                  out of curiosity - did this happen to you, or one of your plugin users?

                   

                  The registry excerpt you posted is one that works, right?

                   

                  I wonder what it look like if it doesn't work, or how/why it gets hosed up...

                   

                  Cheers,

                  Rob

                  • 6. Re: i'd like to poll a website regularly. what's a good way?
                    Serhit Level 1

                    One of the users for my plugin reported that this feature does not work for him, however, many other users had no issues.

                     

                    Yes, this registry example should be working fine. I tested myself by removing (renaming) and placing back the key HKEY_CLASSES_ROOT\lightroom.

                    As expected, without the key LR is not launched if protocol lightroom:// is used, if regestry key is back - LR accepts the submission.

                    • 8. Re: i'd like to poll a website regularly. what's a good way?
                      Serhit Level 1

                      Actually, thank you - I learned how to use this URL from your examples