void Job::waitForEvent(DWORD timeout) { gatherInfo(); testRestrictions(); DWORD evnt; ULONG key; LPOVERLAPPED lpOverlapped; BOOL reslt = GetQueuedCompletionStatus(port.handle(),&evnt,&key, &lpOverlapped,timeout); if (reslt == 0) { DWORD err = GetLastError(); tryApi(_T("GetQueuedCompletionStatus"),err == WAIT_TIMEOUT); } else { if (evnt == JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO) jobactive = false; else if (evnt == JOB_OBJECT_MSG_END_OF_JOB_TIME || evnt == JOB_OBJECT_MSG_END_OF_PROCESS_TIME) res = JobResult(JobResult::TL,_T("")); else if (evnt == JOB_OBJECT_MSG_ACTIVE_PROCESS_LIMIT) res = JobResult(JobResult::SV, Messages::RESULT_CHILD_PROCESS); else if (evnt == JOB_OBJECT_MSG_PROCESS_MEMORY_LIMIT || evnt == JOB_OBJECT_MSG_JOB_MEMORY_LIMIT) res = JobResult(JobResult::ML,_T("")); } if (res.valid()) { tryApi(_T("TerminateJobObject"), TerminateJobObject(hJob,0) != 0); } else if (!active()) { DWORD code; tryApi(_T("GetExitCodeProcess"), GetExitCodeProcess(hMainProcess,&code) != 0); res = JobResult(JobResult::OK, _T("")); } }
void Job::testRestrictions() { LONG jobTime = processTimeCounter.getLongValue(); LONGLONG elapsedTime = elapsedTimeCounter.getLargeValue(); if (timeLimit > 0 && jobTime*10000ULL > timeLimit) { res = JobResult(JobResult::TL,_T("")); } if (prevJobTime > 0) { LONG usedSinceLast = jobTime - prevJobTime; LONGLONG elapsedSinceLast = elapsedTime - prevElapsedTime; if (elapsedSinceLast > 0 && static_cast<double>(usedSinceLast)/elapsedSinceLast < cpuUsageIdle) { millisecondsIdle += elapsedSinceLast; } } if (idlenessLimit > 0 && millisecondsIdle*10000ULL > idlenessLimit) { res = JobResult(JobResult::IS,_T("")); } prevJobTime = jobTime; prevElapsedTime = elapsedTime; }
void Job::assignMainProcess(const Process &process) { hMainProcess = process.processHandle(); tryApi(_T("AssignProcessToJobObject"), AssignProcessToJobObject(hJob,hMainProcess) != 0); gatherInfo(); initialElapsedTimeCounter = elapsedTimeCounter.getLargeValue(); millisecondsIdle = 0; prevJobTime = -1; prevElapsedTime = -1; res = JobResult(); jobactive = true; }
void Job::terminate() { res = JobResult(JobResult::ER,Messages::RESULT_TERMINATED); }
JobResult JobResult::error( const QString& message, const QString& details ) { return JobResult( false, message, details ); }
JobResult JobResult::ok() { return JobResult( true, QString(), QString() ); }
JobResult JobResult::internalError( const QString& message, const QString& details, int number ) { return JobResult( message, details, number ? number : GenericError ); }
JobResult JobResult::error( const QString& message, const QString& details ) { return JobResult( message, details, GenericError ); }
JobResult JobResult::ok() { return JobResult( QString(), QString(), NoError ); }