]> git.phdru.name Git - bookmarks_db.git/blob - Robots/bkmk_rforking.py
30133dcb4011fcb908ddb6f011732adf5217fc40
[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-2023 PhiloSoft Design"
8 __license__ = "GNU GPL"
9
10 __all__ = ['robot_forking']
11
12
13 import os
14 import sys
15
16 try:
17     import cPickle as pickle
18 except ImportError:
19     import pickle
20
21 from subproc import Subprocess, RecordFile
22 from bkmk_objects import Robot
23
24
25 # This is to catch 'close failed: [Errno 9] Bad file descriptor' message
26 # from os.close() in Subprocess.die() and errors from the subprocess.
27 sys.stderr = open("err.log", 'a')
28
29 check_subp = None
30 subp_pipe = None
31
32
33 def stop_subp(log):
34     global check_subp, subp_pipe
35     if check_subp:
36         if log: log("   restarting hanging subprocess")
37         del check_subp
38     del subp_pipe
39
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 _set_subproc = True
51
52
53 class robot_forking(Robot):
54     subproc = 'urllib2'  # Default subprocess
55
56     def check_url(self, bookmark):
57         global _set_subproc
58         if _set_subproc:
59             _set_subproc = False
60
61             subproc = self.subproc
62             subproc_attrs = []
63             for attr in dir(self):
64                 if attr.startswith('subproc_'):
65                     subproc_attrs.append((attr[len('subproc_'):], getattr(self, attr)))
66             if subproc_attrs:
67                 subproc += ':' + ':'.join(
68                     ['='.join((k, v)) for k, v in subproc_attrs]
69                 )
70             os.environ['BKMK_ROBOT'] = subproc
71
72         if not check_subp:
73             restart_subp(self.log)  # Not restart, just start afresh
74
75         try:
76             save_parent = bookmark.parent
77             bookmark.parent = None
78             subp_pipe.write_record(pickle.dumps(bookmark))
79
80             if check_subp.waitForPendingChar(60):  # wait a minute
81                 new_b = pickle.loads(subp_pipe.read_record())
82                 for attr in (
83                     "error", "no_error",
84                     "moved", "size", "md5", "real_title",
85                     "last_tested", "last_modified", "test_time",
86                     "icon", "icon_href",
87                 ):
88                     if hasattr(new_b, attr):
89                         setattr(bookmark, attr, getattr(new_b, attr))
90             else:
91                 bookmark.error = "Subprocess connection timed out"
92                 restart_subp(self.log)
93
94             bookmark.parent = save_parent
95
96             while True:
97                 error = check_subp.readPendingErrLine()
98                 if not error:
99                     break
100                 sys.stderr.write("(subp) " + error)
101             sys.stderr.flush()
102
103         except KeyboardInterrupt:
104             return 0
105
106         # Tested
107         return 1
108
109     def stop(self):
110         stop_subp(None)  # Stop subprocess; do not log restarting