Skip to content
Closed
22 changes: 21 additions & 1 deletion src/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ namespace proc {
return _apps;
}

void proc_t::set_apps(std::vector<ctx_t> apps) {
_apps = std::move(apps);
}

// Gets application image from application list.
// Returns image from assets directory if found there.
// Returns default image if image configuration is not set.
Expand All @@ -370,6 +374,18 @@ namespace proc {
return _app.name;
}

const boost::process::v1::environment& proc_t::get_env() const {
return _env;
}

boost::process::v1::environment& proc_t::get_env() {
return _env;
}

void proc_t::set_env(boost::process::v1::environment env) {
_env = std::move(env);
}

proc_t::~proc_t() {
// It's not safe to call terminate() here because our proc_t is a static variable
// that may be destroyed after the Boost loggers have been destroyed. Instead,
Expand Down Expand Up @@ -698,7 +714,11 @@ namespace proc {
auto proc_opt = proc::parse(file_name);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I strongly suggest to change signature of std::optional<proc::proc_t> parse(const std::string &file_name) to std::optional<std:tuple<std::vector<ctx_t>, boost::process::v1::environment>> parse(const std::string &file_name) and also remove proc_t constructor as it is another disaster waiting to happen (don't forget to initialize int _app_id{0};)


if (proc_opt) {
proc = std::move(*proc_opt);
// Update the process object with the new environment and apps
// And, keep app running status.

proc.set_env(proc_opt->get_env());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After looking into this I have found another bug related to _env. It is used in terminate function, this means we have to preserve the _env until process terminates :/

The moment we call execute we should copy the _env to a member variable _app_env (name is debatable) and replace every usage of _env within execute and terminate with _app_env (except the first one where we copy it of course). Or we could store the new ENV to _current_env and the in execute copy it (_env = _current_env). I prefer the first renaming option, but whatever is easier is ok.

proc.set_apps(proc_opt->get_apps());
}
}
} // namespace proc
56 changes: 54 additions & 2 deletions src/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,62 @@ namespace proc {

~proc_t();

const std::vector<ctx_t> &get_apps() const;
std::vector<ctx_t> &get_apps();
/**
* @brief Gets the list of applications.
Comment thread
MiroKaku marked this conversation as resolved.
Outdated
* @return A constant reference to the vector of applications.
*/
const std::vector<ctx_t>& get_apps() const;

/**
* @brief Gets the list of applications.
Comment thread
MiroKaku marked this conversation as resolved.
Outdated
* @return A reference to the vector of applications.
*/
std::vector<ctx_t>& get_apps();
Comment thread
MiroKaku marked this conversation as resolved.
Outdated

/**
* @brief Sets the list of applications.
Comment thread
MiroKaku marked this conversation as resolved.
Outdated
* @param apps The new list of applications.
* @note This will overwrite the existing list of applications.
* @see refresh(const std::string &file_name)
Comment thread
MiroKaku marked this conversation as resolved.
*/
void set_apps(std::vector<ctx_t> apps);

/**
* @brief Gets the image path of the application with the given app_id.
Comment thread
MiroKaku marked this conversation as resolved.
Outdated
* @param app_id The ID of the application.
* @return The image path of the application.
*/
std::string get_app_image(int app_id);

/**
* @brief Gets the name of the last run application.
Comment thread
MiroKaku marked this conversation as resolved.
Outdated
* @return The name of the last run application.
*/
std::string get_last_run_app_name();

/**
* @brief Gets the environment variables.
Comment thread
MiroKaku marked this conversation as resolved.
Outdated
* @return A constant reference to the environment variables.
*/
const boost::process::v1::environment& get_env() const;

/**
* @brief Gets the environment variables.
Comment thread
MiroKaku marked this conversation as resolved.
Outdated
* @return A reference to the environment variables.
*/
boost::process::v1::environment& get_env();
Comment thread
MiroKaku marked this conversation as resolved.
Outdated

/**
* @brief Sets the environment variables.
Comment thread
MiroKaku marked this conversation as resolved.
Outdated
* @param env The new environment variables.
* @note This will overwrite the existing environment variables.
* @see refresh(const std::string &file_name)
Comment thread
MiroKaku marked this conversation as resolved.
Outdated
*/
void set_env(boost::process::v1::environment env);

/**
* @brief Terminates the currently running process.
Comment thread
MiroKaku marked this conversation as resolved.
Outdated
*/
void terminate();

private:
Expand Down