LuaScripts and Error Handling

Good Day All,
Looking to do some error handling within Orthanc scripts. I have a script which OnStoredInstance I forward to a peer (SendToPeer(jpeg2kInstance[‘ID’], ‘PEERNAME’))
I am looking to add some error handling, for example if the instance fails to send, I would like to send an alert or possibly HTTPGet to notify. I have configured error handling using call but I believe because it isn’t LuaContext.cpp but instead HttpClient.cpp and StorePeerCommand.cpp that are being called error handling wont work. Has anyone figured out a way around this or how to perform error handling correctly? Thanks!

Final Goal: When sending to Peer, if send to peer fails, perform HttpGet to an Interface engine which will process a send retry.

Thanks All!

Not sure to understand your exact issue. However, instead of using SendToPeer(…), you might want to try something like

local response = RestApiPost(‘/peers/PEERNAME/store’, jpeg2kinstance[‘ID’])

This way, you’ll get some feedback but I can’t tell you what you’ll get in response in case the /store fails. Give it a try.

Thanks. I am trying that, but still not getting any response. For example, this is my call:

if pcall(funcSendCurrent,varInstanceID) then
PrintRecursive(“Current " … tags[‘Modality’] … " Image Has Arrived. Forwarding”)
else
print(“Failure”)
end

Which calls:

function funcSendCurrent (varInstanceID)
local response = SendToPeer(varInstanceID, ‘PEER’)
end

But I am not getting Error in the logs. Instead I am getting:

E0529 09:55:17.883695 HttpClient.cpp:223] libCURL error: Couldn’t connect to server
E0529 09:55:17.883695 StorePeerCommand.cpp:80] Unable to forward to an Orthanc peer in a Lua script (instance 703682a9-4b5274e8-f6f69bs3-48fd9510-4c237e02, peer https://peer.servername.com:443/): Error in the network protocol

What I am hoping to do, is use the PCAL L to issue an alert outside of the log file using the if/else statement, but it does not seem to call the else on failure.

your function does not seem to return any value … can you just print the ‘response’ so you’ll see if you can use the value to detect a failure or not (sorry, I’m too lazy to try it myself or look into the code).

Best regards

Alain.

Hi Alain,
So I am trying something like this in my Lua script.

local varSendTo = SendToPeer(modifiedId, ‘TRANSFER’)
print(“Message:” … varSendTo)

If I get an error sending, it does not seem to log correctly, only logging the ID.

W0608 14:01:56.289720 LuaContext.cpp:103] Lua says: Message:2a26656d-435e2c78-6b2a555b-5555d1e3-b3e550e6
E0608 14:01:56.632715 HttpClient.cpp:223] libCURL error: Couldn’t connect to server
E0608 14:01:56.632715 StorePeerCommand.cpp:80] Unable to forward to an Orthanc peer in a Lua script (instance 2a26656d-435e2c78-6b2a555b-5555d1e3-b3e550e6, peer https://address.address.com:443/): Error in the network protocol

Then, I’m afraid you won’t be able to perform error handling from Lua.

Note that Sébastien is currently working on a new Job engine that would probably allow you to handle errors (work to be completed in a few weeks).

Lua has actually never been foreseen to implement “complex” behaviors. If you want to implement some complex logic, we recommend to write an external script that communicate with Orthanc through its Rest API and that actually drives Orthanc. You can detect the events like “StableStudy”, “InstanceStored”, … by calling the /changes route regularly and therefore have some code triggered at the same time as your current Lua script.

Great, thanks. I’ll wait to see the new job engine and see if that will work. It isn’t an emergency but would be great to be able to add re-tries if there are network connectivity issues, thanks.

Dear Bryan,

The source code of the revamped jobs engine is now complete, and is available in the “jobs” branch of the Orthanc repository:
https://bitbucket.org/sjodogne/orthanc/src/jobs/

Precompiled binaries for testing are available as well:

There is a new “jobs” tab in the Orthanc Explorer to manage the jobs, that internally calls the “/jobs” URI in the REST API. You are obviously kindly invited to test these new features, that will be part of forthcoming Orthanc 1.4.0.

Regarding your question about Lua scripting, we have introduced 3 additional callbacks that are respectively called when a job is submitted, succeeds, or fails:

function OnJobSubmitted(jobId)

print('Job submitted: ’ … jobId)

end

function OnJobSuccess(jobId)

print('Job success: ’ … jobId)

end

function OnJobFailure(jobId)

print('Job failure: ’ … jobId)

end

Any feedback is obviously welcome!

Regards,
Sébastien-

Wonderful! I’ll put it through its paces and let you know.

Hi Sebastien,
I have started testing. Looks great. It is great to be able to see what is happening behind the scenes.

I have an issue with Jobs engine considering a success, even though I am getting storepeeroperation.cpp and httpclient.cpp errors from my Lua script. Is this how it should be or would you like logs to investigate? Thanks.

Gentle bump on this. I know this module is still in development, just looking for confirmation if it is as designed that the job engine would be successful with storepeeroperation.cpp and httpclient.cpp errors within a Lua script, or how I would identify/process error from sendtopeer. Thanks.

Hello Bryan,

The Lua commands “SendToModality()” and “SendToPeer()” continuously feed one job of type “SequenceOfOperations” that is automatically created. This job can be seen in Orthanc Explorer (its description is set to “Lua”, as can be seen in the detailed content of the job).

Such a “SequenceOfOperations” is continuously active and cannot be stopped, as it would break auto-routing applications as soon as one error is encountered. This is one of the reasons for which these primitives are reserved to basic scenarios, as explained in the Orthanc Book:
http://book.orthanc-server.com/users/lua.html#important-remarks-about-auto-routing

If you want to have more control and more information if sending to peers or modalities, you are invited to call the REST API from the Lua script in order to declare the transfer to be achieve. This will create separated jobs, that can be individually monitored, and that will stop in the case of an error.

HTH,
Sébastien-

Hi Sebastien,
Thanks for the info. I noticed in another post you noted that sendtopeer was depreciated, which I was unaware of. Thanks, this info is helpful. So from now on I should be using /modalities/…/store I imagine?

Actually, looks like I spoke too soon. It looks as though you cannot do from REST API /modalities/peername/store with peers. Is there an alternatively comparable method?

Found it. Sorry. I was going off an old API Reference. /peers/{peer}/store is the command. Thanks.