require 'json'
require 'resque'
require 'resque-retry'
require 'net/http'
require 'digest/md5'

module JamRuby

  # periodically scheduled to find jam_tracks to cleanup
  class JamTracksCleaner
    extend Resque::Plugins::JamLonelyJob

    @queue = :jam_tracks_cleaner

    class << self 
      def log
        @log || Logging.logger[JamTracksCleaner]
      end

      def lock_timeout
        # this should be enough time to make sure the job has finished, but not so long that the system isn't recovering from a abandoned job
        1200
      end

      def perform
        # this needs more testing

        # let's make sure jobs don't stay falsely queued for too long.  1 hour seems more than enough
        JamTrackRight.where("queued = true AND (NOW() - signing_queued_at > '1 hour'::INTERVAL OR NOW() - updated_at > '1 hour'::INTERVAL)").update_all(queued:false)
        JamTrackMixdownPackage.unscoped.where("queued = true AND (NOW() - signing_queued_at > '1 hour'::INTERVAL OR NOW() - updated_at > '1 hour'::INTERVAL)").update_all(queued:false)

        return
        #JamTrackRight.ready_to_clean.each do |jam_track_right|
        #  log.debug("deleting files for jam_track_right #{jam_track_right.id}")
        #  jam_track_right.delete_s3_files
        #end
      end
    end        
  end

end