Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hotfix: replace non-working condition that blocks flush from clearing the logs #5533

Merged
merged 1 commit into from
Jan 27, 2023

Conversation

Sailboat265
Copy link
Contributor

@Sailboat265 Sailboat265 commented Jan 20, 2023

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #4553
License MIT
Doc PR N/A

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:

else if (l.pm2_env.pm_id == api || l.pm2_env.name === api) {
    <...>
}

Next, in the flush function, the condition to flush a file is depended as below:

if (l.pm2_env.pm_out_log_path.lastIndexOf('/') < l.pm2_env.pm_out_log_path.lastIndexOf(api))

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 / and api.
Since api is the argument passed from the user, so the value will be My Cool Server.

Now, the value of l.pm2_env.pm_out_log_path will be /home/user/.pm2/logs/My-Cool-Server-out.log. The lastIndexOf func will try to find My Cool Server from the log path name. But it will never work because there are hyphens (-) in the filename! Hence, the if-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:

if (l.pm2_env.pm_out_log_path && fs.existsSync(FILEPATH))

Here, we will check whether the string l.pm2_env.pm_out_log_path has any value, then we will use fs.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:

image

After applying the fix:

image

Now, it works fine with app_name that has spaces too!

image

@Unitech Unitech merged commit 1b5e0ab into Unitech:development Jan 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants