fix(cron): Avoid error in logs of cron jobs before configuration is completed
Description
Fixes #MON-167678
Type of change
- [x] Patch fixing an issue (non-breaking change)
- [ ] New functionality (non-breaking change)
- [ ] Breaking change (patch or feature) that might cause side effects breaking part of the Software
Target serie
- [ ] 22.10.x
- [ ] 23.04.x
- [ ] 23.10.x
- [ ] 24.04.x
- [x] 24.10.x
- [x] master
📝 Walkthrough
Walkthrough
A series of Centreon cron scripts were updated to add a pre-execution check for the required configuration file (centreon.conf.php for PHP scripts and conf.pm/centreon-config.pm for the Perl script). If the configuration file is missing, the script determines whether it is running interactively (in a terminal) or non-interactively (such as from cron). When run interactively, an error message is displayed and the script exits with status 1; when run non-interactively, it exits silently with status 0. Additionally, unused help and usage functions were removed from the Perl backup script.
Changes
| File(s) | Change Summary |
|---|---|
| centreon/cron/centAcl.php centreon/cron/centKnowledgeSynchronizer.php centreon/cron/centreon-partitioning.php centreon/cron/centreon-send-stats.php centreon/cron/centstorage_purge.php centreon/cron/downtimeManager.php centreon/cron/outdated-token-removal.php |
Added a check at the start of each script to verify the existence of centreon.conf.php in the configuration directory. If missing, the script checks if it is running interactively using posix_isatty(STDIN). If interactive, it prints an error to STDERR and exits with code 1; if not, it exits silently with code 0. This prevents further execution when the configuration file is absent and differentiates behavior based on execution context. No changes to public APIs or function signatures. |
| centreon/cron/centreon-backup.pl | Removed the print_help() and print_usage() functions. Added a check for the presence of configuration files (conf.pm or centreon-config.pm). If neither exists, the script checks if it is running in a terminal; if so, prints an error to STDERR and exits with code 1, otherwise exits silently with code 0. No other functional changes. |
Sequence Diagram(s)
sequenceDiagram
participant User/Cron as User or Cron
participant Script as Centreon Cron Script
User/Cron->>Script: Start script
Script->>Script: Check if config file exists
alt Config file exists
Script->>Script: Proceed with normal execution
else Config file missing
Script->>Script: Check if running interactively (posix_isatty(STDIN))
alt Interactive
Script->>User/Cron: Print error to STDERR, exit 1
else Non-interactive (cron)
Script->>User/Cron: Exit silently with code 0
end
end
[!WARNING]
Review ran into problems
🔥 Problems
Errors were encountered while retrieving linked issues.
Errors (1)
- JIRA integration encountered authorization issues. Please disconnect and reconnect the integration in the CodeRabbit UI.
📜 Recent review details
Configuration used: .coderabbit.yml Review profile: CHILL Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between a994b75d5ba54b546c523677483b092d04bf2105 and 6a18e82f81dd83469ae020c0cc714d421cf570db.
📒 Files selected for processing (8)
centreon/cron/centAcl.php(1 hunks)centreon/cron/centKnowledgeSynchronizer.php(1 hunks)centreon/cron/centreon-backup.pl(1 hunks)centreon/cron/centreon-partitioning.php(1 hunks)centreon/cron/centreon-send-stats.php(1 hunks)centreon/cron/centstorage_purge.php(1 hunks)centreon/cron/downtimeManager.php(1 hunks)centreon/cron/outdated-token-removal.php(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: backend-lint
- GitHub Check: check-status
🔇 Additional comments (8)
centreon/cron/downtimeManager.php (1)
43-51: LGTM: Clean implementation of configuration file check.The code properly checks for the existence of the configuration file and handles different execution contexts appropriately - displaying an error when run interactively while silently exiting when run from cron to avoid cluttering logs.
centreon/cron/centreon-partitioning.php (1)
44-52: LGTM: Clean implementation of configuration file check.The code properly checks for the configuration file existence and handles different execution contexts appropriately, which aligns perfectly with the PR objective to avoid errors in logs when cron jobs run before configuration is completed.
centreon/cron/centreon-send-stats.php (1)
30-38: LGTM: Clean implementation of configuration file check.The implementation properly verifies that the required configuration file exists and differentiates between interactive and non-interactive execution contexts, which meets the PR objective of avoiding error logs in cron jobs.
centreon/cron/outdated-token-removal.php (1)
29-37: LGTM: Clean implementation of configuration file check.This implementation correctly checks for the existence of the configuration file and handles the execution context appropriately to avoid errors in logs when run from cron, consistent with the other scripts in this PR.
centreon/cron/centAcl.php (1)
45-53: Good addition of configuration file check before proceedingThis change properly handles cases where the configuration file doesn't exist, differentiating between interactive and non-interactive execution. For cron jobs (non-interactive), it exits silently with status 0, which prevents unnecessary error logs - directly addressing the PR objective.
centreon/cron/centKnowledgeSynchronizer.php (1)
38-46: Consistent implementation of configuration file checkThis change correctly checks for the existence of the configuration file before proceeding with the script execution. The differentiated behavior between interactive and non-interactive execution is appropriate - showing errors to users but avoiding log pollution when run from cron.
centreon/cron/centstorage_purge.php (1)
38-46: Good implementation of configuration file checkLike the other scripts, this change properly checks if the required configuration file exists before continuing. The error handling is appropriate - displaying errors when run interactively, but exiting silently when run from cron to avoid polluting logs.
centreon/cron/centreon-backup.pl (1)
65-73: Effective implementation of configuration file check in PerlThis Perl implementation correctly checks for the existence of either configuration file (
conf.pmorcentreon-config.pm) and follows the same pattern as the PHP scripts - showing errors in interactive mode but exiting silently when run from cron. The comment "Probably cron, let's not pollute logs" clearly explains the intent.
✨ Finishing Touches
- [ ] 📝 Generate Docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
🪧 Tips
Chat
There are 3 ways to chat with CodeRabbit:
‼️ IMPORTANT Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.
- Files and specific lines of code (under the "Files changed" tab): Tag
@coderabbitaiin a new review comment at the desired location with your query. Examples:@coderabbitai generate unit testing code for this file.@coderabbitai modularize this function.
- PR comments: Tag
@coderabbitaiin a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.@coderabbitai read src/utils.ts and generate unit testing code.@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.@coderabbitai help me debug CodeRabbit configuration file.
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.
CodeRabbit Commands (Invoked using PR comments)
@coderabbitai pauseto pause the reviews on a PR.@coderabbitai resumeto resume the paused reviews.@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository.@coderabbitai full reviewto do a full review from scratch and review all the files again.@coderabbitai summaryto regenerate the summary of the PR.@coderabbitai generate docstringsto generate docstrings for this PR.@coderabbitai generate sequence diagramto generate a sequence diagram of the changes in this PR.@coderabbitai resolveresolve all the CodeRabbit review comments.@coderabbitai configurationto show the current CodeRabbit configuration for the repository.@coderabbitai helpto get help.
Other keywords and placeholders
- Add
@coderabbitai ignoreanywhere in the PR description to prevent this PR from being reviewed. - Add
@coderabbitai summaryto generate the high-level summary at a specific location in the PR description. - Add
@coderabbitaianywhere in the PR title to generate the title automatically.
Documentation and Community
- Visit our Documentation for detailed information on how to use CodeRabbit.
- Join our Discord Community to get help, request features, and share feedback.
- Follow us on X/Twitter for updates and announcements.