fluent / fluent-package-builder

td-agent (Fluentd) Building and Packaging System
Apache License 2.0
21 stars 23 forks source link

msi: guard duplicated instance #622

Closed kenhys closed 1 week ago

kenhys commented 4 months ago

msi: guard multiple Fluentd instance

Before:

  Even though fluentdwinsvc is running, you can execute duplicated
  Fluentd instance.
  It may cause inconsistency of buffer or pos file.

After:

  Guard multiple Fluentd instance when fluendwinsvc service is
  running by default.

  Note that --force option is specified, you can run additional
  Fluentd instance explicitly. (it is same as previous behavior)
kenhys commented 4 months ago

This is PoC

kenhys commented 4 months ago

Basically it will work, but there is one concern.

If user execute directly c:\opt\fluent\bin\fluentd, it can not be blocked. Usually fluent package prompt will be used, so it is a rare case.

NOTE: search path is like this:

c:\opt\fluent>where fluentd.bat
c:\opt\fluent\fluentd.bat
c:\opt\fluent\bin\fluentd.bat
kenhys commented 4 months ago

It should use findstr /I

kenhys commented 4 months ago

Verified when fluentdwinsvc is running, it will abort like this:

c:\opt\fluent>fluentd.bat -c c:\opt\fluent\etc\fluent\fluentd.conf
Error: can't start duplicate Fluentd instance with same "c:\opt\fluent\etc\fluent\fluentd.conf"

c:\opt\fluent>fluentd.bat
Error: can't start duplicate Fluentd instance with same c:/opt/fluent/etc/fluent/fluentd.conf
kenhys commented 4 months ago

Hmm, it seems that fluentd.bat exit expectedly, but not capture $LASTEXITCODE correctly.

2024-02-20T08:26:44.9840365Z DEBUG:   55+  >>>> Start-Process "C:\\opt\\fluent\\fluentd.bat" -ArgumentList   
2024-02-20T08:26:44.9842223Z "-c", "C:\\opt\\fluent\\etc\\fluent\\fluentd.conf" -Wait -NoNewWindow
2024-02-20T08:26:44.9843565Z 
2024-02-20T08:26:45.0579514Z [?25h[?25lError: can't start duplicate Fluentd instance with same C:/opt/fluent/etc/fluent[?25h[?25l
2024-02-20T08:26:45.0580328Z /fluentd.conf
2024-02-20T08:26:45.0580632Z [?25h[?25l
2024-02-20T08:26:45.0580880Z 
2024-02-20T08:26:45.9898406Z [?25h1[?25lDEBUG:   56+  >>>> $exitcode = $LASTEXITCODE 
2024-02-20T08:26:45.9929166Z [?25h[?25l1[?25h[?25l
2024-02-20T08:26:45.9929643Z DEBUG:   57+ if ( >>>> $exitcode -ne 2) {
2024-02-20T08:26:45.9952814Z [?25h[?25l
2024-02-20T08:26:45.9953365Z DEBUG:   58+      >>>> Write-Host "Failed to abort when it is conflict with     
2024-02-20T08:26:45.9955671Z [?25h[?25l
2024-02-20T08:26:45.9956365Z running fluentdwinsvc"
2024-02-20T08:26:45.9986556Z [?25h[?25l
2024-02-20T08:26:45.9995251Z Failed to abort when it is conflict with running fluentdwinsvc[?25h[?25l
2024-02-20T08:26:45.9996149Z DEBUG:   59+      >>>> [Environment]::Exit(1)
2024-02-20T08:26:46.0051281Z [?25h[?25l
2024-02-20T08:26:46.0051698Z 
2024-02-20T08:26:50.0433064Z [?25hD
2024-02-20T08:26:50.0555867Z ##[error]Process completed with exit code 1.
2024-02-20T08:26:50.0788284Z Post job cleanup.
kenhys commented 4 months ago

With locally built msi, it work as expected.

PS C:\opt\fluent> .\fluentd.bat
Error: can't start duplicate Fluentd instance with same C:/opt/fluent/etc/fluent/fluentd.conf
PS C:\opt\fluent> $exitcode = $LASTEXITCODE
PS C:\opt\fluent> $exitcode
2
PS C:\opt\fluent>
kenhys commented 4 months ago

It's a problem of test code. It should use -PassThru and capture with .ExitCode.

kenhys commented 4 months ago

Test code was fixed, waiting CI.

kenhys commented 4 months ago

All checks has passed.

kenhys commented 4 months ago

I've forgot to eliminate --force from command line arguments. It was fixed now.

kenhys commented 4 months ago

pr622

kenhys commented 1 week ago

647 and #648 approaches are more better alternatives.

daipom commented 1 week ago

Thanks for considering. If we need a force execution option or a strict check whether the config path is the same in the future, let's revive this!

Note: strict check: https://github.com/fluent/fluent-package-builder/blob/f045a795e8f02c9731262f29a6832d95b40c85a1/fluent-package/msi/assets/fluentd.bat