]> git.phdru.name Git - bookmarks_db.git/blob - Robots/bkmk_rforking.py
Redirect stderr to catch 'close failed: [Errno 9] Bad file descriptor' message
[bookmarks_db.git] / Robots / bkmk_rforking.py
1 """
2    Forking robot
3
4    Written by BroytMann. Copyright (C) 2000-2007 PhiloSoft Design.
5 """
6
7
8 try:
9    import cPickle
10    pickle = cPickle
11 except ImportError:
12    import pickle
13
14 import sys, os
15 from subproc import Subprocess, RecordFile
16
17 # This is to catch 'close failed: [Errno 9] Bad file descriptor'
18 # message from os.close() in Subprocess.die().
19 sys.stderr = open("err.log", 'w')
20
21 check_subp = None
22 subp_pipe = None
23
24 def stop_subp(log):
25    global check_subp, subp_pipe
26    if check_subp:
27       if log: log("   restarting hanging subprocess")
28       del check_subp
29    del subp_pipe
30
31 def restart_subp(log):
32    global check_subp, subp_pipe
33    stop_subp(log)
34
35    check_subp = Subprocess("%s/Robots/bkmk_rforking_sub.py" % os.path.dirname(sys.argv[0]))
36    subp_pipe = RecordFile(check_subp)
37
38
39 from bkmk_objects import Robot
40
41 class robot_forking(Robot):
42    def check_url(self, bookmark):
43       if not check_subp:
44          restart_subp(self.log) # Not restart, just start afresh
45
46       try:
47          save_parent = bookmark.parent
48          bookmark.parent = None
49
50          bookmark.tempfname = self.tempfname
51          subp_pipe.write_record(pickle.dumps(bookmark))
52
53          if check_subp.waitForPendingChar(900): # wait 15 minutes
54             new_b = pickle.loads(subp_pipe.read_record())
55             for attr in ("error", "no_error",
56                   "moved", "size", "md5", "real_title",
57                   "last_tested", "last_modified", "test_time", "icon"):
58                if hasattr(new_b, attr):
59                   setattr(bookmark, attr, getattr(new_b, attr))
60          else:
61             bookmark.error = "Subprocess connection timed out"
62             restart_subp(self.log)
63
64          bookmark.parent = save_parent
65
66       except KeyboardInterrupt:
67          return 0
68
69       # Tested
70       return 1
71
72
73    def stop(self):
74       stop_subp(None) # Stop subprocess; do not log restarting