def wrapper(*args, **kwargs): try: # Phase 1: Calculate refresh and current date to determine # if whether to download a new copy of the MAC # txt file should be downloaded from the IEEE site. # We do not want to repopulate more than once a day # Are we running on Bluemix or not? if VCAP_CONFIG: nextDate = REDIS_INSTANCE.get("nextDate") # Nothing stored for first time this app has been deployed if nextDate is None: nextDate = datetime.utcnow() else: # Need to convert the cached date from str to datetime nextDate = datetime.strptime(nextDate, '%Y-%m-%d %H:%M:%S.%f') else: # Running local. First time? if not wrapper.has_run: nextDate = datetime.utcnow() # We've already populated so let's not populate again else: nextDate = datetime.utcnow() + timedelta(seconds=86400) # Let's record what datetime it is right now ... now = datetime.utcnow() # Phase 2: Test whether the current date is past our 24 hour # refresh date criteria or not. print("Evaluating if Now is >= Criteria ...") if now >= nextDate: print("Refresh criteria has been met") # Set a flag so that future local repopulates fall through wrapper.has_run = True # Test if we are running in Bluemix if VCAP_CONFIG: # Persist a refresh date 24 hours in the future REDIS_INSTANCE.set("nextDate", datetime.utcnow() + timedelta(seconds=86400)) return f(*args, **kwargs) else: # Refresh not required, relay a default response print("Refresh criteria NOT met") data = { 'message': 'Whoaa! MacReduce Data is up to date!' } message = json.dumps(data) print(message) resp = Response(message, status=200, mimetype='application/json') return resp except Exception as e: print(e) print(traceback.format_exc())
def wrapper(*args, **kwargs): try: # Phase 1: Calculate refresh and current date to determine # if whether to download a new copy of the MAC # txt file should be downloaded from the IEEE site. # We do not want to repopulate more than once a day # Are we running on Bluemix or not? if VCAP_CONFIG: nextDate = REDIS_INSTANCE.get("nextDate") # Nothing stored for first time this app has been deployed if nextDate is None: nextDate = datetime.utcnow() else: # Need to convert the cached date from str to datetime nextDate = datetime.strptime(nextDate, '%Y-%m-%d %H:%M:%S.%f') else: # Running local. First time? if not wrapper.has_run: nextDate = datetime.utcnow() # We've already populated so let's not populate again else: nextDate = datetime.utcnow() + timedelta(seconds=86400) # Let's record what datetime it is right now ... now = datetime.utcnow() # Phase 2: Test whether the current date is past our 24 hour # refresh date criteria or not. print("Evaluating if Now is >= Criteria ...") if now >= nextDate: print("Refresh criteria has been met") # Set a flag so that future local repopulates fall through wrapper.has_run = True # Test if we are running in Bluemix if VCAP_CONFIG: # Persist a refresh date 24 hours in the future REDIS_INSTANCE.set( "nextDate", datetime.utcnow() + timedelta(seconds=86400)) return f(*args, **kwargs) else: # Refresh not required, relay a default response print("Refresh criteria NOT met") data = {'message': 'Whoaa! MacReduce Data is up to date!'} message = json.dumps(data) print(message) resp = Response(message, status=200, mimetype='application/json') return resp except Exception as e: print(e) print(traceback.format_exc())
# Monkey Patching app behavior to make it greenlet non-blocking # This is usually required by gevent for native bindings for things # like Redis interactions, etc ... monkey.patch_all() socket.setdefaulttimeout(240) # capture current working directory PWD = os.environ.get("PWD") # set static folder path for static data static_folder = os.path.join(PWD, "macreduce/static") # Detect if we are deployed within Bluemix or not and configure accordingly if VCAP_CONFIG: print('Welcome to Bluemix') print('Running on Python version: ' + python_version()) app = Eve(static_folder=static_folder, redis=REDIS_INSTANCE) REDIS_INSTANCE.flushdb() else: print('We are not running in Bluemix! Dev Mode Enabled') app = Eve(static_folder=static_folder, redis=REDIS_INSTANCE) print(' Enabling Debug ...') app.debug = True # Setup some default home page path rules for JSON and HTML app.add_url_rule('/', 'index', home.index) # app.add_url_rule('/<path:path>', 'nonresource', home.index) # Setup a favicon url for the home page app.add_url_rule('/favicon', 'favicon', view_func=home.favicon, methods=['GET'])
# Monkey Patching app behavior to make it greenlet non-blocking # This is usually required by gevent for native bindings for things # like Redis interactions, etc ... monkey.patch_all() socket.setdefaulttimeout(240) # capture current working directory PWD = os.environ.get("PWD") # set static folder path for static data static_folder = os.path.join(PWD, "macreduce/static") # Detect if we are deployed within Bluemix or not and configure accordingly if VCAP_CONFIG: print("Welcome to Bluemix") print("Running on Python version: " + python_version()) app = Eve(static_folder=static_folder, redis=REDIS_INSTANCE) REDIS_INSTANCE.flushdb() else: print("We are not running in Bluemix! Dev Mode Enabled") app = Eve(static_folder=static_folder, redis=REDIS_INSTANCE) print(" Enabling Debug ...") app.debug = True # Setup some default home page path rules for JSON and HTML app.add_url_rule("/", "index", home.index) # app.add_url_rule('/<path:path>', 'nonresource', home.index) # Setup a favicon url for the home page app.add_url_rule("/favicon", "favicon", view_func=home.favicon, methods=["GET"]) app.add_url_rule("/populate", "populate", view_func=home.populate, methods=["GET"]) # Setup examples of event hooks