def standalone(): """ Entry point to script """ logging_config.configure_logging("sync_databases") check_existing_uris() remove_nonexisting_uris() IOLoop.instance().start()
def standalone(): """ Initializes Tornado and our application. Forks worker processes to handle requests. Does not return until all child processes exit normally. """ # Parse arguments parser = argparse.ArgumentParser(description="Ellis web server") parser.add_argument("--background", action="store_true", help="Detach and run server in background") args = parser.parse_args() # We don't initialize logging until we fork because we want each child to # have its own logging and it's awkward to reconfigure logging that is # defined by the parent. application = create_application() listening_on_some_port = False http_sockets = None https_sockets = None if settings.ALLOW_HTTP: http_sockets = bind_sockets(settings.HTTP_PORT, address=settings.LOCAL_IP) listening_on_some_port = True if (os.path.exists(settings.TLS_CERTIFICATE) and os.path.exists(settings.TLS_PRIVATE_KEY)): https_sockets = bind_sockets(settings.HTTPS_PORT, address=settings.LOCAL_IP) listening_on_some_port = True if not listening_on_some_port: # We usually don't configure logging until after we fork but since # we're about to crash... logging_config.configure_logging("parent") _log.critical("Failed to listen on any ports.") raise Exception("Failed to listen on any ports") if args.background: # Get a new logfile, rotating the old one if present. err_log_name = os.path.join(settings.LOGS_DIR, settings.LOG_FILE_PREFIX + "-err.log") try: os.rename(err_log_name, err_log_name + ".old") except OSError: pass # Fork into background. utils.daemonize(err_log_name) utils.install_sigusr1_handler(settings.LOG_FILE_PREFIX) # Drop a pidfile. pid = os.getpid() with open(settings.PID_FILE, "w") as pidfile: pidfile.write(str(pid) + "\n") # Fork off a child process per core. In the parent process, the # fork_processes call blocks until the children exit. num_processes = settings.TORNADO_PROCESSES_PER_CORE * tornado.process.cpu_count() task_id = tornado.process.fork_processes(num_processes) if task_id is not None: logging_config.configure_logging(task_id) # We're a child process, start up. _log.info("Process %s starting up", task_id) connection.init_connection() if http_sockets: _log.info("Going to listen for HTTP on port %s", settings.HTTP_PORT) http_server = httpserver.HTTPServer(application) http_server.add_sockets(http_sockets) else: _log.info("Not starting HTTP, set ALLOW_HTTP in local_settings.py to enable HTTP.") if https_sockets: _log.info("Going to listen for HTTPS on port %s", settings.HTTPS_PORT) https_server = httpserver.HTTPServer(application, ssl_options={ "certfile": settings.TLS_CERTIFICATE, "keyfile": settings.TLS_PRIVATE_KEY, }) https_server.add_sockets(https_sockets) else: _log.critical("Not starting HTTPS") homestead.ping() background.start_background_worker_io_loop() io_loop = tornado.ioloop.IOLoop.instance() io_loop.start() else: # This shouldn't happen since the children should run their IOLoops # forever. _log.critical("Children all exited")
if pstn: public_id = "sip:+1%010d@%s" % (start + x, realm) else: public_id = "sip:%010d@%s" % (start + x, realm) try: numbers.add_number_to_pool(s, public_id, pstn) except IntegrityError: # Entry already exists, not creating in db pass else: create_count += 1 s.commit() print "Created %d numbers, %d already present in database" % (create_count, num - create_count) if __name__ == '__main__': logging_config.configure_logging("create_db") parser = OptionParser() parser.add_option("-s", "--start", dest="start", type="int", help="Start creating with this number, defaulting to 5108580271 (PSTN) or 6505550000") parser.add_option("-c", "--count", dest="num", type="int", default=1, help="Create this many numbers, if not specified, only one number will be created") parser.add_option("-p", "--pstn", action="store_true",