OLD | NEW |
| (Empty) |
1 # Copyright (C) 2010 Gabor Rapcsanyi (rgabor@inf.u-szeged.hu), University of Sze
ged | |
2 # Copyright (C) 2010 Andras Becsi (abecsi@inf.u-szeged.hu), University of Szeged | |
3 # | |
4 # All rights reserved. | |
5 # | |
6 # Redistribution and use in source and binary forms, with or without | |
7 # modification, are permitted provided that the following conditions | |
8 # are met: | |
9 # 1. Redistributions of source code must retain the above copyright | |
10 # notice, this list of conditions and the following disclaimer. | |
11 # 2. Redistributions in binary form must reproduce the above copyright | |
12 # notice, this list of conditions and the following disclaimer in the | |
13 # documentation and/or other materials provided with the distribution. | |
14 # | |
15 # THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY | |
16 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
18 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF SZEGED OR | |
19 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
20 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
21 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
22 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
23 # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
25 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
26 | |
27 # FIXME: rename this file, and add more text about how this is | |
28 # different from the base file_lock class. | |
29 | |
30 """This class helps to block NRWT threads when more NRWTs run | |
31 perf, http and websocket tests in a same time.""" | |
32 | |
33 import logging | |
34 import os | |
35 import sys | |
36 import tempfile | |
37 import time | |
38 | |
39 from webkitpy.common.system.executive import Executive | |
40 from webkitpy.common.system.file_lock import FileLock | |
41 from webkitpy.common.system.filesystem import FileSystem | |
42 | |
43 | |
44 _log = logging.getLogger(__name__) | |
45 | |
46 | |
47 class HttpLock(object): | |
48 def __init__(self, lock_path, lock_file_prefix="WebKitHttpd.lock.", guard_lo
ck="WebKit.lock", filesystem=None, executive=None, name='HTTP'): | |
49 self._executive = executive or Executive() | |
50 self._filesystem = filesystem or FileSystem() | |
51 self._lock_path = lock_path | |
52 if not self._lock_path: | |
53 # FIXME: FileSystem should have an accessor for tempdir() | |
54 self._lock_path = tempfile.gettempdir() | |
55 self._lock_file_prefix = lock_file_prefix | |
56 self._lock_file_path_prefix = self._filesystem.join(self._lock_path, sel
f._lock_file_prefix) | |
57 self._guard_lock_file = self._filesystem.join(self._lock_path, guard_loc
k) | |
58 self._guard_lock = FileLock(self._guard_lock_file) | |
59 self._process_lock_file_name = "" | |
60 self._name = name | |
61 | |
62 def cleanup_http_lock(self): | |
63 """Delete the lock file if exists.""" | |
64 if self._filesystem.exists(self._process_lock_file_name): | |
65 _log.debug("Removing lock file: %s" % self._process_lock_file_name) | |
66 self._filesystem.remove(self._process_lock_file_name) | |
67 | |
68 def _extract_lock_number(self, lock_file_name): | |
69 """Return the lock number from lock file.""" | |
70 prefix_length = len(self._lock_file_path_prefix) | |
71 return int(lock_file_name[prefix_length:]) | |
72 | |
73 def _lock_file_list(self): | |
74 """Return the list of lock files sequentially.""" | |
75 lock_list = self._filesystem.glob(self._lock_file_path_prefix + '*') | |
76 lock_list.sort(key=self._extract_lock_number) | |
77 return lock_list | |
78 | |
79 def _next_lock_number(self): | |
80 """Return the next available lock number.""" | |
81 lock_list = self._lock_file_list() | |
82 if not lock_list: | |
83 return 0 | |
84 return self._extract_lock_number(lock_list[-1]) + 1 | |
85 | |
86 def _current_lock_pid(self): | |
87 """Return with the current lock pid. If the lock is not valid | |
88 it deletes the lock file.""" | |
89 lock_list = self._lock_file_list() | |
90 if not lock_list: | |
91 _log.debug("No lock file list") | |
92 return | |
93 try: | |
94 current_pid = self._filesystem.read_text_file(lock_list[0]) | |
95 if not (current_pid and self._executive.check_running_pid(int(curren
t_pid))): | |
96 _log.debug("Removing stuck lock file: %s" % lock_list[0]) | |
97 self._filesystem.remove(lock_list[0]) | |
98 return | |
99 except IOError, e: | |
100 _log.debug("IOError: %s" % e) | |
101 return | |
102 except OSError, e: | |
103 _log.debug("OSError: %s" % e) | |
104 return | |
105 return int(current_pid) | |
106 | |
107 def _create_lock_file(self): | |
108 """The lock files are used to schedule the running test sessions in firs
t | |
109 come first served order. The guard lock ensures that the lock numbers ar
e | |
110 sequential.""" | |
111 if not self._filesystem.exists(self._lock_path): | |
112 _log.debug("Lock directory does not exist: %s" % self._lock_path) | |
113 return False | |
114 | |
115 if not self._guard_lock.acquire_lock(): | |
116 _log.debug("Guard lock timed out!") | |
117 return False | |
118 | |
119 self._process_lock_file_name = (self._lock_file_path_prefix + str(self._
next_lock_number())) | |
120 _log.debug("Creating lock file: %s" % self._process_lock_file_name) | |
121 # FIXME: Executive.py should have an accessor for getpid() | |
122 self._filesystem.write_text_file(self._process_lock_file_name, str(os.ge
tpid())) | |
123 self._guard_lock.release_lock() | |
124 return True | |
125 | |
126 def wait_for_httpd_lock(self): | |
127 """Create a lock file and wait until it's turn comes. If something goes
wrong | |
128 it wont do any locking.""" | |
129 if not self._create_lock_file(): | |
130 _log.debug("Warning, %s locking failed!" % self._name) | |
131 return | |
132 | |
133 # FIXME: This can hang forever! | |
134 while self._current_lock_pid() != os.getpid(): | |
135 time.sleep(1) | |
136 | |
137 _log.debug("%s lock acquired" % self._name) | |
OLD | NEW |