-
Notifications
You must be signed in to change notification settings - Fork 31
Add cpp_supervised_app_simple example for the Alive API #362
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
Open
Narasimha2211
wants to merge
1
commit into
eclipse-score:main
Choose a base branch
from
Narasimha2211:234-cpp-supervised-app-simple
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # ******************************************************************************* | ||
| # Copyright (c) 2026 Contributors to the Eclipse Foundation | ||
| # | ||
| # See the NOTICE file(s) distributed with this work for additional | ||
| # information regarding copyright ownership. | ||
| # | ||
| # This program and the accompanying materials are made available under the | ||
| # terms of the Apache License Version 2.0 which is available at | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # ******************************************************************************* | ||
| load("@rules_cc//cc:defs.bzl", "cc_binary") | ||
|
|
||
| cc_binary( | ||
| name = "cpp_supervised_app_simple", | ||
| srcs = [ | ||
| "main.cpp", | ||
| ], | ||
| copts = select({ | ||
| "@platforms//os:qnx": [], | ||
| "@platforms//os:linux": [], | ||
| }), | ||
| linkopts = select({ | ||
| "@platforms//os:qnx": [ | ||
| "-lsocket", | ||
| ], | ||
| "@platforms//os:linux": [ | ||
| "-lpthread", | ||
| "-lrt", | ||
| ], | ||
| }), | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| "//score/launch_manager:alive_cc", | ||
| "//score/launch_manager:lifecycle_cc", | ||
| ], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /******************************************************************************** | ||
| * Copyright (c) 2026 Contributors to the Eclipse Foundation | ||
| * | ||
| * See the NOTICE file(s) distributed with this work for additional | ||
| * information regarding copyright ownership. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Apache License Version 2.0 which is available at | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| ********************************************************************************/ | ||
|
|
||
| #include <unistd.h> | ||
| #include <atomic> | ||
| #include <chrono> | ||
| #include <csignal> | ||
| #include <cstdint> | ||
| #include <iostream> | ||
| #include <optional> | ||
| #include <thread> | ||
|
|
||
| #ifdef __linux__ | ||
| #include <linux/prctl.h> | ||
| #include <sys/prctl.h> | ||
| #endif | ||
|
|
||
| #include <score/mw/lifecycle/alive.h> | ||
| #include <score/mw/lifecycle/report_running.h> | ||
|
|
||
| /// @brief CLI configuration options for the cpp_supervised_app_simple process | ||
| struct Config | ||
| { | ||
| std::uint32_t sleepInMs{50}; | ||
| }; | ||
|
|
||
| std::optional<Config> parseOptions(int argc, char* const* argv) noexcept | ||
| { | ||
| Config config{}; | ||
| int c; | ||
| while ((c = getopt(argc, argv, "s:h")) != -1) | ||
| { | ||
| switch (static_cast<char>(c)) | ||
| { | ||
| case 's': | ||
| config.sleepInMs = std::stoi(optarg); | ||
| break; | ||
|
|
||
| case 'h': | ||
| std::cout << "Usage: \n\ | ||
| -s <Sleep time in ms between alive notifications>\n"; | ||
| return std::nullopt; | ||
|
|
||
| default: | ||
| break; | ||
| } | ||
| } | ||
| return config; | ||
| } | ||
|
|
||
| std::atomic<bool> exitRequested{false}; | ||
| void signalHandler(int signal) | ||
| { | ||
| if (signal == SIGINT || signal == SIGTERM) | ||
| { | ||
| exitRequested = true; | ||
| } | ||
| } | ||
|
|
||
| void set_process_name() | ||
| { | ||
| const char* identifier = getenv("PROCESSIDENTIFIER"); | ||
| if (identifier != nullptr) | ||
| { | ||
| #if defined(__QNXNTO__) | ||
| if (pthread_setname_np(pthread_self(), identifier) != 0) | ||
| { | ||
| std::cerr << "Failed to set QNX thread name" << std::endl; | ||
| } | ||
| #elif defined(__linux__) | ||
| if (prctl(PR_SET_NAME, identifier) < 0) | ||
| { | ||
| std::cerr << "Failed to set process name to " << identifier << std::endl; | ||
| } | ||
| #endif | ||
| } | ||
| } | ||
|
|
||
| /// @brief Minimal example of the Alive API: report Running once, then report | ||
| /// alive notifications periodically until the Launch Manager requests shutdown. | ||
| /// Unlike cpp_supervised_app, this example has no deadline/checkpoint | ||
| /// supervision -- it only demonstrates the plain alive heartbeat. | ||
| int main(int argc, char** argv) | ||
| { | ||
| set_process_name(); | ||
|
|
||
| signal(SIGINT, signalHandler); | ||
| signal(SIGTERM, signalHandler); | ||
|
|
||
| const auto config = parseOptions(argc, argv); | ||
| if (!config) | ||
| { | ||
| return EXIT_FAILURE; | ||
| } | ||
|
|
||
| std::optional<score::mw::lifecycle::Alive> alive; | ||
| try | ||
| { | ||
| alive.emplace("cpp_supervised_app_simple"); | ||
| } | ||
| catch (const std::exception& e) | ||
| { | ||
| std::cerr << "Failed to create Alive instance: " << e.what() << std::endl; | ||
| return EXIT_FAILURE; | ||
| } | ||
|
|
||
| score::mw::lifecycle::report_running(); | ||
|
|
||
| while (!exitRequested) | ||
| { | ||
| alive->ReportAlive(); | ||
| std::this_thread::sleep_for(std::chrono::milliseconds(config->sleepInMs)); | ||
| } | ||
|
|
||
| return EXIT_SUCCESS; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could remove this. This is not required for the functionality. In some other sample app we had this only for some demo purpose.