-
Notifications
You must be signed in to change notification settings - Fork 6
/
run.py
executable file
·84 lines (67 loc) · 2.46 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/root/screenshots/env/bin/python
import subprocess
import time
class PyshotX(object):
webserver = None
directory = 'screens/'
levels = False
childs = 1
childrens = {}
childrenStarted = {}
def setChildrenProcesses(self, number):
self.childs = number
def setDirectory(self, directory):
self.directory = directory
def getDirectory(self):
return self.directory
def getChildrenProcesses(self):
return self.childs
def setUseLevels(self, use=False):
self.levels = use
def getUseLevels(self):
return self.levels
def startWebServer(self):
args = ['./webserver.py']
if self.getUseLevels():
args.append('levels')
webserverLog = open('webserver.log', 'w')
webserver = subprocess.Popen(['./webserver.py'], stdout=webserverLog)
self.webserver = webserver
def startChildren(self, processNumber):
processLog = open('children_%s.log' % processNumber, 'w')
process = subprocess.Popen(['phantomjs',
'screenshot.js',
self.getDirectory()],
stdout=processLog)
self.childrenStarted[processNumber] = time.time()
self.childrens[processNumber] = process
def runChildrenProcesses(self):
for processNumber in xrange(0, self.getChildrenProcesses()):
self.startChildren(processNumber)
def checkProcesses(self):
for processNumber, children in self.childrens.iteritems():
if children.poll() == 0:
print 'process not running...restart'
self.startChildren(processNumber)
else:
started = time.time() - self.childrenStarted[processNumber]
if started > 60 * 10:
print 'kill and run new process'
try:
children.kill()
except OSError:
print 'this process already died by itself'
pass
self.startChildren(processNumber)
def run(self):
self.startWebServer()
self.runChildrenProcesses()
while True:
self.checkProcesses()
time.sleep(5)
if __name__ == '__main__':
pyshotx = PyshotX()
pyshotx.setUseLevels(True)
pyshotx.setDirectory('/DATA1/screenshots/')
pyshotx.setChildrenProcesses(10)
pyshotx.run()