Autoreload Apache on project modifications

 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
#!/usr/bin/env python

# Usage: reloader.py dirToWatch [minDelay]
# reloader.py is Public Domain.

# Requires the development version of pyinotify
# See: http://seb.dbzteam.com/pages/pyinotify-dev.html
# Download: http://seb.dbzteam.com/pub/pyinotify.py

# Change the two settings below-

# changes to files with these extensions will trigger runCmd
reloadExtensions = ['.py', '.wsgi'] 
# command passed to os.system
runCmd = "/usr/sbin/apache2ctl restart"

import os
import sys
import time

import pyinotify

try:
    watchDir = sys.argv[1]
except IndexError:
    print "Usage: reloader.py dirToWatch [minDelay]"
    sys.exit()

minDelay = 0.5 # minimum delay before running the command again
if len(sys.argv) > 2:
    minDelay = float(sys.argv[2])
mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE | \
     pyinotify.IN_MODIFY # watched events

lastTime = 0

wm = pyinotify.WatchManager()

class PTmp(pyinotify.ProcessEvent):
    def process_default(self, event):
        global lastTime
        print("Change in %s" % os.path.join(event.path, event.name))
        if os.path.splitext(event.name)[-1] in reloadExtensions:
            if time.time() > (lastTime + minDelay):
                print "Running " + runCmd
                os.system(runCmd)
                lastTime = time.time()
            else:
                print "Timeout not reached, not running command."

notifier = pyinotify.Notifier(wm, PTmp())

wdd = wm.add_watch(watchDir, mask, rec=True, auto_add=True)

while True:
    try:
        notifier.process_events()
        if notifier.check_events():
            notifier.read_events()
    except KeyboardInterrupt:
        notifier.stop()
        break

More like this

  1. MODPYTHON logging by adroffner 5 years, 2 months ago
  2. MODPYTHON Sample Site Logging by adroffner 4 years, 9 months ago
  3. Apache X-sendfile with permissions checking by h0axify 1 year, 1 month ago
  4. django under apache / mod_fcgid by mike_45 4 years, 3 months ago
  5. Log username in Apache access logs by arthur 3 years, 2 months ago

Comments

(Forgotten your password?)