Skip to content

Commit

Permalink
Merge pull request #28640 from loganharbour/28639_exit_code
Browse files Browse the repository at this point in the history
  • Loading branch information
loganharbour authored Sep 27, 2024
2 parents c889ff8 + 342dfe7 commit bfd4591
Show file tree
Hide file tree
Showing 65 changed files with 126 additions and 206 deletions.
4 changes: 1 addition & 3 deletions examples/ex01_inputfile/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@
int
main(int argc, char * argv[])
{
Moose::main<ExampleApp>(argc, argv);

return 0;
return Moose::main<ExampleApp>(argc, argv);
}
4 changes: 1 addition & 3 deletions examples/ex02_kernel/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@
int
main(int argc, char * argv[])
{
Moose::main<ExampleApp>(argc, argv);

return 0;
return Moose::main<ExampleApp>(argc, argv);
}
4 changes: 1 addition & 3 deletions examples/ex03_coupling/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,5 @@
int
main(int argc, char * argv[])
{
Moose::main<ExampleApp>(argc, argv);

return 0;
return Moose::main<ExampleApp>(argc, argv);
}
4 changes: 1 addition & 3 deletions examples/ex04_bcs/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,5 @@
int
main(int argc, char * argv[])
{
Moose::main<ExampleApp>(argc, argv);

return 0;
return Moose::main<ExampleApp>(argc, argv);
}
4 changes: 1 addition & 3 deletions examples/ex05_amr/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,5 @@
int
main(int argc, char * argv[])
{
Moose::main<ExampleApp>(argc, argv);

return 0;
return Moose::main<ExampleApp>(argc, argv);
}
4 changes: 1 addition & 3 deletions examples/ex06_transient/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,5 @@
int
main(int argc, char * argv[])
{
Moose::main<ExampleApp>(argc, argv);

return 0;
return Moose::main<ExampleApp>(argc, argv);
}
4 changes: 1 addition & 3 deletions examples/ex07_ics/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@
int
main(int argc, char * argv[])
{
Moose::main<ExampleApp>(argc, argv);

return 0;
return Moose::main<ExampleApp>(argc, argv);
}
4 changes: 1 addition & 3 deletions examples/ex08_materials/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@
int
main(int argc, char * argv[])
{
Moose::main<ExampleApp>(argc, argv);

return 0;
return Moose::main<ExampleApp>(argc, argv);
}
4 changes: 1 addition & 3 deletions examples/ex09_stateful_materials/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,5 @@
int
main(int argc, char * argv[])
{
Moose::main<ExampleApp>(argc, argv);

return 0;
return Moose::main<ExampleApp>(argc, argv);
}
4 changes: 1 addition & 3 deletions examples/ex10_aux/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@
int
main(int argc, char * argv[])
{
Moose::main<ExampleApp>(argc, argv);

return 0;
return Moose::main<ExampleApp>(argc, argv);
}
4 changes: 1 addition & 3 deletions examples/ex11_prec/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@
int
main(int argc, char * argv[])
{
Moose::main<ExampleApp>(argc, argv);

return 0;
return Moose::main<ExampleApp>(argc, argv);
}
4 changes: 1 addition & 3 deletions examples/ex12_pbp/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@
int
main(int argc, char * argv[])
{
Moose::main<ExampleApp>(argc, argv);

return 0;
return Moose::main<ExampleApp>(argc, argv);
}
4 changes: 1 addition & 3 deletions examples/ex13_functions/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,5 @@
int
main(int argc, char * argv[])
{
Moose::main<ExampleApp>(argc, argv);

return 0;
return Moose::main<ExampleApp>(argc, argv);
}
4 changes: 1 addition & 3 deletions examples/ex14_pps/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,5 @@
int
main(int argc, char * argv[])
{
Moose::main<ExampleApp>(argc, argv);

return 0;
return Moose::main<ExampleApp>(argc, argv);
}
4 changes: 1 addition & 3 deletions examples/ex15_actions/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,5 @@
int
main(int argc, char * argv[])
{
Moose::main<ExampleApp>(argc, argv);

return 0;
return Moose::main<ExampleApp>(argc, argv);
}
4 changes: 1 addition & 3 deletions examples/ex16_timestepper/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,5 @@
int
main(int argc, char * argv[])
{
Moose::main<ExampleApp>(argc, argv);

return 0;
return Moose::main<ExampleApp>(argc, argv);
}
4 changes: 1 addition & 3 deletions examples/ex17_dirac/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,5 @@
int
main(int argc, char * argv[])
{
Moose::main<ExampleApp>(argc, argv);

return 0;
return Moose::main<ExampleApp>(argc, argv);
}
4 changes: 1 addition & 3 deletions examples/ex18_scalar_kernel/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,5 @@
int
main(int argc, char * argv[])
{
Moose::main<ExampleApp>(argc, argv);

return 0;
return Moose::main<ExampleApp>(argc, argv);
}
4 changes: 1 addition & 3 deletions examples/ex19_dampers/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,5 @@
int
main(int argc, char * argv[])
{
Moose::main<ExampleApp>(argc, argv);

return 0;
return Moose::main<ExampleApp>(argc, argv);
}
4 changes: 1 addition & 3 deletions examples/ex20_user_objects/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,5 @@
int
main(int argc, char * argv[])
{
Moose::main<ExampleApp>(argc, argv);

return 0;
return Moose::main<ExampleApp>(argc, argv);
}
4 changes: 1 addition & 3 deletions examples/ex21_debugging/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@
int
main(int argc, char * argv[])
{
Moose::main<ExampleApp>(argc, argv);

return 0;
return Moose::main<ExampleApp>(argc, argv);
}
17 changes: 15 additions & 2 deletions framework/include/base/MooseApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ class MooseApp : public ConsoleStreamInterface,
return name;
}

/**
* Get the shell exit code for the application
* @return The shell exit code
*/
int exitCode() const { return _exit_code; }

/**
* Sets the exit code that the application will exit with.
*/
void setExitCode(const int exit_code) { _exit_code = exit_code; }

/**
* Get the parameters of the object
* @return The parameters of the object
Expand Down Expand Up @@ -1234,6 +1245,8 @@ class MooseApp : public ConsoleStreamInterface,
/// Indicates whether warnings or errors are displayed when overridden parameters are detected
bool _error_overridden;
bool _ready_to_exit;
/// The exit code
int _exit_code;

/// This variable indicates when a request has been made to restart from an Exodus file
bool _initial_from_file;
Expand Down Expand Up @@ -1405,14 +1418,14 @@ class MooseApp : public ConsoleStreamInterface,
* read/writable location for the user.
* @return a Boolean value used to indicate whether the application should exit early
*/
bool copyInputs() const;
bool copyInputs();

/**
* Handles the run input parameter logic: Checks to see whether a directory exists in user space
* and launches the TestHarness to process the given directory.
* @return a Boolean value used to indicate whether the application should exit early
*/
bool runInputs() const;
bool runInputs();

/// General storage for custom RestartableData that can be added to from outside applications
std::unordered_map<RestartableDataMapName, std::pair<RestartableDataMap, std::string>>
Expand Down
4 changes: 3 additions & 1 deletion framework/include/base/MooseMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void addMainCommandLineParams(InputParameters & params);
* Initialize, create and run a MooseApp
*/
template <typename DefaultAppType>
void
int
main(int argc, char * argv[])
{
MooseInit init(argc, argv);
Expand All @@ -45,5 +45,7 @@ main(int argc, char * argv[])
auto app = createMooseApp(default_app_name, argc, argv);

app->run();

return app->exitCode();
}
}
41 changes: 21 additions & 20 deletions framework/src/base/MooseApp.C
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ MooseApp::MooseApp(InputParameters parameters)
_factory(*this),
_error_overridden(false),
_ready_to_exit(false),
_exit_code(0),
_initial_from_file(false),
_distributed_mesh_on_command_line(false),
_recover(false),
Expand Down Expand Up @@ -1605,10 +1606,13 @@ MooseApp::getInstallableInputs() const
}

bool
MooseApp::copyInputs() const
MooseApp::copyInputs()
{
if (isParamValid("copy_inputs"))
{
if (comm().size() > 1)
mooseError("The --copy-inputs option should not be ran in parallel");

// Get command line argument following --copy-inputs on command line
auto dir_to_copy = getParam<std::string>("copy_inputs");

Expand Down Expand Up @@ -1654,28 +1658,29 @@ MooseApp::copyInputs() const

TIME_SECTION("copy_inputs", 2, "Copying Inputs");

// Only perform the copy on the root processor
int return_value = 0;
if (processor_id() == 0)
return_value = system(cmd.c_str());
_communicator.broadcast(return_value);

if (WIFEXITED(return_value) && WEXITSTATUS(return_value) != 0)
mooseError("Failed to copy the requested directory.");
Moose::out << "Directory successfully copied into ./" << dst_dir << '\n';
mooseAssert(comm().size() == 1, "Should be run in serial");
const auto return_value = system(cmd.c_str());
if (!WIFEXITED(return_value))
mooseError("Process exited unexpectedly");
setExitCode(WEXITSTATUS(return_value));
if (exitCode() == 0)
Moose::out << "Directory successfully copied into ./" << dst_dir << '\n';
return true;
}
return false;
}

bool
MooseApp::runInputs() const
MooseApp::runInputs()
{
if (isParamValid("run"))
{
// These options will show as unused by petsc; ignore them all
Moose::PetscSupport::setSinglePetscOption("-options_left", "0");

if (comm().size() > 1)
mooseError("The --run option should not be ran in parallel");

// Here we are going to pass everything after --run on the cli to the TestHarness. That means
// cannot validate these CLIs.
auto it = _command_line->find("run");
Expand Down Expand Up @@ -1711,16 +1716,12 @@ MooseApp::runInputs() const
" --run <dir>\" again.");
}

// Only launch the tests on the root processor
Moose::out << "Working Directory: " << working_dir << "\nRunning Command: " << cmd << std::endl;
int return_value = 0;
if (processor_id() == 0)
return_value = system(cmd.c_str());
_communicator.broadcast(return_value);

// TODO: return the actual return value here
if (WIFEXITED(return_value) && WEXITSTATUS(return_value) != 0)
mooseError("Run failed");
mooseAssert(comm().size() == 1, "Should be run in serial");
const auto return_value = system(cmd.c_str());
if (!WIFEXITED(return_value))
mooseError("Process exited unexpectedly");
setExitCode(WEXITSTATUS(return_value));
return true;
}

Expand Down
4 changes: 1 addition & 3 deletions modules/chemical_reactions/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,5 @@
int
main(int argc, char * argv[])
{
Moose::main<ChemicalReactionsTestApp>(argc, argv);

return 0;
return Moose::main<ChemicalReactionsTestApp>(argc, argv);
}
4 changes: 1 addition & 3 deletions modules/combined/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,5 @@
int
main(int argc, char * argv[])
{
Moose::main<CombinedTestApp>(argc, argv);

return 0;
return Moose::main<CombinedTestApp>(argc, argv);
}
4 changes: 1 addition & 3 deletions modules/contact/src/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,5 @@
int
main(int argc, char * argv[])
{
Moose::main<ContactTestApp>(argc, argv);

return 0;
return Moose::main<ContactTestApp>(argc, argv);
}
26 changes: 26 additions & 0 deletions modules/doc/content/newsletter/2024/2024_09.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,32 @@ now provide interfaces to compute the specific internal energy as a function of
temperature. This new interface is useful, for example, in measuring the
internal energy in solids for energy conservation verification.

### Exit Code Management

The ability was added to allow an application to set its exit code, which can
be seen in `MooseApp::setExitCode()`. This feature was added in order to better
support instances in which you want to run an external process within an application,
as the proper return code from the external process can be captured and
returned via your application.

In order for an application properly return this exit code, a modification is needed
within the `main` function. To do this, modify the `main()` function in `src/main.C`
to look like the following:

```
int
main(int argc, char * argv[])
{
return Moose::main<YourApp>(argc, argv);
}
```

where `YourApp` is the class for your application.

This change should be made as soon as possible as the logic that was changed here
impacts the application command line options `--run` and `--copy-inputs` used for
executing installed MOOSE applications.

## libMesh-level Changes

## PETSc-level Changes
Expand Down
Loading

0 comments on commit bfd4591

Please sign in to comment.