BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | # |
2 | # Watches a directory and runs an update program whenever something |
||
3 | # changes. This is race-free, and if the directory changes while the |
||
4 | # update is running, it will be done again. |
||
5 | # |
||
6 | # NOTE: the update should not modify the directory. If it does, the |
||
7 | # the result will be endless and constant updating. |
||
8 | # |
||
9 | |||
10 | process watcher { |
||
11 | # Blocker object used to trigger an update. |
||
12 | blocker() blk; |
||
13 | |||
14 | # State: updating - if updater script is running |
||
15 | # updating_dirty - if something changed while |
||
16 | # script was running |
||
17 | var("false") updating; |
||
18 | var("false") updating_dirty; |
||
19 | |||
20 | # Start update process. |
||
21 | spawn("updater", {}); |
||
22 | |||
23 | # Wait for directory event. |
||
24 | # CHANGE THIS |
||
25 | sys.watch_directory("/home/ambro") watcher; |
||
26 | |||
27 | # Print event details (e.g. "added somefile"). |
||
28 | println(watcher.filename, " ", watcher.event_type); |
||
29 | |||
30 | # If updating is in progress, mark dirty. |
||
31 | If (updating) { |
||
32 | updating_dirty->set("true"); |
||
33 | }; |
||
34 | |||
35 | # Request update. This makes use() proceed forward. |
||
36 | blk->up(); |
||
37 | |||
38 | # Wait for next event (execution moves up). |
||
39 | watcher->nextevent(); |
||
40 | } |
||
41 | |||
42 | template updater { |
||
43 | # Wait for update request. |
||
44 | _caller.blk->use(); |
||
45 | |||
46 | # Wait some time. |
||
47 | sleep("1000", "0"); |
||
48 | |||
49 | # We're about to start update script - set updating |
||
50 | # variable and mark as non dirty. |
||
51 | _caller.updating_dirty->set("false"); |
||
52 | _caller.updating->set("true"); |
||
53 | |||
54 | println("Update started"); |
||
55 | |||
56 | # CHANGE THIS |
||
57 | sleep("3000", "0"); |
||
58 | #runonce({"/bin/echo", "Updater speaking"}, {"keep_stdout", "keep_stderr"}); |
||
59 | |||
60 | println("Update finished"); |
||
61 | |||
62 | # No longer running update script. |
||
63 | _caller.updating->set("false"); |
||
64 | |||
65 | # If something changed while script was running, restart |
||
66 | # update; else wait for next update request. |
||
67 | If (_caller.updating_dirty) { |
||
68 | _caller.blk->downup(); |
||
69 | } else { |
||
70 | _caller.blk->down(); |
||
71 | }; |
||
72 | } |