hotfix: replace non-working condition that blocks flush from clearing the logs #5533
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes explanation
Although the pm2 docs mentioned users can pass <app_id> as an argument to
pm2 flush <...>
, the code doesn't recognize that. So we will be using this instead:Next, in the flush function, the condition to flush a file is depended as below:
The condition is fine, however, when the name of an app has space, the flush command will never succeed.
Here's why:
Let's say an app's name is
My Cool Server
, the log file will be created as<...home_dir...>/.pm2/logs/My-Cool-Server-out.log
.Take note that any spaces in the app name have been replaced with hyphens (-).
So when a user tries to do
pm2 flush "My Cool Server"
. The condition will be comparing the last occurrence of/
andapi
.Since
api
is the argument passed from the user, so the value will beMy Cool Server
.Now, the value of
l.pm2_env.pm_out_log_path
will be/home/user/.pm2/logs/My-Cool-Server-out.log
. ThelastIndexOf
func will try to findMy Cool Server
from the log path name. But it will never work because there are hyphens (-) in the filename! Hence, theif-else
condition is never passed, which is why the flush command doesn't flush the logs!Hence, it's now replaced with the code below:
Here, we will check whether the string
l.pm2_env.pm_out_log_path
has any value, then we will usefs.existsSync()
to check whether the file exists or not. If both returned true, we will then proceed to flush the logs.Note: The issues are present in both, the development and stable version of pm2.
A screenshot of the issue:
After applying the fix:
Now, it works fine with app_name that has spaces too!