Skip to content

Commit

Permalink
msi: prevent launching Fluentd wrongly if the service is running
Browse files Browse the repository at this point in the history
Before:

  We can launch Fluentd by fluentd.bat even though fluentdwinsvc is
  running.
  Launching multiple Fluentd with the same config may cause
  inconsistency of the buffers or the pos files.

After:

  We can't launch Fluentd by fluent.bat with the default config path if
  fluentdwinsvc is running.
  If some options are specified, we can execute fluentd.bat as before.

Inspired by @kenhys's PR #622.

Signed-off-by: Daijiro Fukuda <fukuda@clear-code.com>
Co-authored-by: Kentaro Hayashi <hayashi@clear-code.com>
  • Loading branch information
daipom and kenhys committed Jun 26, 2024
1 parent ce3fbf3 commit 31f118f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
29 changes: 28 additions & 1 deletion fluent-package/msi/assets/fluentd.bat
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,40 @@ set PATH=%FLUENT_PACKAGE_TOPDIR%;%PATH%
set "FLUENT_CONF=%FLUENT_PACKAGE_TOPDIR%etc/fluent/fluentd.conf"
set "FLUENT_PLUGIN=%FLUENT_PACKAGE_TOPDIR%etc/fluent/plugin"

setlocal
setlocal enabledelayedexpansion
set "FLUENT_PACKAGE_VERSION=%FLUENT_PACKAGE_TOPDIR%bin/fluent-package-version.rb"
set NEED_DUPLICATE_LAUNCH_CHECK=1
set HAS_SHORT_VERBOSE_OPTION=0

for %%p in (%*) do (
if "%%p"=="--version" (
ruby "%FLUENT_PACKAGE_VERSION%"
goto last
)
if "%%p"=="-c" set NEED_DUPLICATE_LAUNCH_CHECK=0
if "%%p"=="--config" set NEED_DUPLICATE_LAUNCH_CHECK=0
if "%%p"=="--dry-run" set NEED_DUPLICATE_LAUNCH_CHECK=0
if "%%p"=="--reg-winsvc" set NEED_DUPLICATE_LAUNCH_CHECK=0
if "%%p"=="--reg-winsvc-fluentdopt" set NEED_DUPLICATE_LAUNCH_CHECK=0
if "%%p"=="-v" set HAS_SHORT_VERBOSE_OPTION=1
)

@rem Abort if the fluentdwinsvc service is running and the config path is not specified.
if %NEED_DUPLICATE_LAUNCH_CHECK% equ 1 (
sc query fluentdwinsvc | findstr RUNNING > nul 2>&1
if !ERRORLEVEL! equ 0 (
echo Error: Can't start duplicate Fluentd instance with the default config.
if %HAS_SHORT_VERBOSE_OPTION% equ 1 (
echo.
echo To take the version, please use '--version', not '-v' ^('--verbose'^).
)
echo.
echo To start Fluentd, please do one of the following:
echo ^(Caution: Please be careful not to start multiple instances with the same config.^)
echo - Stop the Fluentd Windows service 'fluentdwinsvc'.
echo - Specify the config path explicitly by '-c' ^('--config'^).
exit /b 2
)
)
endlocal

Expand Down
35 changes: 35 additions & 0 deletions fluent-package/msi/install-test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,41 @@ Get-ChildItem "C:\\opt\\fluent\\*.log" | %{
}
}

# Test: fluentd.bat
Start-Service fluentdwinsvc

$proc = Start-Process "C:\\opt\\fluent\\fluentd.bat" -Wait -NoNewWindow -PassThru
if ($proc.ExitCode -ne 2) {
Write-Host "Failed to abort when already fluentdwinsvc service is running"
[Environment]::Exit(1)
}
Write-Host "Succeeded to abort when already fluentdwinsvc service is running"

$proc = Start-Process "C:\\opt\\fluent\\fluentd.bat" -ArgumentList "--version" -Wait -NoNewWindow -PassThru
if ($proc.ExitCode -ne 0) {
Write-Host "Failed to take the version"
[Environment]::Exit(1)
}
Write-Host "Succeeded to take the version"

$proc = Start-Process "C:\\opt\\fluent\\fluentd.bat" -ArgumentList "--dry-run" -Wait -NoNewWindow -PassThru
if ($proc.ExitCode -ne 0) {
Write-Host "Failed to dry-run"
[Environment]::Exit(1)
}
Write-Host "Succeeded to dry-run"

$fluentdopt = "-c 'C:\opt\fluent\etc\fluent\fluentd.conf' -o 'C:\opt\fluent\fluentd.log' -v"
$proc = Start-Process "C:\\opt\\fluent\\fluentd.bat" -ArgumentList "--reg-winsvc-fluentdopt ""$fluentdopt""" -Wait -NoNewWindow -PassThru
$fluentdoptResult = Get-ItemPropertyValue -Path HKLM:\SYSTEM\CurrentControlSet\Services\fluentdwinsvc -Name fluentdopt
if ($proc.ExitCode -ne 0 -or $fluentdopt -ne $fluentdoptResult) {
Write-Host "Failed to register fluentdopt"
[Environment]::Exit(1)
}
Write-Host "Succeeded to register fluentdopt"

# Test: Uninstall
Stop-Service fluentdwinsvc
$msi -Match "fluent-package-([0-9\.]+)-.+\.msi"
$name = "Fluent Package v" + $matches[1]
Write-Host "Uninstalling ...${name}"
Expand Down

0 comments on commit 31f118f

Please sign in to comment.