What is the best way to implement resending instances?

Hello. We want to implement resending instances logic. We just send them on NEW_INSTANCE callback asynchronously. And if it is failed to send, just add instance id to file and resend each time on new study uploads:

def OnChange(changeType, level, resource):
    if changeType == orthanc.ChangeType.NEW_INSTANCE: 
        orthanc.RestApiPost('/peers/server/store', json.dumps({
           "Resources" : [instance_id],
           "Synchronous":False
        }))
            
     if changeType == orthanc.ChangeType.JOB_FAILURE:
	    print(resource) # this is failed job id

     if changeType == orthanc.ChangeType.STABLE_STUDY:
     	# logic to resend all previous failed instances

but JOB_FAILURE callback gives failed job id, not instance id. I can make request to get job details and grab instance id, but at that time job can be deleted according to JobsHistorySize.
For example If i set JobsHistorySize to 1000, and 5 instances failed and 1000 instances succeed after it, my first 5 failed jobs will be deleted and i can’t resume it or get instance id in job details.

May be any other better ways?

We use dedicated Orthanc instances for that with this kind of lua script.

The same kind of OrthancForwarder scripts also exists in python as part of this library.