root/projects/AsynCluster/trunk/asyncluster/ndm/worker.py

Revision 207, 3.1 kB (checked in by edsuom, 7 months ago)

Got Rao-Blackwellized version working locally & remotely, with better but still disappointing degeneracy

Line 
1 # AsynCluster: Node Display Manager (NDM)
2 # A simple X display manager for cluster nodes that also serve as
3 # access-restricted workstations.
4 #
5 # An NDM client runs on each node and communicates via Twisted's Perspective
6 # Broker to the Aysncluster server, which regulates when and how much each user
7 # can use his account on any of the workstations. The NDM server also
8 # dispatches cluster operations to the nodes via the NDM clients, unbeknownst
9 # to the workstation users.
10 #
11 # Copyright (C) 2006-2008 by Edwin A. Suominen, http://www.eepatents.com
12 #
13 # This program is free software; you can redistribute it and/or modify it under
14 # the terms of the GNU General Public License as published by the Free Software
15 # Foundation; either version 2 of the License, or (at your option) any later
16 # version.
17 #
18 # This program is distributed in the hope that it will be useful, but WITHOUT
19 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 # FOR A PARTICULAR PURPOSE.  See the file COPYING for more details.
21 #
22 # You should have received a copy of the GNU General Public License along with
23 # this program; if not, write to the Free Software Foundation, Inc., 51
24 # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 #
26 # -----------------------------------------------------------------------------
27 #
28 # Daemonization code adapted from
29 #     http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012
30
31 """
32 The main module for worker clients, running as daemons.
33 """
34
35 import os, sys
36
37
38 CONFIG_PATH = "/etc/asyncluster.conf"
39
40
41 class Manager(object):
42     """
43     I manage a child worker client.
44
45     @ivar config: A L{configobj} config object loaded from the config file.
46     
47     """
48     def __init__(self):
49         # Imports
50         from twisted.internet import reactor
51         import configobj, client
52         # The config object
53         self.config = configobj.ConfigObj(CONFIG_PATH)
54         # The session-less client
55         self.client = client.Client(self)
56         # Go!
57         reactor.callWhenRunning(self.client.connect)
58         reactor.run()
59
60
61 def run():
62     """
63     Runs a child worker L{Manager} in a process forked into the background.
64     """
65     # First fork
66     try:
67         pid = os.fork()
68         if pid > 0:
69             sys.exit(0)
70     except OSError, e:
71         sys.exit(1)
72     # Decouple from parent environment
73     os.chdir(".")
74     os.umask(0)
75     os.setsid()
76     # Second fork
77     try:
78         pid = os.fork()
79         if pid > 0:
80             sys.exit(0)
81     except OSError, e:
82         sys.exit(1)
83     # Remap standard file descriptors to /dev/null
84     si = file("/dev/null", 'r')
85     os.dup2(si.fileno(), sys.stdin.fileno())
86     so = file("/dev/null", 'a+')
87     os.dup2(so.fileno(), sys.stdout.fileno())
88     se = file("/dev/null", 'a+', 0)
89     os.dup2(se.fileno(), sys.stderr.fileno())
90     # Finally, run the child worker
91     Manager()
92
93
94 def runForeground(debug=False):
95     """
96     Runs a child worker L{Manager} in the foreground. Useful for debugging,
97     because you can see stdout & stderr.
98     """
99     if debug:
100         import pdb
101         try:
102             Manager()
103         except:
104             pdb.pm()
105     else:
106         Manager()
Note: See TracBrowser for help on using the browser.