summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/fcl-extra/examples/daemon.txt
blob: 01e9607588ac9ed4cc972f5301d91e63b4eb567e (plain)
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
The daemonapp unit implements support for daemon (service) applications
in free pascal.

On Windows, these applications will act as services. On Linux, they are
normal command-line applications.

The unit implements 3 classes:

TDaemonApplication
------------------

  A TCustomApplication descendent. It is the main entry point for the
  application. It handles the starting/stopping/installing of the service.

TDaemon
-------

  A TDatamodule descendent. Here, the actual daemon service code should be
  implemented. There are several events:

  before/after install
    Executed before/after the service is being installed. Mainly useful on
    windows. Triggered if the binary is run with the --install option. 

  before/after uninstall
    Executed before/after the service is being installed. Mainly useful on
    windows. Triggered if the binary is run with the --uninstall option.

  OnStart
    Executed when the service is started. This event handler should return
    as soon as possible. So at most it should start some event loops or
    start a thread which does the actual work. if it takes a long time
    to start, call ReportStatus at regular intervals to report the current 
    status (important on  Windows)

  OnStop
    Executed when the service is stopped. This event handler should return
    as soon as possible.  if it takes a long time to stop, call ReportStatus 
     at regular intervals to report the current status (important on  Windows)

  OnPause
    Executed when the service should be paused.

  OnContinue
    Executed when the service should be continued.

  OnShutdown
    If stop does not work, the service will be forcedly shut down.

The TDaemon class has a property Logger, which is a TEventLog descendent, which
can be used to write messages to the system log.

An application can contain many daemons/services. Each of them will be run
in it's own thread. Only the main program will run in the main thread.

TDaemonMapper
-------------

  This is used to define the service(s) in the system: it contains all
  properties with which to define the services in this binary to the system.
  The definitions are kept in the DaemonDefs property (a collection)

  Each item in the daemondefs collection defines a service: it needs a TDaemon 
  descendent classname, a name (must be a unique name on the system) a 
  display name. The winbindings property contains options which are specific to
  windows: they correspond to the options one sees in the service manager.

  Note that the TDaemonMapper can be used to define 2 services, but they can
  use 2 instances of the same TDaemon descendent class to handle the service.

  for example: the daemonmapper can be used to create 2 http server daemons,
  each which listens on a separate port. TDaemon has a property Definition,
  which is the definiton that was used to create the instance.

Schematically one could draw it like this:

TDaemonApplication
 +- TDaemonMapper
     +-TDaemonDef1 -> TDaemon instance
     +-TDaemonDef2 -> TDaemon instance

Note that the daemon instances work independently, they are each running
in their own thread. (plus an additional thread which handles the control
messages from the windows service manager)

There is a lazarus package available which installs support for daemons in 
the IDE.

Michael.