diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/application_controller.rb | 11 | ||||
-rw-r--r-- | app/models/info_request.rb | 21 | ||||
-rw-r--r-- | app/models/purge_request.rb | 27 |
3 files changed, 36 insertions, 23 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0ec8e206e..0d0cca3e4 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # controllers/application.rb: # Parent class of all controllers in FOI site. Filters added to this controller # apply to all controllers in the application. Likewise, all the methods added @@ -543,16 +544,6 @@ class ApplicationController < ActionController::Base return country end - def quietly_try_to_open(url) - begin - result = open(url).read.strip - rescue OpenURI::HTTPError, SocketError, Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::EHOSTUNREACH - logger.warn("Unable to open third-party URL #{url}") - result = "" - end - return result - end - # URL generating functions are needed by all controllers (for redirects), # views (for links) and mailers (for use in emails), so include them into # all of all. diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 3a1f4b9f3..9b129e74b 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -453,7 +453,6 @@ public # An annotation (comment) is made def add_comment(body, user) comment = Comment.new - ActiveRecord::Base.transaction do comment.body = body comment.user = user @@ -1043,18 +1042,14 @@ public return ret end - before_save(:mark_view_is_dirty) - def mark_view_is_dirty - self.view_is_dirty = true - self.save! - end - - def self.purge_varnish - for info_request in InfoRequest.find_by_view_is_dirty(true) - url = "/request/#{info_request.url_title}" - purge(url) - info_request.view_is_dirty = true - info_request.save! + before_save :purge_in_cache + def purge_in_cache + if !MySociety::Config.get('VARNISH_HOST').nil? && !self.id.nil? + # we only do this for existing info_requests (new ones have a nil id) + req = PurgeRequest.new(:url => "/request/#{self.url_title}", + :model => self.class.base_class.to_s, + :model_id => self.id) + req.save() end end end diff --git a/app/models/purge_request.rb b/app/models/purge_request.rb new file mode 100644 index 000000000..a96d0f39e --- /dev/null +++ b/app/models/purge_request.rb @@ -0,0 +1,27 @@ +# models/purge_request.rb: +# A queue of URLs to purge +# +# Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved. +# Email: francis@mysociety.org; WWW: http://www.mysociety.org/ +# + +class PurgeRequest < ActiveRecord::Base + require 'open-uri' + def self.purge_all + for item in PurgeRequest.all() + item.purge + end + end + + def purge + config = MySociety::Config.load_default() + varnish_url = config['VARNISH_HOST'] + result = quietly_try_to_purge(varnish_url, self.url) + if result == "200" + self.delete() + end + end +end + + + |