directory_updater.ncd 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. process watcher {
  10. # Blocker object used to trigger an update.
  11. blocker() blk;
  12. # State: updating - if updater script is running
  13. # updating_dirty - if something changed while
  14. # script was running
  15. var("false") updating;
  16. var("false") updating_dirty;
  17. # Start update process.
  18. spawn("updater", {});
  19. # Wait for directory event.
  20. # CHANGE THIS
  21. sys.watch_directory("/home/ambro") watcher;
  22. # Print event details (e.g. "added somefile").
  23. println(watcher.filename, " ", watcher.event_type);
  24. # If updating is in progress, mark dirty.
  25. If (updating) {
  26. updating_dirty->set("true");
  27. };
  28. # Request update. This makes use() proceed forward.
  29. blk->up();
  30. # Wait for next event (execution moves up).
  31. watcher->nextevent();
  32. }
  33. template updater {
  34. # Wait for update request.
  35. _caller.blk->use();
  36. # Wait some time.
  37. sleep("1000", "0");
  38. # We're about to start update script - set updating
  39. # variable and mark as non dirty.
  40. _caller.updating_dirty->set("false");
  41. _caller.updating->set("true");
  42. println("Update started");
  43. # CHANGE THIS
  44. sleep("3000", "0");
  45. #runonce({"/bin/echo", "Updater speaking"}, {"keep_stdout", "keep_stderr"});
  46. println("Update finished");
  47. # No longer running update script.
  48. _caller.updating->set("false");
  49. # If something changed while script was running, restart
  50. # update; else wait for next update request.
  51. If (_caller.updating_dirty) {
  52. _caller.blk->downup();
  53. } else {
  54. _caller.blk->down();
  55. };
  56. }