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

Fixes pm2 not flushing logs #5530

Closed
wants to merge 4 commits into from

Conversation

Sailboat265
Copy link
Contributor

@Sailboat265 Sailboat265 commented Jan 18, 2023

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

What's this PR about?

This PR aims to fix and restore the ability to use either pm2 flush <app_id> or pm2 flush <app_name> to flush logs successfully.

Why so many commits?

In the first commit of this PR, I used a regex to extract a valid filename from a log path to allow the existing if-else comparison to continue working whatever it's intended.

It worked perfectly. Well only if the user didn't seek to customize their logging path or name! Because it's found out later that this solution isn't feasible as it would mean that a log file must end with an extension of .log, or else the flush will not work.

An example is when a user tries to redirect stderr to a file like ~/myApp/logs/process.error, the solution will not be able to handle this. Hence, I have chosen to revert the changes made by the first commit.

About the latest commit

Now, about the latest commit of this PR. The printout msg of flushing a log file has been moved inside their respective if block. This fixed a problem where users are reporting that the command is printing out the log file message like below even though the logs aren't even getting flushed.

[PM2] Flushing:
[PM2] /home/pi/.pm2/logs/My-App-out.log
[PM2] /home/pi/.pm2/logs/My-App-error.log
[PM2] Logs flushed

And I don't really get what's this if-else trying to accomplish within its logic comparison.

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

Anyways, we will be replacing it and use fs.existsSync(FILE_PATH) to check whether a log file exists or not. So now, part of the code looks like this:

if (fs.existsSync(l.pm2_env.pm_out_log_path)) {
    Common.printOut(cst.PREFIX_MSG + l.pm2_env.pm_out_log_path);
    fs.closeSync(fs.openSync(l.pm2_env.pm_out_log_path, 'w'));
}

This solution should be universal enough that it allows the command to flush the logs correctly even though a unique log path has been specified.

@CLAassistant
Copy link

CLAassistant commented Jan 18, 2023

CLA assistant check
All committers have signed the CLA.

@Sailboat265 Sailboat265 marked this pull request as draft January 19, 2023 08:31
@Sailboat265 Sailboat265 marked this pull request as ready for review January 19, 2023 09:48
@Sailboat265 Sailboat265 changed the base branch from master to development January 19, 2023 09:57
@Sailboat265
Copy link
Contributor Author

This pull request is too polluted and it seems like I didn't adhere to the CONTRIBUTING rules too.

Hence, I have decided to move to a new PR, #5533.

@Sailboat265 Sailboat265 deleted the master branch January 28, 2023 05:48
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.

3 participants