hpcpack-linux-agent/LinuxNodeMgr/Executor.cpp

232 строки
6.2 KiB
C++
Исходник Постоянная ссылка Обычный вид История

2014-10-16 12:44:12 +04:00
#include "Executor.h"
void Callback(std::string callbackUri, web::json::value callbackBody)
{
web::http::client::http_client client(U(callbackUri));
web::http::http_request request;
request.set_method(http::methods::POST);
2014-10-16 12:44:12 +04:00
std::string jsonBody = callbackBody.serialize();
2014-10-16 12:44:12 +04:00
std::cout << "Callback to " << callbackUri << " with " << jsonBody << std::endl;
2014-10-16 12:44:12 +04:00
request.set_body(jsonBody, U("application/json"));
client.request(request).get();
2014-10-16 12:44:12 +04:00
}
/// arg: the pointer of the input.
void* RunThread(void* arg)
{
ComputeClusterTaskInformation* taskInfo = (ComputeClusterTaskInformation*)arg;
taskInfo->Message = U("");
taskInfo->ProcessIds = U("");
ProcessStartInfo* startInfo = taskInfo->StartInfo;
int pipeForStdOut[2], pipeForStdErr[2];
if (-1 == pipe(pipeForStdOut))
{
taskInfo->Message += U("Error:");
std::cout << "Error Pipe >>>>>>>>>>>>>>>>>>>>" << errno << std::endl;
taskInfo->Message += errno;
taskInfo->Message += U("\n");
}
if (-1 == pipe(pipeForStdErr))
{
taskInfo->Message += U("Error:");
std::cout << "Error pipe >>>>>>>>>>>>>>>>>>>" << errno << std::endl;
taskInfo->Message += errno;
taskInfo->Message += U("\n");
}
// Fork the task as child process.
std::cout << "before fork" << std::endl;
2014-10-28 09:31:02 +03:00
std::string scrpitPath = startInfo->GetCommandScript();
pid_t childPid = fork();
if (childPid < 0)
{
std::cout << "failed to fork " << childPid << std::endl;
taskInfo->Message += "Failed to fork because ";
if (errno == EAGAIN){
taskInfo->Message += "number of processes has reached the upper limit.";
}
else{ // ENOMEM
taskInfo->Message += "no enough memories.";
}
taskInfo->ExitCode = -1;
taskInfo->Exited = true;
taskInfo->NumberOfProcesses = 0;
// taskInfo->ProcessIds += (int)childPid;
taskInfo->TaskRequeueCount = 0;
}
else if (childPid == 0)
{
// run command
2014-10-28 09:31:02 +03:00
//std::string command = startInfo->GetCommand();
//std::cout << "command : " << command << std::endl;
char * argarray[3] = { (char*)"/bin/bash", (char*)scrpitPath.c_str(), NULL };
//std::vector< std::string > args;
//startInfo->GetCommandArgs(&args);
//char** argarray = NULL;
//int argsize = (int)args.size();
//if (argsize > 0) {
// argarray = new char*[argsize + 1];
// size_t ix = 0;
// for (std::vector<std::string>::iterator i = args.begin(); i != args.end(); i++, ix++){
// argarray[ix] = (char*)i->c_str();
// }
// argarray[ix] = NULL;
//}
2014-10-23 05:37:04 +04:00
//for (int i = 0; i < argsize + 1; i++){
// if (argarray[i] == NULL){
// std::cout << "arg[" << i << "]: " << "NULL" << std::endl;
// }
// else{
// std::cout << "arg[" << i << "]: " << argarray[i] << std::endl;
// }
//}
std::vector< std::string > envs = startInfo->environmentVariables;
//startInfo->GetCommandEnvs(&envs);
char** envarray = NULL;
int envsize = (int)envs.size();
if (envsize > 0) {
envarray = new char*[envsize + 1];
size_t ix = 0;
for (std::vector<std::string>::iterator i = envs.begin(); i != envs.end(); ++i, ++ix){
envarray[ix] = (char*)i->c_str();
2014-10-23 05:37:04 +04:00
//std::cout << "env[" << ix << "]: " << envarray[ix] << std::endl;
}
envarray[ix] = NULL;
}
// In child process, redirect the stdout and stderr to pipes.
std::cout << "before exec" << std::endl;
dup2(pipeForStdOut[1], 1);
close(pipeForStdOut[0]);
close(pipeForStdOut[1]);
dup2(pipeForStdErr[1], 2);
close(pipeForStdErr[0]);
close(pipeForStdErr[1]);
2014-10-28 09:31:02 +03:00
//int sts = execvpe(command.c_str(), argarray, envarray);
int sts = execvpe("/bin/bash", argarray, envarray);
std::cout << "after exec " << sts << std::endl;
2014-10-28 09:31:02 +03:00
//delete[] argarray;
delete[] envarray;
2014-10-28 09:31:02 +03:00
startInfo->DeleteCommandScript();
//execle(startInfo->commandLine.c_str(), startInfo->commandLine.c_str(), NULL);
exit(0);
}
else
{
std::cout << "after fork " << childPid << std::endl;
startInfo->processId = childPid;
/// Wait the end of child process.
int status;
2014-10-28 09:31:02 +03:00
char buf[32] = { 0 };
ssize_t bytesRead;
wait3(&status, 0, &startInfo->Usage);
if (WIFEXITED(status)){
startInfo->exitCode = WEXITSTATUS(status);
}
else{
startInfo->exitCode = -1;
}
close(pipeForStdOut[1]);
close(pipeForStdErr[1]);
/// Read data from pipe for stdout.
while ((bytesRead = read(pipeForStdOut[0], buf, sizeof(buf) - 1)) > 0)
{
buf[bytesRead] = 0;
startInfo->stdOutText += buf;
}
/// Read data from pipe for stderr.
while ((bytesRead = read(pipeForStdErr[0], buf, sizeof(buf) - 1)) > 0)
{
buf[bytesRead] = 0;
startInfo->stdErrText += buf;
}
2014-10-28 09:31:02 +03:00
close(pipeForStdOut[0]);
close(pipeForStdErr[0]);
2014-10-28 09:31:02 +03:00
startInfo->DeleteCommandScript();
taskInfo->Message += startInfo->stdOutText + startInfo->stdErrText;
taskInfo->ExitCode = startInfo->exitCode;
taskInfo->Exited = true;
taskInfo->NumberOfProcesses = 1;
// taskInfo->ProcessIds += (int)childPid;
taskInfo->TaskRequeueCount = 0;
}
try
{
Callback(taskInfo->CallbackUri, taskInfo->GetEventArgJson());
delete taskInfo;
JobTaskDb::GetInstance().RemoveTask(taskInfo->JobId, taskInfo->TaskId);
}
catch (const web::http::http_exception& ex)
{
std::cout << "Http Exception Occurred" << ex.what() << std::endl;
}
catch (...)
{
std::cout << "Exception occurred" << std::endl;
}
pthread_exit(0);
2014-10-16 12:44:12 +04:00
}
Executor::Executor()
{
//ctor
2014-10-16 12:44:12 +04:00
}
Executor::~Executor()
{
//dtor
2014-10-16 12:44:12 +04:00
}
ComputeClusterTaskInformation* Executor::StartTask(int jobId, int taskId, ProcessStartInfo* startInfo, const std::string& callbackUri)
{
ComputeClusterTaskInformation* taskInfo = new ComputeClusterTaskInformation(jobId, taskId, startInfo, callbackUri);
2014-10-16 12:44:12 +04:00
// std::cout << "Before set env" << std::endl;
//
// ///Set Environment Variable
// for (std::map<std::string, std::string>::iterator it = processStartInfo->environmentVariables.begin(); it != processStartInfo->environmentVariables.end(); it++)
// {
// std::string command = it->first + "=" + it->second;
// putenv((char*)command.c_str());
// }
2014-10-16 12:44:12 +04:00
// std::cout << "After set env" << std::endl;
2014-10-16 12:44:12 +04:00
// Create the thread for task.
pthread_t *threadId = new pthread_t();
taskInfo->StartInfo->threadId = threadId;
pthread_create(threadId, NULL, RunThread, (void*)taskInfo);
2014-10-16 12:44:12 +04:00
std::cout << "created thread." << std::endl;
2014-10-16 12:44:12 +04:00
return taskInfo;
2014-10-16 12:44:12 +04:00
}
void Executor::EndTask(ComputeClusterTaskInformation* taskInfo)
{
kill(taskInfo->StartInfo->processId, SIGQUIT);
2014-10-16 12:44:12 +04:00
}