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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
unit DaemonUnit;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, DaemonApp, DaemonWorkerThread
{$IFDEF UNIX}
, DaemonSystemdInstallerUnit // Some systemd installer support for -install | -ininstall
{$ENDIF};
// -------------------------------------------------------------------------------
// Demo application on how to use the TDaemon application type
// 2/2022 by arminlinder@arminlinder.de
// Based on document
// "Taming the daemon: Writing cross-platform service applications in FPC/Lazarus"
// by Michaƫl Van Canneyt, February 4, 2007
// -------------------------------------------------------------------------------
const
DAEMON_VERSION = '1.2'; // Just for logging
type
{ TTestDaemon }
TTestDaemon = class(TDaemon)
procedure DataModuleAfterInstall(Sender: TCustomDaemon);
procedure DataModuleAfterUnInstall(Sender: TCustomDaemon);
procedure DataModuleContinue(Sender: TCustomDaemon; var OK: boolean);
procedure DataModulePause(Sender: TCustomDaemon; var OK: boolean);
procedure DataModuleStart(Sender: TCustomDaemon; var OK: boolean);
procedure DataModuleStop(Sender: TCustomDaemon; var OK: boolean);
private
FDaemonWorkerThread: TDaemonWorkerThread;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;
procedure LogToFile(aMessage: string);
var
TestDaemon: TTestDaemon;
implementation
{$R *.lfm}
// ---------------------------------------------------------------------
// Quick and dirty write log message to file
// Note: TDaemonApplication has extensive logging capabilities built-in!
// ---------------------------------------------------------------------
procedure LogToFile(aMessage: string);
function TimeStamped(S: string): string;
// Return a timestamped copy of a string
begin
Result := FormatDateTime('hh:mm:ss', now) + ' ' + S;
end;
var
f: Text;
LogFilePath: string;
begin
// create a daily log file in the .exe directory
LogFilePath := IncludeTrailingPathDelimiter(ExtractFilePath(Application.ExeName)) +
FormatDateTime('YYYYMMDD', now) + '.log';
AssignFile(f, LogFilePath);
try
if FileExists(LogFilePath) then
Append(f)
else
begin
Rewrite(f);
writeln(f, TimeStamped('Log created'));
end;
Writeln(f, TimeStamped(aMessage));
finally
CloseFile(f);
end;
end;
{ TTestDaemon }
// -------------------------------
// Installation and deinstallation
// -------------------------------
// Note: Windows code is already in the DaemonApp unit
// Code here tries to approximate same functionality for Unix
procedure TTestDaemon.DataModuleAfterInstall(Sender: TCustomDaemon);
var
isInstalled: boolean;
FilePath: string;
begin
isInstalled := True;
{$IFDEF UNIX}
FilePath := GetSystemdControlFilePath(self.Name);
isInstalled := CreateSystemdControlFile(self, FilePath);
if not isInstalled then
LogToFile('Error creating systemd control file: ' + FilePath);
{$ENDIF}
if isInstalled then
LogToFile('Daemon installed, version:' + DAEMON_VERSION);
end;
procedure TTestDaemon.DataModuleAfterUnInstall(Sender: TCustomDaemon);
var
isUnInstalled: boolean;
FilePath: string;
begin
isUninstalled := True;
{$IFDEF UNIX}
FilePath := GetSystemdControlFilePath(self.Name);
isUnInstalled := RemoveSystemdControlFile(FilePath);
if not isUninstalled then
LogToFile('Error removing systemd control file: ' + FilePath);
{$ENDIF}
if isUninstalled then
LogToFile('Daemon uninstalled');
end;
// -----------------------
// Service control signals
// -----------------------
procedure TTestDaemon.DataModuleStart(Sender: TCustomDaemon; var OK: boolean);
begin
LogToFile(Format('Daemon received start signal, PID:%d', [GetProcessID]));
// Create a suspended worker thread
FDaemonWorkerThread := TDaemonWorkerThread.Create;
// Parametrize it
FDaemonWorkerThread.FreeOnTerminate := False;
// Start the worker
FDaemonWorkerThread.Start;
OK := True;
end;
procedure TTestDaemon.DataModulePause(Sender: TCustomDaemon; var OK: boolean);
// Note: Pause and resume signals are provided by Windows only
begin
LogToFile('Daemon recedived pause signal');
FDaemonWorkerThread.Suspend; // deprecated, yet still working
OK := True;
end;
procedure TTestDaemon.DataModuleContinue(Sender: TCustomDaemon; var OK: boolean);
// Note: Pause and resume signals are provided by Windows only
begin
LogToFile('Daemon recedived continue signal');
FDaemonWorkerThread.resume; // deprecated, yet still working
OK := True;
end;
procedure TTestDaemon.DataModuleStop(Sender: TCustomDaemon; var OK: boolean);
begin
LogToFile('Daemon received stop signal');
// stop and terminate the worker
FDaemonWorkerThread.Terminate;
// Wait for the thread to terminate.
FDaemonWorkerThread.WaitFor;
FDaemonWorkerThread.Free;
LogToFile('Daemon stopped');
OK := True;
end;
// --------------------------
// constructor and destructor
// --------------------------
constructor TTestDaemon.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
// Nothing to do here, just for logging
LogToFile('Daemon object created');
end;
destructor TTestDaemon.Destroy;
begin
// Nothing to do here, just for logging
LogToFile('Daemon object destroyed');
inherited Destroy;
end;
// ----------------------------------------
// Daemon registration. Created by template
// ----------------------------------------
procedure RegisterDaemon;
begin
RegisterDaemonClass(TTestDaemon);
end;
initialization
RegisterDaemon;
end.
|