From 81891ea4d7681d49246f079cb1055ea3c1d55b1e Mon Sep 17 00:00:00 2001 From: Keith Mitchell Date: Thu, 25 Oct 2012 13:08:36 -0700 Subject: [PATCH] ads: Add endpoint for health check on ads Adds a new route /health/ads which returns in plain text the seconds since the epoch that the ad system was last updated via make_daily_promotions() --- r2/r2/config/routing.py | 1 + r2/r2/controllers/health.py | 5 +++++ r2/r2/lib/promote.py | 13 +++++++++++++ 3 files changed, 19 insertions(+) diff --git a/r2/r2/config/routing.py b/r2/r2/config/routing.py index 9bc63a59c..3764661db 100644 --- a/r2/r2/config/routing.py +++ b/r2/r2/config/routing.py @@ -158,6 +158,7 @@ def make_map(): mc('/promoted/', controller='promoted', action="listing", sort="") mc('/health', controller='health', action='health') + mc('/health/ads', controller='health', action='promohealth') mc('/', controller='hot', action='listing') diff --git a/r2/r2/controllers/health.py b/r2/r2/controllers/health.py index 0d56a23a5..a8ebb9fe5 100644 --- a/r2/r2/controllers/health.py +++ b/r2/r2/controllers/health.py @@ -30,6 +30,7 @@ from pylons import c, g, response from reddit_base import MinimalController from r2.lib.amqp import worker +from r2.lib import promote from validator import * @@ -47,3 +48,7 @@ class HealthController(MinimalController): c.dontcache = True response.headers['Content-Type'] = 'text/plain' return json.dumps(g.versions, sort_keys=True, indent=4) + + def GET_promohealth(self): + response.headers['Content-Type'] = 'text/plain' + return json.dumps(promote.health_check()) diff --git a/r2/r2/lib/promote.py b/r2/r2/lib/promote.py index 92d9a9280..d88eda02b 100644 --- a/r2/r2/lib/promote.py +++ b/r2/r2/lib/promote.py @@ -23,9 +23,11 @@ from __future__ import with_statement import json +import time from r2.models import * from r2.models.bidding import SponsorBoxWeightings, WeightingRef +from r2.models.keyvalue import NamedGlobals from r2.lib.wrapped import Wrapped from r2.lib import authorize from r2.lib import emailer, filters @@ -54,6 +56,16 @@ CAMPAIGN = Enum("start", "end", "bid", "sr", "trans_id") UPDATE_QUEUE = 'update_promos_q' QUEUE_ALL = 'all' +PROMO_HEALTH_KEY = 'promotions_last_updated' + + +def _mark_promos_updated(): + NamedGlobals.set(PROMO_HEALTH_KEY, time.time()) + + +def health_check(): + return NamedGlobals.get(PROMO_HEALTH_KEY, default=0) + @memoize("get_promote_srid") def get_promote_srid(name = 'promos'): @@ -831,6 +843,7 @@ def make_daily_promotions(offset = 0, test = False): if not test: set_live_promotions(all_links, weighted) + _mark_promos_updated() else: print (all_links, weighted)