]> git.phdru.name Git - bookmarks_db.git/blob - Robots/bkmk_rforking.py
Added __all__.
[bookmarks_db.git] / Robots / bkmk_rforking.py
1 """Forking robot
2
3 This file is a part of Bookmarks database and Internet robot.
4 """
5
6 __version__ = "$Revision$"[11:-2]
7 __revision__ = "$Id$"[5:-2]
8 __date__ = "$Date$"[7:-2]
9 __author__ = "Oleg Broytman <phd@phdru.name>"
10 __copyright__ = "Copyright (C) 2000-2011 PhiloSoft Design"
11 __license__ = "GNU GPL"
12
13 __all__ = ['robot_forking']
14
15
16 import sys, os
17
18 try:
19    import cPickle as pickle
20 except ImportError:
21    import pickle
22
23 from subproc import Subprocess, RecordFile
24 from bkmk_objects import Robot
25
26
27 # This is to catch 'close failed: [Errno 9] Bad file descriptor' message
28 # from os.close() in Subprocess.die() and errors from from the subprocess.
29 sys.stderr = open("err.log", 'a')
30
31 check_subp = None
32 subp_pipe = None
33
34 def stop_subp(log):
35    global check_subp, subp_pipe
36    if check_subp:
37       if log: log("   restarting hanging subprocess")
38       del check_subp
39    del subp_pipe
40
41 def restart_subp(log):
42    global check_subp, subp_pipe
43    stop_subp(log)
44
45    check_subp = Subprocess("%s/Robots/bkmk_rforking_sub.py" % os.path.dirname(sys.argv[0]),
46       control_stderr=True)
47    subp_pipe = RecordFile(check_subp)
48
49
50 class robot_forking(Robot):
51    def check_url(self, bookmark):
52       if not check_subp:
53          restart_subp(self.log) # Not restart, just start afresh
54
55       try:
56          save_parent = bookmark.parent
57          bookmark.parent = None
58          subp_pipe.write_record(pickle.dumps(bookmark))
59
60          if check_subp.waitForPendingChar(60): # wait a minute
61             new_b = pickle.loads(subp_pipe.read_record())
62             for attr in ("error", "no_error",
63                   "moved", "size", "md5", "real_title",
64                   "last_tested", "last_modified", "test_time",
65                   "icon", "icon_href"):
66                if hasattr(new_b, attr):
67                   setattr(bookmark, attr, getattr(new_b, attr))
68          else:
69             bookmark.error = "Subprocess connection timed out"
70             restart_subp(self.log)
71
72          bookmark.parent = save_parent
73
74          while True:
75             error = check_subp.readPendingErrLine()
76             if not error:
77                break
78             sys.stderr.write("(subp) " + error)
79          sys.stderr.flush()
80
81       except KeyboardInterrupt:
82          return 0
83
84       # Tested
85       return 1
86
87    def stop(self):
88       stop_subp(None) # Stop subprocess; do not log restarting