def main(args): # Parse and validate command line options. parser = setup_options() (options, args) = parser.parse_args(args) if len(args) < 1: usage() # ... I wish there was a better way. x = [x for x in [options.sockfile, options.port, options.systemd] if x] if len(x) > 1: die("can only use one of -s, -p, or --systemd-socket") elif len(x) == 0: die("must supply one of -s, -p, or --systemd-socket") elif options.perms and not options.sockfile: die("-P requires -s") if options.systemd: lsocks = sockact.sd_listen_sockets() if not lsocks: die("--systemd-socket given but no socket(s) found") elif len(lsocks) > 1: die("sadly I cannot listen on multiple sockets yet, passed %d sockets" % len(lsocks)) saddr = lsocks[0] elif options.sockfile: saddr = options.sockfile # Apparently required by Unix? Sigh. try: st = os.stat(options.sockfile) if stat.S_ISSOCK(st.st_mode): os.unlink(options.sockfile) except EnvironmentError: pass elif options.port: saddr = (options.addr, options.port) if options.perms: try: options.perms = int(options.perms, 0) except ValueError: die("cannot convert permissions '%s' to integer" % options.perms) s = wsgi.gensock.gen_sock(saddr, options) try: process(s, options, args) except KeyboardInterrupt: pass
def main(args): # Parse and validate command line options. parser = setup_options() (options, args) = parser.parse_args(args) if len(args) != 1: usage() if options.lockfile: try: fd = os.open(options.lockfile, os.O_RDONLY) except EnvironmentError as e: die("Could not open lockfile: " + str(e)) try: fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) except EnvironmentError: # All failures are assumed to be 'cannot lock, # someone else is already there', and we die. sys.exit(0) # Note that is important that we never close 'fd'; # locks die on close. # ... I wish there was a better way. x = [x for x in [options.sockfile, options.port, options.systemd] if x] if len(x) > 1: die("can only use one of -s, -p, or --systemd-socket") elif len(x) == 0: die("must supply one of -s, -p, or --systemd-socket") elif options.perms and not options.sockfile: die("-P requires -s") if options.systemd: lsocks = sockact.sd_listen_sockets() if not lsocks: die("--systemd-socket given but no socket(s) found") elif len(lsocks) > 1: die("sadly I cannot listen on multiple sockets yet, passed %d sockets" % len(lsocks)) saddr = lsocks[0] elif options.sockfile: saddr = options.sockfile # Apparently required by Unix? Sigh. try: st = os.stat(options.sockfile) if stat.S_ISSOCK(st.st_mode): os.unlink(options.sockfile) except EnvironmentError: pass elif options.port: saddr = (options.addr, options.port) if options.perms: try: options.perms = int(options.perms, 0) except ValueError: die("cannot convert permissions '%s' to integer" % options.perms) if options.idletimeout: options.idletimeout *= 60 if options.workertimeout: options.workertimeout *= 60 # Load up the configuration from the single argument, and then # create the dependant services and stuff. procfunc, _ = dwconfig.materialize(args[0], options, "scgi-%s" % options.servtype) # If we have been asked to do timings, overwrite the WSGI process # function with the null WSGI app. if options.nullapp: procfunc = NullApp # Create the function that will tell the server when it should # stop. s_time = time.time() def stopNow(): try: if options.stopfile and s_time < os.path.getmtime(options.stopfile): return True except EnvironmentError: pass if options.minlifetime: r_time = time.time() - s_time if (options.minlifetime * 60) > r_time: return False if options.minloadavg: cla = get_load() if cla and options.minloadavg > max(cla): return True return False # To make our shutdown more orderly, remove the socket file # when we are shutting down so that new connections can't be # made. def stopNow2(): r = stopNow() if r and options.sockfile: try: os.unlink(options.sockfile) except EnvironmentError: pass return r sfunc = None if options.stopfile or options.minloadavg: sfunc = stopNow2 # Generate the SCGI server, and then serve it out. if options.servtype == "prefork": builder = wsgi.scgipf.gen_server else: builder = wsgi.scgiserv.gen_server scgi = builder(saddr, procfunc, sfunc, options) try: if options.verbose: sys.stderr.write("[%s] [note] dwiki-scgi starting\n" % timestamp()) scgi.serve_forever() except KeyboardInterrupt: pass if options.verbose: sys.stderr.write("[%s] [note] dwiki-scgi stopping\n" % timestamp())
def main(args): # Parse and validate command line options. parser = setup_options() (options, args) = parser.parse_args(args) if len(args) != 1: usage() if options.lockfile: try: fd = os.open(options.lockfile, os.O_RDONLY) except EnvironmentError as e: die("Could not open lockfile: " + str(e)) try: fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) except EnvironmentError: # All failures are assumed to be 'cannot lock, # someone else is already there', and we die. sys.exit(0) # Note that is important that we never close 'fd'; # locks die on close. # ... I wish there was a better way. x = [x for x in [options.sockfile, options.port, options.systemd] if x] if len(x) > 1: die("can only use one of -s, -p, or --systemd-socket") elif len(x) == 0: die("must supply one of -s, -p, or --systemd-socket") elif options.perms and not options.sockfile: die("-P requires -s") if options.systemd: lsocks = sockact.sd_listen_sockets() if not lsocks: die("--systemd-socket given but no socket(s) found") elif len(lsocks) > 1: die("sadly I cannot listen on multiple sockets yet, passed %d sockets" % len(lsocks)) saddr = lsocks[0] elif options.sockfile: saddr = options.sockfile # Apparently required by Unix? Sigh. try: st = os.stat(options.sockfile) if stat.S_ISSOCK(st.st_mode): os.unlink(options.sockfile) except EnvironmentError: pass elif options.port: saddr = (options.addr, options.port) if options.perms: try: options.perms = int(options.perms, 0) except ValueError: die("cannot convert permissions '%s' to integer" % options.perms) if options.idletimeout: options.idletimeout *= 60 if options.workertimeout: options.workertimeout *= 60 # Load up the configuration from the single argument, and then # create the dependant services and stuff. procfunc, _ = dwconfig.materialize(args[0], options, "scgi-%s" % options.servtype) # If we have been asked to do timings, overwrite the WSGI process # function with the null WSGI app. if options.nullapp: procfunc = NullApp # Create the function that will tell the server when it should # stop. s_time = time.time() def stopNow(): try: if options.stopfile and \ s_time < os.path.getmtime(options.stopfile): return True except EnvironmentError: pass if options.minlifetime: r_time = time.time() - s_time if (options.minlifetime * 60) > r_time: return False if options.minloadavg: cla = get_load() if cla and options.minloadavg > max(cla): return True return False # To make our shutdown more orderly, remove the socket file # when we are shutting down so that new connections can't be # made. def stopNow2(): r = stopNow() if r and options.sockfile: try: os.unlink(options.sockfile) except EnvironmentError: pass return r sfunc = None if options.stopfile or options.minloadavg: sfunc = stopNow2 # Generate the SCGI server, and then serve it out. if options.servtype == "prefork": builder = wsgi.scgipf.gen_server else: builder = wsgi.scgiserv.gen_server scgi = builder(saddr, procfunc, sfunc, options) try: if options.verbose: sys.stderr.write("[%s] [note] dwiki-scgi starting\n" % timestamp()) scgi.serve_forever() except KeyboardInterrupt: pass if options.verbose: sys.stderr.write("[%s] [note] dwiki-scgi stopping\n" % timestamp())