def get(self):
          
     self.__getConnectedUsers()
     self.__setupSecureChannel()
     self.__setupClientChannel()
     
     # set shutdown hook
     runtime.set_shutdown_hook(self.isShuttingDown)
Ejemplo n.º 2
0
  def get(self):
    runtime.set_shutdown_hook(shutdown_hook)

    global streams, streams_lock, update_thread
    with streams_lock:
      streams = {}
      if update_thread is None:
        update_thread = background_thread.start_new_background_thread(
          update_streams, [])
 def get(self):
     """A handler for /_ah/start, registering myself."""
     runtime.set_shutdown_hook(shutdown_hook)
     con = get_connection()
     with con:
         con.execute(CREATE_TABLE_SQL)
     instance_id = modules.get_current_instance_id()
     server = ActiveServer(key=ActiveServer.get_instance_key(instance_id))
     server.put()
Ejemplo n.º 4
0
	def process(self):
		logging.info('Backend running')
		
		server_name = backends.get_backend()
		logging.info(server_name)

		servers = memcache.get('servers')
		if servers is None:
			servers = dict()
			servers[server_name] = 1
		else:
			if server_name not in servers:
				servers[server_name] = 1
			else:
				servers[server_name] = servers[server_name] + 1

		memcache.set('servers', servers)

		logservice.flush()

		runtime.set_shutdown_hook(self.shutdown)
		self.response.set_status(200)
		return
 def get(self):
     runtime.set_shutdown_hook(_counter_store.shutdown_hook)
Ejemplo n.º 6
0
 def get(self):  # pylint: disable=g-bad-name
   """Handler for /_ah/start get requests."""
   runtime.set_shutdown_hook(MessageRecallShutdownHook)
   self.response.status = 200
Ejemplo n.º 7
0
 def get(self):
     logging.info("Instance start request")
     from google.appengine.api import runtime
     runtime.set_shutdown_hook(shutdown)
Ejemplo n.º 8
0
        while not runtime.is_shutting_down():
            try:
                tasks = taskq.lease_tasks(3600, 100)
            except Exception as e:
                logging.error(e)
                time.sleep(60)
                return
            logging.info('pull %d task',len(tasks))
            if len(tasks)==0:
                return
            tasktodelete=[]
            for task in tasks:
                logging.info(task.payload)
                tasktodelete.append(task)
            taskq.delete_tasks(tasks)


def my_shutdown_hook():
  apiproxy_stub_map.apiproxy.CancelApiCalls()
  #save_state()
runtime.set_shutdown_hook(my_shutdown_hook)

application = webapp.WSGIApplication([
('/_ah/start',FetchBackend)],
debug=True)

def main():
    run_wsgi_app(application)
if __name__ == '__main__':
    main()
Ejemplo n.º 9
0
def initialize(app=None,
               is_enabled_fn=None,
               cron_module='default',
               is_local_unittest=None):
    """Instruments webapp2 `app` with gae_ts_mon metrics.

  Instruments all the endpoints in `app` with basic metrics.

  Args:
    app (webapp2 app): the app to instrument.
    is_enabled_fn (function or None): a function returning bool if ts_mon should
      send the actual metrics. None (default) is equivalent to lambda: True.
      This allows apps to turn monitoring on or off dynamically, per app.
    cron_module (str): the name of the module handling the
      /internal/cron/ts_mon/send endpoint. This allows moving the cron job
      to any module the user wants.
    is_local_unittest (bool or None): whether we are running in a unittest.
  """
    if is_local_unittest is None:  # pragma: no cover
        # Since gae_ts_mon.initialize is called at module-scope by appengine apps,
        # AppengineTestCase.setUp() won't have run yet and none of the appengine
        # stubs will be initialized, so accessing Datastore or even getting the
        # application ID will fail.
        is_local_unittest = ('expect_tests' in sys.argv[0])

    if is_enabled_fn is not None:
        interface.state.flush_enabled_fn = is_enabled_fn

    if app is not None:
        instrument_wsgi_application(app)
        if is_local_unittest or modules.get_current_module_name(
        ) == cron_module:
            instrument_wsgi_application(handlers.app)

    # Use the application ID as the service name and the module name as the job
    # name.
    if is_local_unittest:  # pragma: no cover
        service_name = 'unittest'
        job_name = 'unittest'
        hostname = 'unittest'
    else:
        service_name = app_identity.get_application_id()
        job_name = modules.get_current_module_name()
        hostname = modules.get_current_version_name()
        runtime.set_shutdown_hook(_shutdown_hook)

    interface.state.target = targets.TaskTarget(service_name,
                                                job_name,
                                                shared.REGION,
                                                hostname,
                                                task_num=-1)
    interface.state.flush_mode = 'manual'
    interface.state.last_flushed = datetime.datetime.utcnow()

    # Don't send metrics when running on the dev appserver.
    if (is_local_unittest or os.environ.get('SERVER_SOFTWARE',
                                            '').startswith('Development')):
        logging.info('Using debug monitor')
        interface.state.global_monitor = monitors.DebugMonitor()
    else:
        logging.info('Using https monitor %s with %s',
                     shared.PRODXMON_ENDPOINT,
                     shared.PRODXMON_SERVICE_ACCOUNT_EMAIL)
        interface.state.global_monitor = monitors.HttpsMonitor(
            shared.PRODXMON_ENDPOINT,
            monitors.DelegateServiceAccountCredentials(
                shared.PRODXMON_SERVICE_ACCOUNT_EMAIL,
                monitors.AppengineCredentials()))
        interface.state.use_new_proto = True

    interface.register_global_metrics([shared.appengine_default_version])
    interface.register_global_metrics_callback(shared.INTERNAL_CALLBACK_NAME,
                                               _internal_callback)

    # We invoke global callbacks once for the whole application in the cron
    # handler.  Leaving this set to True would invoke them once per task.
    interface.state.invoke_global_callbacks_on_flush = False

    standard_metrics.init()

    logging.info(
        'Initialized ts_mon with service_name=%s, job_name=%s, '
        'hostname=%s', service_name, job_name, hostname)
Ejemplo n.º 10
0
def initialize(app=None, is_enabled_fn=None, cron_module='default',
               is_local_unittest=None):
  """Instruments webapp2 `app` with gae_ts_mon metrics.

  Instruments all the endpoints in `app` with basic metrics.

  Args:
    app (webapp2 app): the app to instrument.
    is_enabled_fn (function or None): a function returning bool if ts_mon should
      send the actual metrics. None (default) is equivalent to lambda: True.
      This allows apps to turn monitoring on or off dynamically, per app.
    cron_module (str): the name of the module handling the
      /internal/cron/ts_mon/send endpoint. This allows moving the cron job
      to any module the user wants.
    is_local_unittest (bool or None): whether we are running in a unittest.
  """
  if is_local_unittest is None:  # pragma: no cover
    # Since gae_ts_mon.initialize is called at module-scope by appengine apps,
    # AppengineTestCase.setUp() won't have run yet and none of the appengine
    # stubs will be initialized, so accessing Datastore or even getting the
    # application ID will fail.
    is_local_unittest = ('expect_tests' in sys.argv[0])

  if is_enabled_fn is not None:
    interface.state.flush_enabled_fn = is_enabled_fn

  if app is not None:
    instrument_wsgi_application(app)
    if is_local_unittest or modules.get_current_module_name() == cron_module:
      instrument_wsgi_application(handlers.app)

  # Use the application ID as the service name and the module name as the job
  # name.
  if is_local_unittest:  # pragma: no cover
    service_name = 'unittest'
    job_name = 'unittest'
    hostname = 'unittest'
  else:
    service_name = app_identity.get_application_id()
    job_name = modules.get_current_module_name()
    hostname = modules.get_current_version_name()
    runtime.set_shutdown_hook(_shutdown_hook)

  interface.state.target = targets.TaskTarget(
      service_name, job_name, shared.REGION, hostname, task_num=-1)
  interface.state.flush_mode = 'manual'
  interface.state.last_flushed = datetime.datetime.utcnow()

  # Don't send metrics when running on the dev appserver.
  if (is_local_unittest or
      os.environ.get('SERVER_SOFTWARE', '').startswith('Development')):
    logging.info('Using debug monitor')
    interface.state.global_monitor = monitors.DebugMonitor()
  else:
    logging.info('Using pubsub monitor %s/%s', shared.PUBSUB_PROJECT,
                 shared.PUBSUB_TOPIC)
    interface.state.global_monitor = monitors.PubSubMonitor(
        monitors.APPENGINE_CREDENTIALS, shared.PUBSUB_PROJECT,
        shared.PUBSUB_TOPIC)

  shared.register_global_metrics([shared.appengine_default_version])
  shared.register_global_metrics_callback(
      shared.INTERNAL_CALLBACK_NAME, _internal_callback)

  logging.info('Initialized ts_mon with service_name=%s, job_name=%s, '
               'hostname=%s', service_name, job_name, hostname)
from google.appengine.api import apiproxy_stub_map
from google.appengine.api import runtime
from utilities.logger import logThis, AEL_LEVEL_WARNING, AEL_LEVEL_INFO

def fashionista_register_shutdown_hook():
    apiproxy_stub_map.apiproxy.CancelApiCalls()
  
    # We can save state to datastore here or Log some statistics.
    logThis(AEL_LEVEL_WARNING, 'SHUTDOWN IN PROGRESS...')
    logThis(AEL_LEVEL_INFO,'CPU USAGE: %s' % runtime.cpu_usage())
    logThis(AEL_LEVEL_INFO,'MEMORY USAGE: %s' % runtime.memory_usage())
#Register a shutdown hook. Save state before being terminated.    
runtime.set_shutdown_hook(fashionista_register_shutdown_hook)
Ejemplo n.º 12
0
from google.appengine.api import apiproxy_stub_map
from google.appengine.api import runtime
from utilities.logger import logThis, AEL_LEVEL_WARNING, AEL_LEVEL_INFO


def fashionista_register_shutdown_hook():
    apiproxy_stub_map.apiproxy.CancelApiCalls()

    # We can save state to datastore here or Log some statistics.
    logThis(AEL_LEVEL_WARNING, 'SHUTDOWN IN PROGRESS...')
    logThis(AEL_LEVEL_INFO, 'CPU USAGE: %s' % runtime.cpu_usage())
    logThis(AEL_LEVEL_INFO, 'MEMORY USAGE: %s' % runtime.memory_usage())


#Register a shutdown hook. Save state before being terminated.
runtime.set_shutdown_hook(fashionista_register_shutdown_hook)
Ejemplo n.º 13
0
    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.'''

from google.appengine.api import apiproxy_stub_map
from google.appengine.api import backends
from google.appengine.api import runtime

import datetime
import logging

_NAME = '{}.{} {} ({})'.format(backends.get_backend(), backends.get_instance(),
                               backends.get_url(), datetime.datetime.now())


def my_shutdown_hook():
    logging.warning('{} SHUTDOWN HOOK CALLED'.format(_NAME))
    apiproxy_stub_map.apiproxy.CancelApiCalls()
    # save_state()
    # May want to raise an exception


# register our shutdown hook, which is not guaranteed to be called
logging.info('{} REGISTERING SHUTDOWN HOOK'.format(_NAME))
runtime.set_shutdown_hook(my_shutdown_hook)
Ejemplo n.º 14
0
 def get(self):
     runtime.set_shutdown_hook(shutdown)
     thread = background_thread.BackgroundThread(
         target=counter_loop)
     thread.start()
 def get(self):
     runtime.set_shutdown_hook(_counter_store.shutdown_hook)
Ejemplo n.º 16
0
	from lib.utils import DEBUG
	if DEBUG:
		return

	rpcs = []
	for index in range(0, len(events), MAX_BATCH_SIZE):
		batch = events[index:index+MAX_BATCH_SIZE]
		data  = 'data=' + b64encode(json_stringify(batch))
		rpc   = urlfetch.create_rpc()
		try:
			urlfetch.make_fetch_call(
				rpc,
				API_URL,
				method  = 'POST',
				headers = { 'Content-Type' : 'application/x-www-form-urlencoded' },
				payload = data,
			)
			rpcs.append(rpc)
		except Exception as e:
			logging.error('failed to trigger urlfetch')
			logging.exception(e)

	for rpc in rpcs:
		try:
			rpc.wait()
		except Exception as e:
			logging.error('failed to resolve urlfetch')
			logging.exception(e)

runtime.set_shutdown_hook(flush)