Daemons and Services/pl

From Free Pascal wiki
Revision as of 00:25, 9 January 2022 by Slawek (talk | contribs) (→‎Daemon Methods: tłumaczenie na j. polski)
Jump to navigationJump to search

English (en) español (es) français (fr) polski (pl) português (pt) русский (ru)

Demony i usługi

Czym są demony, usługi i agenci?

With Free Pascal, Lazarus it is possible to write these daemons/services platform-independent via the Lazarus lazdaemon package. To avoid name conflicts with the Delphi components these classes are called 'daemons'. Dzięki Free Pascal, Lazarus można pisać te demony/usługi niezależnie od platformy za pomocą pakietu Lazarus lazdaemon. Aby uniknąć konfliktów nazw z komponentami Delphi, klasy te nazywane są 'demonami' (daemons).

Instalacja LazDaemon

Zanim zaczniesz, zainstaluj pakiet lazdemon. Albo przez Komponenty / Konfiguruj zainstalowane pakiety, albo przez otwarcie/instalację pliku lpk bezpośrednio: lazarus/components/daemon/lazdemon.lpk. Ten pakiet instaluje kilka nowych komponentów i elementów menu w IDE:

W menu Plik - Nowy: 3 pozycje pojawiają się w oknie dialogowym, pod nagłówkiem: "Aplikacje Daemon(usług)":

Klasy

TCustomDaemon

To jest potomek TDataModule, który wykonuje całą pracę. Może być kilka klas i/lub instancji TCustomDaemons działających w tym samym czasie i w tym samym procesie (wielowątkowym).

TCustomDaemonApplication

To jest potomek klasy TCustomApplication, który tworzy TCustomDaemons. Nie wymaga on żadnych zmian. Działa w systemie Windows do momentu odebrania wywołania Stop lub w systemie Linux do sygnału TERM.

TDaemonMapper

Ten składnik obsługuje rejestrację usługi. Każda jego instancja wymaga jednego wpisu we właściwości DaemonDefs.

Demon - Krok po kroku

  • When the daemon is started the command line parameters are parsed. The following are predefined:
  • Gdy demon jest uruchamiany, analizowane są parametry wiersza poleceń. Wstępnie zdefiniowane są:
    • -i --install: zarejestruj demona. Nie ma to żadnego wpływu pod Uniksem.
    • -u --uninstall: wyrejestruj demona. Nie ma to żadnego wpływu pod Uniksem.
    • -r --run: uruchom demona. Windows zwykle robi to sam.
  • Utwórz TDaemonMapper
  • Utwórz jeden TCustomDaemon dla każdego wpisu DaemonDefs.
  • instaluj, odinstaluj lub uruchom każdą instancję.
  • jeśli działa: uruchom każdą instancję we własnym wątku, a następnie poczekaj na sygnał Stop/TERM.

Metody demona

Start
Wywoływana, gdy demon powinien się uruchomić. Ta metoda musi natychmiast zwrócić wartość True.
Stop
Wywoływana, gdy demon powinien się zatrzymać. Ta metoda musi natychmiast zwrócić wartość True.
Shutdown
Wywoływana, gdy demon powinien zostać zabity. Ta metoda musi natychmiast zatrzymać demona i zwrócić wartość True. Nie jest to uruchamiane pod Linuksem. Linux po prostu zabija demona.
Pause
Wywoływana, gdy demon powinien się zatrzymać. Ta metoda musi natychmiast zwrócić wartość True. W Linuksie nie jest to uruchamiane, ponieważ jądro zatrzymuje całego demona na sygnał STOP i kontynuuje go na sygnał CONT.
Continue
Wywoływana, gdy demon powinien kontynuować pracę po przerwie. Ta metoda musi natychmiast zwrócić wartość True. W Linuksie nie jest to uruchamiane.
Install
Wywoływana, gdy demon jest zarejestrowany jako usługa systemu Windows. Ta metoda powinna zwrócić wartość True w przypadku powodzenia.
Uninstall
Wywoływana, gdy demon jest wyrejestrowany jako usługa systemu Windows. Ta metoda powinna zwrócić wartość True w przypadku powodzenia.
AfterUnInstall
Wywoływana po tym, jak demon został wyrejestrowany jako usługa systemu Windows. Ta metoda powinna zwrócić wartość True w przypadku powodzenia.
HandleCustomCode
Wywoływana, gdy do demona został wysłany specjalny sygnał. Ta metoda powinna zwrócić wartość True w przypadku powodzenia.

Pierwsze kroki

Zanim będziesz mógł stworzyć aplikację Service lub Daemon, musisz najpierw upewnić się, że zainstalowałeś pakiet Lazarus Daemon "lazdemon".

Example

There is a simple example in examples/cleandir/. Read the README.txt.

Service Installation

Windows

You can install the service by executing the process with the Install parameter. Windows service manager will do the rest for you. You can configure the service and its options from the service manager.

See also: ServiceManager

System codepage / UTF-8

A LazDeamon project is working with default, not UTF-8, codepage. The -dDisableUTF8RTL mode has to be activated with Project Options ... -> Compiler Options -> Additions and Overrides -> Use system encoding.

Linux (only for older Debian)

Download, configure, and "Save As" - the sample script located at Web Archive: [1] (The original link is dead for a long time).

  • SVC_ALIAS is the long description of your application
  • SVC_FILENAME is the actual file name of your compiled service application
  • SVC_DIR is the place your you copied the service application
  • SVC_SERVICE_SCRIPT is the final name of the service.sh when you "Save As" the customized debian-service.sh script.

Place your script in the /etc/init.d/ folder

start the service by running "sudo service Name_Of_Your_Script start"

Light bulb  Uwaga: sudo has some variations, e.g.:


sudo -s #
sudo -H #
sudo -i #
sudo su #

sudo sh #

In order to auto-run the service at startup you can try update-rc.d or else will need a third party tool that will do this.

Option 1

sudo update-rc.d Name_Of_Your_Script defaults

Option 2

sudo apt-get install chkconfig
sudo chkconfig --add Name_Of_Your_Script
sudo chkconfig --level 2345 Name_Of_Your_Script on

systemd (Fedora, Debian, SLES12)

Presently, linux flavors are trending away from differing daemon launching and into a unified service model.

Fedora and SuSE Enterprise Linux Server 12 use systemd and with that commands to start/stop services are the same as on debian but there are differences on the configuration files.

  • From the command prompt sudo gedit
  • Copy and Paste
[Unit]
Description=Long description of your application
After=network.target

[Service]
Type=simple
ExecStart=complete_path_and_file_name -r
RemainAfterExit=yes
TimeoutSec=25

[Install]
WantedBy=multi-user.target
  • Edit the following values
    • Description - Long Description of your service application
    • ExecStart - complete-path_and_file_name is the name of your compiled service application with its complete path
  • Save As Dialog
    • Navigate to /lib/systemd/system/
    • Name the file the name_of_your_service.service

See also