]> git.phdru.name Git - bookmarks_db.git/blob - Robots/bkmk_rforking.py
Change parse_html to parse strings, not files
[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 __author__ = "Oleg Broytman <phd@phdru.name>"
7 __copyright__ = "Copyright (C) 2000-2012 PhiloSoft Design"
8 __license__ = "GNU GPL"
9
10 __all__ = ['robot_forking']
11
12
13 import sys, os
14
15 try:
16    import cPickle as pickle
17 except ImportError:
18    import pickle
19
20 from subproc import Subprocess, RecordFile
21 from bkmk_objects import Robot
22
23
24 # This is to catch 'close failed: [Errno 9] Bad file descriptor' message
25 # from os.close() in Subprocess.die() and errors from the subprocess.
26 sys.stderr = open("err.log", 'a')
27
28 check_subp = None
29 subp_pipe = None
30
31 def stop_subp(log):
32    global check_subp, subp_pipe
33    if check_subp:
34       if log: log("   restarting hanging subprocess")
35       del check_subp
36    del subp_pipe
37
38 def restart_subp(log):
39    global check_subp, subp_pipe
40    stop_subp(log)
41
42    check_subp = Subprocess("%s/Robots/bkmk_rforking_sub.py" % os.path.dirname(sys.argv[0]),
43       control_stderr=True)
44    subp_pipe = RecordFile(check_subp)
45
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    def stop(self):
85       stop_subp(None) # Stop subprocess; do not log restarting