daemon.txt 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. The daemonapp unit implements support for daemon (service) applications
  2. in free pascal.
  3. On Windows, these applications will act as services. On Linux, they are
  4. normal command-line applications.
  5. The unit implements 3 classes:
  6. TDaemonApplication
  7. ------------------
  8. A TCustomApplication descendent. It is the main entry point for the
  9. application. It handles the starting/stopping/installing of the service.
  10. TDaemon
  11. -------
  12. A TDatamodule descendent. Here, the actual daemon service code should be
  13. implemented. There are several events:
  14. before/after install
  15. Executed before/after the service is being installed. Mainly useful on
  16. windows. Triggered if the binary is run with the --install option.
  17. before/after uninstall
  18. Executed before/after the service is being installed. Mainly useful on
  19. windows. Triggered if the binary is run with the --uninstall option.
  20. OnStart
  21. Executed when the service is started. This event handler should return
  22. as soon as possible. So at most it should start some event loops or
  23. start a thread which does the actual work. if it takes a long time
  24. to start, call ReportStatus at regular intervals to report the current
  25. status (important on Windows)
  26. OnStop
  27. Executed when the service is stopped. This event handler should return
  28. as soon as possible. if it takes a long time to stop, call ReportStatus
  29. at regular intervals to report the current status (important on Windows)
  30. OnPause
  31. Executed when the service should be paused.
  32. OnContinue
  33. Executed when the service should be continued.
  34. OnShutdown
  35. If stop does not work, the service will be forcedly shut down.
  36. The TDaemon class has a property Logger, which is a TEventLog descendent, which
  37. can be used to write messages to the system log.
  38. An application can contain many daemons/services. Each of them will be run
  39. in it's own thread. Only the main program will run in the main thread.
  40. TDaemonMapper
  41. -------------
  42. This is used to define the service(s) in the system: it contains all
  43. properties with which to define the services in this binary to the system.
  44. The definitions are kept in the DaemonDefs property (a collection)
  45. Each item in the daemondefs collection defines a service: it needs a TDaemon
  46. descendent classname, a name (must be a unique name on the system) a
  47. display name. The winbindings property contains options which are specific to
  48. windows: they correspond to the options one sees in the service manager.
  49. Note that the TDaemonMapper can be used to define 2 services, but they can
  50. use 2 instances of the same TDaemon descendent class to handle the service.
  51. for example: the daemonmapper can be used to create 2 http server daemons,
  52. each which listens on a separate port. TDaemon has a property Definition,
  53. which is the definiton that was used to create the instance.
  54. Schematically one could draw it like this:
  55. TDaemonApplication
  56. +- TDaemonMapper
  57. +-TDaemonDef1 -> TDaemon instance
  58. +-TDaemonDef2 -> TDaemon instance
  59. Note that the daemon instances work independently, they are each running
  60. in their own thread. (plus an additional thread which handles the control
  61. messages from the windows service manager)
  62. There is a lazarus package available which installs support for daemons in
  63. the IDE.
  64. Michael.