~mozillateam/firefox/firefox.disco

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
85
86
87
88
89
90
91
#!/usr/bin/python

import datetime
import os
import select
import subprocess
import sys
import thread

proc = None

def run_child(args, fd):
  global proc

  f = os.fdopen(fd, 'w')
  proc = subprocess.Popen(args)

  # Tell main thread to continue
  f.write('a')
  f.flush()

  f.write(str(proc.wait()))

def main():
  global proc

  def usage():
    sys.stderr.write('Usage: %s <max_minutes> <cmd> [<args> ..]\n' % sys.argv[0])
    sys.exit(1)

  if len(sys.argv) < 3:
    usage()

  try:
    max_mins = int(sys.argv[1])
  except:
    usage()

  max_days = max_mins / (24 * 60)
  max_seconds = (max_mins % (max_days * 24 * 60) if max_days > 0 else max_mins) * 60

  start = datetime.datetime.now()

  (rfd, wfd) = os.pipe()
  f = os.fdopen(rfd, 'r')
  thread.start_new_thread(run_child, (sys.argv[2:], wfd))

  # Make sure that we have a process before continuing
  f.read(1)

  target_timeout = 60

  last_target_timeout = target_timeout
  timeout = target_timeout
  terminated = False
  laststart = None

  while True:
    if target_timeout != last_target_timeout:
      last_target_timeout = target_timeout
      timeout = target_timeout
    elif laststart is not None:
      now = datetime.datetime.now()
      timeout = timeout - (((now - laststart).total_seconds() - target_timeout) / 4)
    laststart = datetime.datetime.now()

    (r, w, x) = select.select([f], [], [], timeout)
    if len(r) != 0:
      sys.exit(int(f.read()))

    duration = datetime.datetime.now() - start

    if not terminated:
      sys.stdout.write('*** KEEP ALIVE MARKER ***\n')
      sys.stdout.write('Total duration: %s\n' % str(duration))
      sys.stdout.flush()

    if (duration.days >= max_days and duration.seconds >= max_seconds) or terminated:
      if not terminated:
        sys.stderr.write('Process max time exceeded, attempting to terminate\n')
        sys.stderr.flush()
        proc.terminate()
        terminated = True
        target_timeout = 15
      else:
        sys.stderr.write('Attempting to kill process\n')
        sys.stderr.flush()
        proc.kill()

if __name__ == '__main__':
  main()