require 'json' require 'resque' require 'resque-lonely_job' require 'net/http' require 'digest/md5' module JamRuby # http://blog.bignerdranch.com/1643-never-use-resque-for-serial-jobs/ # periodically scheduled to find sources that need to be brought down, or alternatively, it seems the client failed to start sourcing class IcecastSourceCheck extend Resque::Plugins::LonelyJob @queue = :icecast_source_check @@log = Logging.logger[IcecastSourceCheck] def self.perform @@log.debug("waking up") JamWebEventMachine.run_wait_stop do IcecastSourceCheck.new.run end @@log.debug("done") end def run # if we haven't seen updated_at be tickled in 5 minutes, but config_changed is still set to TRUE, this record has gotten stale IcecastMount.find_each(lock: true, :conditions => "sourced_needs_changing_at < (NOW() - interval '#{APP_CONFIG.icecast_max_sourced_changed} second')", :batch_size => 100) do |mount| if mount.music_session_id mount.with_lock do handle_notifications(mount) end end end end def handle_notifications(mount) if mount.listeners == 0 && mount.sourced # if no listeners, but we are sourced, then ask it to stop sourcing @@log.debug("SOURCE_DOWN_REQUEST called on mount #{mount.name}") mount.update_attribute(:sourced_needs_changing_at, Time.now) # we send out a source request, so we need to update the time mount.notify_source_down_requested elsif mount.listeners > 0 && !mount.sourced # if we have some listeners, and still are not sourced, then ask to start sourcing again @@log.debug("SOURCE_UP_REQUEST called on mount #{mount.name}") mount.update_attribute(:sourced_needs_changing_at, Time.now) # we send out a source request, so we need to update the time mount.notify_source_up_requested end end end end