OnStableStudy infinite loop

Hi,
I’m trying to merge a study based on “OnStableStudy”.
But it turns into an infinite loop because merging cause the study to be unstable again.
What would be the apropriate way to prevent the loop ?

With OnStoredInstance it’s possible to use :
if origin[‘RequestOrigin’] ~= ‘Lua’

What would be the equivalent with OnStableStudy ?

Here is the code :

function OnStableStudy(studyId, tags, metadata)
local command = {}
command[‘Resources’] = {‘04e8b1d4-5b4dba44-25a05683-e70aaf9b-c23529d6’}
command['KeepSource '] = true
RestApiPost(‘/studies/’… studyId …‘/merge’, DumpJson(command, true))
end

Thank you

According to https://groups.google.com/g/orthanc-users/c/GuFqiZtkwtg/m/PGu2fM5LCAAJ
One way would be to add a metadata to the study with the help of OnStoredInstance. Is there a more direct approach ?

Hi Thibaut,

No, you still need to use a custom metadata to store this info.
Note that you could store “merge”: “true” metadata at the study-level once you have merged it to avoid a second processing (easier to implement than the solution suggested in the referenced post).

HTH,

Alain.

Thank you,
I went with the code :
function OnStableStudy(studyId, tags, metadata)
if metadata[‘1024’] ~= ‘merged’ then
local command = {}
command[‘Resources’] = {‘8d485228-6ba0540e-2ac6db4c-66996272-51aeb1e2’}
command[‘KeepSource’] = true
RestApiPut(‘/studies/’… studyId … ‘/metadata/1024’, ‘merged’)
local posted = ParseJson(RestApiPost(‘/studies/’… studyId …‘/merge’, DumpJson(command, true)))
end
end

The merge API doesn’t return the merged instance new uuid.
Is there a way to catch this uuid ?
So far I’m thinking to catch it from the OnStoredInstance function with yet an other metadata at the instance level. But that feel a little cumbersome.