crystal-win32svc
win32svc
Crystal lang shard which provides support for running a Crystal application as a Windows service.
Installation
-
Add the dependency to your
shard.yml
:dependencies: win32svc: github: lachlan/crystal-win32svc
-
Run
shards install
Usage
- Pass a block to
Win32::Service.run
to run that block as the service logic. The block should either monitor theWin32::Service.started?
boolean and exit when it becomesfalse
, or register a block to handle Windows service stop and shutdown events by callingWin32::Service.on_stop
, to ensure the service stops when requested by the Windows service controller.
require "win32svc"
# optionally register a block as the stop event handler
Win32::Service.on_stop do
Log.info { "Service requested to stop" }
# ...stop running the service
end
# run the service
Win32::Service.run do |args|
# continue running while service remains started
while Win32::Service.started?
# ...run loop for doing stuff...
end
end
# wait for the service to finish
Win32::Service.await
-
Compile
shards build -Dpreview_mt
- to run as a Windows Service necessarily requires at least 2 threads: the main thread which is transformed into the service dispatcher, and another thread to run the service itself. For this to work, it requires compiling using thepreview_mt
flag to enable multithreading in Crystal. -
Create Windows service
sc create <ServiceName> binpath= <ExecutablePath>
sc config <ServiceName> start= <boot|system|auto|demand|disabled|delayed-auto>
sc config <ServiceName> DisplayName= "<Service Display Name>"
sc description <ServiceName> "<Service Description>"
- Run Windows service
sc start <ServiceName>
...
sc stop <ServiceName>
Contributing
- Fork it (https://github.com/lachlan/crystal-win32svc/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- Lachlan Dowding - creator and maintainer
Repository
crystal-win32svc
Owner
Statistic
- 3
- 0
- 0
- 0
- 1
- 11 months ago
- January 11, 2024
License
MIT License
Links
Synced at
Sat, 21 Dec 2024 12:20:33 GMT
Languages