Permalink
Browse files

Add explicit support for retrying in the face of 'Session ID is inval…

…id' errors from the OpenX server
  • Loading branch information...
1 parent ea1176b commit 407a5bc64bc6df24ea1f20200d537283a50d2cb8 Dan Sketcher committed with aasmith Oct 23, 2009
View
@@ -4,7 +4,7 @@ class << self
#
# banner = OpenX::Invocation.view("Plumber")
#
- # banners = OpenX::Invocation.view("Plumber", :count => 2, :exclude_by_bannerid => true) ;nil
+ # banners = OpenX::Invocation.view("Plumber", :count => 2, :exclude_by_campaignid => true) ;nil
# banners.each do |banner|
# puts "Banner #{banner['bannerid']}"
# end; nil
@@ -17,7 +17,7 @@ def find(id, *args)
session = self.connection
server = XmlrpcClient.new2("#{session.url}")
if id == :all
- responses = server.call(find_all(), session.id, *args)
+ responses = server.call(find_all(), session, *args)
response = responses.first
return [] unless response
responses = [response]
@@ -34,7 +34,7 @@ def find(id, *args)
new(translate(response))
}
else
- response = server.call(find_one(), session.id, id)
+ response = server.call(find_one(), session, id)
new(translate(response))
end
end
@@ -72,24 +72,24 @@ def find(id, *args)
self.find_all = 'ox.getBannerListByCampaignId'
def initialize(params = {})
- raise ArgumentError unless params[:campaign_id] || params[:campaign]
+ raise ArgumentError.new("Missing campaign_id") unless params[:campaign_id] || params[:campaign]
params[:campaign_id] ||= params[:campaign].id
super(params)
end
def statistics start_on = Date.today, end_on = Date.today
session = self.class.connection
- @server.call('ox.bannerDailyStatistics', session.id, self.id, start_on, end_on)
+ @server.call('ox.bannerDailyStatistics', session, self.id, start_on, end_on)
end
def targeting
session = self.class.connection
- @server.call('ox.getBannerTargeting', session.id, self.id)
+ @server.call('ox.getBannerTargeting', session, self.id)
end
def targeting= targeting
session = self.class.connection
- @server.call('ox.setBannerTargeting', session.id, self.id, targeting)
+ @server.call('ox.setBannerTargeting', session, self.id, targeting)
end
end
end
View
@@ -62,12 +62,12 @@ def find(id, *args)
session = self.connection
server = XmlrpcClient.new2("#{session.url}")
if id == :all
- responses = server.call(find_all(), session.id, *args)
+ responses = server.call(find_all(), session, *args)
responses.map { |response|
new(translate(response))
}
else
- response = server.call(find_one(), session.id, id)
+ response = server.call(find_one(), session, id)
new(translate(response))
end
end
@@ -103,16 +103,16 @@ def save!
}
if new_record?
- @id = @server.call(self.class.create, session.id, params)
+ @id = @server.call(self.class.create, session, params)
else
- @server.call(self.class.update, session.id, params)
+ @server.call(self.class.update, session, params)
end
self
end
def destroy
session = self.class.connection
- @server.call(self.class.delete, session.id, id)
+ @server.call(self.class.delete, session, id)
@id = nil
end
@@ -38,7 +38,7 @@ class Campaign < Base
EXCLUSIVE = 3
def initialize(params = {})
- raise ArgumentError unless params[:advertiser_id] || params[:advertiser]
+ raise ArgumentError.new("Missing advertiser_id") unless params[:advertiser_id] || params[:advertiser]
params[:advertiser_id] ||= params[:advertiser].id
super(params)
end
@@ -2,6 +2,7 @@ module OpenX
module Services
class Session
attr_accessor :url, :id
+ attr_accessor :user, :password
def initialize(url)
@url = url
@@ -10,7 +11,15 @@ def initialize(url)
end
def create(user, password)
- @id = @server.call('ox.logon', user, password)
+ @user = user
+ @password = password
+ @id = @server.call('ox.logon', @user, @password)
+ self
+ end
+
+ def recreate!
+ raise "Unable to refresh Session" unless @user && @password
+ @id = @server.call('ox.logon', @user, @password)
self
end
View
@@ -54,7 +54,7 @@ def link_campaign(campaign)
session = self.class.connection
server = XmlrpcClient.new2("#{session.url}")
- server.call("ox.linkCampaign", session.id, self.id, campaign.id)
+ server.call("ox.linkCampaign", session, self.id, campaign.id)
end
# Unlink this zone from +campaign+
@@ -64,7 +64,7 @@ def unlink_campaign(campaign)
session = self.class.connection
server = XmlrpcClient.new2("#{session.url}")
- server.call("ox.unlinkCampaign", session.id, self.id, campaign.id)
+ server.call("ox.unlinkCampaign", session, self.id, campaign.id)
end
# Link this zone to +banner+
@@ -74,7 +74,7 @@ def link_banner(banner)
session = self.class.connection
server = XmlrpcClient.new2("#{session.url}")
- server.call("ox.linkBanner", session.id, self.id, banner.id)
+ server.call("ox.linkBanner", session, self.id, banner.id)
end
# Unlink this zone from +banner+
@@ -84,14 +84,14 @@ def unlink_banner(banner)
session = self.class.connection
server = XmlrpcClient.new2("#{session.url}")
- server.call("ox.unlinkBanner", session.id, self.id, banner.id)
+ server.call("ox.unlinkBanner", session, self.id, banner.id)
end
# Generate tags for displaying this zone using +tag_type+
def generate_tags(tag_type = IFRAME)
session = self.class.connection
server = XmlrpcClient.new2("#{session.url}")
- server.call("ox.generateTags", session.id, self.id, tag_type, [])
+ server.call("ox.generateTags", session, self.id, tag_type, [])
end
end
end
View
@@ -26,24 +26,52 @@ def init_server(uri)
#server.instance_variable_get(:@http).set_debug_output($stderr)
server
end
- protected :init_server
def new2(uri)
- @uri = uri
- @server = init_server(uri)
+ server = init_server(uri)
+ new(server, uri)
end
end
+ def initialize(server, uri)
+ @server = server
+ @uri = uri
+ end
def call(method, *args)
+ if args.first.is_a?(OpenX::Services::Session)
+ session = args.shift()
+ args.unshift(session.id)
+ begin
+ do_call(method, *args)
+ rescue XMLRPC::FaultException => sess_id_err
+ if sess_id_err.message.strip == 'Session ID is invalid'
+ session.recreate!
+ args.shift()
+ args.unshift(session.id)
+ do_call(method, *args)
+ else
+ raise sess_id_err
+ end
+ end
+ else
+ do_call(method, *args)
+ end
+ end
+
+ def do_call(method, *args)
begin
@server.call(method, *args)
rescue HTTPBroken => httpbe
if self.class.retry_on_http_error
- @server = init_server(uri)
+ @server = self.class.init_server(@uri)
@server.call(method, *args)
+ else
+ raise httpbe
end
end
end
+ private :do_call
+
end
end

0 comments on commit 407a5bc

Please sign in to comment.