lib/rubyrun/rubyrun_rss__.rb in rubyrun-0.9.6 vs lib/rubyrun/rubyrun_rss__.rb in rubyrun-0.9.7

- old
+ new

@@ -39,11 +39,10 @@ @rss_filename = rss_filename @html_filename = html_filename @apps_name = Rails::Configuration.new.root_path.split('/').last @rss_xml_destination = "#{@directory}/#{@rss_filename}" create_channel_content unless File::exists?(@rss_xml_destination) - @rss = load_rss_content end # Create the RSS channel def create_channel_content content = RSS::Maker.make(RUBYRUN_RSS_VERSION) do |m| @@ -68,19 +67,32 @@ File.open("#{@directory}/#{filename}", 'w') { |file| file.puts html_content } item = RSS::Rss::Channel::Item.new item.title = sprintf(title, "#{$rubyrun_startup_id_type} #{$rubyrun_startup_id}",Time.now.strftime("%H:%M:%S")) item.description = sprintf(description, @apps_name, "#{$rubyrun_startup_id_type} #{$rubyrun_startup_id}", Time.now.strftime("%H:%M:%S")) item.link = "http://#{$rubyrun_host_with_port}/#{RUBYRUN_RSS_FOLDER}/#{filename}" - sleep(rand(3)) - @rss = load_rss_content - remove_old_item(@rss) if @rss.items.length == $rubyrun_report_shift_age - @rss.items << item + acquire_lock + rss = load_rss_content + remove_old_item(rss) if rss.items.length == $rubyrun_report_shift_age + rss.items << item File.open(@rss_xml_destination,"w") do |f| - f.write(@rss) - end + f.write(rss) + end + release_lock end private + + # Acquire a lock so that only one Ruby process can write to the RSS XML file + def acquire_lock + @lock_file = File.new(File.join(@directory,'lock'),'w') + @lock_file.flock(File::LOCK_EX) + end + + # Release a lock after writing the RSS XML file content + def release_lock + @lock_file.flock(File::LOCK_UN) + @lock_file.close + end # Load the existing RSS XML file. Create a new channel if the channel is blank def load_rss_content content = "" # raw content of rss feed will be loaded here open(@rss_xml_destination) do |s| content = s.read end \ No newline at end of file