How do I handle this race condition?
I'm using Uploadcare to allow my users to upload files. When the upload completes, Uploadcare returns a file UUID to my page which I use to create an Upload
record in my DB with a remote AJAX call.
At the same moment Uploadcare also send a webhook to my endpoint which enables me to decide if the file requires processing or not (video conversion).
Problem is, the webhook will sometimes hit my app before the upload record is created so the endpoint function can't find the upload. There's no way for me to delay the sending of the webhook or for me to create the upload any earlier in the process.
What are my options for solving this?
Sounds like a terrible webhook lol.
You can always have the webhook schedule a job for 30 seconds in the future or something. If it still can't find it, schedule another job 30 seconds in the future, until it finds it or hits a maximum number of retries.
Thanks Chris. I haven't done any type of scheduling stuff before. Any good resources or videos for this?
Just your standard ActiveJob features: MyJob.set(wait: 30.seconds).perform_later
And in the job, try to find if the file exists, if not, schedule the job again with the same args.
Roughly:
class MyJob < ApplicationJob
def perform(file_id, retry_count=0)
file = lookup_file(file_id)
if file.present?
# process
elsif retry_count < 10 # maximum number of retries
MyJob.set(wait: 30.seconds).perform_later(file_id, retry_count + 1)
end
end
end