How do I handle unsccessful cases where an object is created in order to upload a video to it?

So I have a video.rb model, that allows users to upload a video.

The videos are being uploaded to YouTube. In order to save the YT link to the record, I first need to create the object, handle the upload via the YT API, then update the record with the YT link.

This works fine, except for errors/issues with the upload. If, for whatever reason, the video doesn’t get successfully uploaded to YT and the user abandons the upload process, the unnecessary video record is left in the DB.

So, ideally, what I would love to do is after something (either some time or some event) I check to see if the record had the YT link successfully added to it (i.e. if the video record actually became a “legit” video record with an actual video).

If it didn’t become a “legit video”, then I want to clean up the DB and remove that dummy record.

What’s the best way to approach this?

This is the process in my app:

  • Video Created (VideosController#Create)
  • Video Upload (VideosController#Upload)
  • Video Save (VideosController#Save) - this then updates the attributes for the video with the YT URL.

I would probably go with a super simple MVP approach to this and just make a rake task that is scheduled via the Heroku Scheduler to run everyday at like 11pm or something.

Something like:

# Assumes you are storing the URL as a string { |v| v.destroy unless v.url != "" }

Idk I would keep it super basic and just do something like that. Maybe create a class if you wanted to take it one step further. Also if you are doing TDD you definitely want a class or some way to test it so a rake task won’t work. You could do:

:task => :environment do
  cleaner =

class VideoCleaner
  def initialize ; end
  def begin { |v| v.destroy unless v.url != "" }

Those method names could use some work, but that’s the basic gist of how I’d handle that. Super simple and basic is always my preference.

This is definitely an interesting approach. I hate the idea of relying on a cron job/rake task to run to clean up my DB though.

I would rather try and nip this in the bud, rather than clean it up after.

God forbid this scheduler not run (without me noticing) for a month or two…you can imagine how that ends.

So then, I have to build in edge-case support - i.e. have another rake task that runs if this rake task hasn’t run, and so on and so forth.

You could try and handle it the same way the discourse/facebook/twitter handles abandoning a post.If the user navigates away from the page, confirm they don’t intend to upload etc and trigger the deletion on a case by case basis that way?