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