bool DBManager::writeFinishedTask() { TaskPtr pTask; finishedTaskMgr_->lock(); if(finishedTaskMgr_->hasTask()) { pTask = finishedTaskMgr_->popTask(); } finishedTaskMgr_->unlock(); if(!pTask) { return true; } if(pTask->input().ProblemID == 0)//IDE测试功能,不写数据库 { return true; } if(!writeToDB(pTask)) { return false; } OJCout<<GetOJString("write finished:")<<pTask->input().SolutionID<<std::endl; return true; }
std::vector<TaskPtr> ThreadGroup::remove(TaskGenerator* generator) { std::vector<TaskPtr> remaining_tasks; std::unique_lock<std::recursive_mutex> lock(tasks_mtx_); TaskGeneratorPtr removed; for (auto it = tasks_.begin(); it != tasks_.end();) { TaskPtr task = *it; if (task->getParent() == generator) { remaining_tasks.push_back(task); it = tasks_.erase(it); } else { ++it; } } for (auto it = generators_.begin(); it != generators_.end();) { if (it->get() == generator) { removed = *it; it = generators_.erase(it); } else { ++it; } } work_available_.notify_all(); generator_connections_[generator].clear(); generator_removed(removed); return remaining_tasks; }
void Peer::HandleResponses(WMessagePtr pWMsg) { MessagePtr pMsg = pWMsg->GetMsg(); // get the peer list if given in message std::vector<RemotePeers> peerList = pMsg->m_peerList; if( peerList.size() > 0 ) { long ts = pMsg->m_timestamp; this->HandlePeerList(peerList, ts); } // see if it is a task message if( pMsg->IsTaskMessage() ) { int msgTaskId = pMsg->m_taskId; // get the task and forward the message std::map<int, TaskPtr>::iterator it; it = m_taskList.find(msgTaskId); if( it != m_taskList.end() ) { TaskPtr pTask = (*it).second; pTask->GetMessageQueue()->PutMessage(pWMsg); } else { Log(CONSOLE, L"No Task Related to Task Response Message Received for taskId: %d\n", msgTaskId); } return; } switch(pMsg->m_respType) { case GET_ONLINE_RESP: HandleOnlineResp(pMsg); break; default: break; } }
void ThreadGroup::executeTask(const TaskPtr& task) { try { std::unique_lock<std::recursive_mutex> state_lock(execution_mtx_); ProfilerPtr profiler = getProfiler(); Trace::Ptr interlude; if (profiler && profiler->isEnabled()) { TimerPtr timer = profiler->getTimer(getName()); interlude.reset(new Trace(timer, task->getName())); } task->execute(); } catch (const std::exception& e) { TaskGenerator* gen = task->getParent(); if (gen) { gen->setError(e.what()); } } catch (const std::string& s) { std::cerr << "Uncaught exception (string) exception: " << s << std::endl; } catch (const csapex::Failure& assertion) { handler_.handleAssertionFailure(assertion); } catch (...) { std::cerr << "Uncaught exception of unknown type and origin in execution of task " << task->getName() << "!" << std::endl; throw; } }
void* SchedulerProcessor() { bool flag = true; while(true) { vector<string> machines = MachinePoolI::Instance()->GetAllMachineEndpoint(); if(machines.size() == 0) { //没有机器,不能一直查询 //LOG4CPLUS_INFO(logger, "no machine right now..."); sleep(1); } else { if(flag) { LOG4CPLUS_INFO(logger, "begin to generate request"); //有机器再启动线请求程,只能启动一次 thread request_generator_t(RequestGenerator); //这个线程主要修改rate RequestI::Instance()->Start(); flag = false; } for(vector<string>::iterator it = machines.begin(); it != machines.end(); it++) { TaskPtr task; TaskBufferI::Instance()->PopFront(&task); try { Proxy<WorkerClient> proxy = Rpc<WorkerClient, WorkerClient>::GetProxy(*it); proxy().StartTask(task->GetTaskInfo()); } catch (TException &tx) { LOG4CPLUS_WARN(logger, "machine " << *it << " is down"); MachinePoolI::Instance()->Delete(*it); } } } } return NULL; }
void GL2TextureManager::makeLoadFullResource(ResourceMeta* meta, GL2Texture* texture) { texture->m_loaded = GL2Texture::TextureLoadState::LOADING; ResourceLoader::RawResourceTask rawDataTask = loadRawData(loader, meta); TaskPtr textureLoader = std::make_shared<RawTextureLoader>(this, texture, rawDataTask); textureLoader->addDependency(rawDataTask); loader->getEngine()->getTaskManager()->addTask(textureLoader); }
void VC::RemoveTask(const TaskPtr& task) { //里面有并发控制机制 TaskState ts = task->GetTaskState(); assert(ts == TASK_WAIT || ts == TASK_RUN); if(ts == TASK_WAIT) { m_wait_queue.Erase(task->GetId()); } else { m_run_queue.Erase(task->GetId()); } }
void ThreadPool::PoolWorker() { TaskPtr task; while (running_) { task_event_.wait(); while (running_ && task_queue_.try_pop(task)) { task->Run(); } } }
bool TaskViewImpl::updateTask(Task* task) { bool updated = false; int index = taskCombo.findText(task->name().c_str()); if(index < 0 || index >= static_cast<int>(tasks.size())){ addTask(task); } else { if(isWaiting()){ mv->putln(MessageView::WARNING, format(_("Task \"%1%\" cannot be updated now because it is wating for a command to finish.")) % task->name()); } else { TaskInfo& info = tasks[index]; TaskPtr oldTask = info.task; info.task = task; bool doEmitSignals = task != oldTask; if(index != currentTaskIndex){ if(doEmitSignals){ sigTaskRemoved(oldTask); sigTaskAdded(task); } } else { info.state = new Mapping(); if(isExecutionEnabled()){ oldTask->storeState(self, *info.state); } if(doEmitSignals){ sigTaskRemoved(oldTask); } setCurrentTask(index, true); if(doEmitSignals){ sigTaskAdded(task); } if(isExecutionEnabled()){ task->restoreState(self, *info.state); } } os << format(_("Task \"%1%\" has been updated with the new one.")) % task->name() << endl; updated = true; } } return updated; }
static void _exec(executor* exec) { while (true) { TaskPtr task = exec->pull(); if (task) { task->run(); continue; } if (!exec->_running) { std::cout << "Finished running all task " << exec << std::endl; return; } }; }
SingleTaskMenu::SingleTaskMenu(const QString& tablename, CamcopsApp& app) : MenuWindow(app, ""), // start with a blank title m_tablename(tablename), m_current_task(nullptr) { // Title TaskFactoryPtr factory = app.factory(); TaskPtr specimen = factory->create(m_tablename); m_title = specimen->menutitle(); m_anonymous = specimen->isAnonymous(); // m_items is EXPENSIVE (and depends on security), so leave it to build() }
// handle the connection failures void Peer::HandleConnectionFailure(WMessagePtr pMsg) { // see to which peer-node the connection failed // see if there is any task been alloted to that peer // see if there is any job been executed for that peer int failedConnPeer = pMsg->GetPeerId(); // get the task and forward the failure message std::map<int, TaskPtr>::iterator it = m_taskList.begin(); for( ;it != m_taskList.end(); it++ ) { TaskPtr pTask = (*it).second; pTask->GetMessageQueue()->PutMessage(pMsg); } }
int ThreadPool::threadRun(){ LOG_INFO("ThreadPool " << name() << " workThread start"); while (_started) { TaskPtr t = getNextTask(); int ret = t->execute(); if (ret < 0) { LOG_ERROR("ThreadPool " << name() << "task execution error"); } } LOG_INFO("ThreadPool stoped"); return 0; }
void Server::HandleTasks() { //logs::Debug("Handling listener tasks.."); TaskPtr task; while (true) { { std::lock_guard<std::mutex> lock(queueMutex); if (queue.empty()) break; task = queue.front(); queue.pop(); } task->Execute(*this); } }
/// choose a task and assign it to cellet void* ScheduleProcessor(void* unused) { while (true) { // pick a task from the framework pool TaskPtr task = ComponentsMgr::Instance()->GetPool()->GetTask(); // has wait task then begin to assign the task if (task.get()) { // task asssign success change task state to started if (task->AssignTask()) task->TaskAssigned(); else ComponentsMgr::Instance()->GetPool()->AddTask(task); } usleep(1000*50); } return NULL; }
void SingleTaskMenu::build() { TaskFactoryPtr factory = m_app.factory(); TaskPtr specimen = factory->create(m_tablename); // Common items QString info_icon_filename = UiFunc::iconFilename(UiConst::ICON_INFO); m_items = { MenuItem(tr("Options")).setLabelOnly(), MAKE_CHANGE_PATIENT(app), MenuItem( tr("Task information"), HtmlMenuItem( m_title, FileFunc::taskHtmlFilename(specimen->infoFilenameStem()), info_icon_filename), info_icon_filename ), MenuItem(tr("Task instances") + ": " + m_title).setLabelOnly(), }; // Task items TaskPtrList tasklist = factory->fetch(m_tablename); qDebug() << Q_FUNC_INFO << "-" << tasklist.size() << "tasks"; for (auto task : tasklist) { m_items.append(MenuItem(task, false)); } // Call parent buildMenu() MenuWindow::build(); // Signals connect(&m_app, &CamcopsApp::selectedPatientChanged, this, &SingleTaskMenu::selectedPatientChanged, Qt::UniqueConnection); connect(&m_app, &CamcopsApp::taskAlterationFinished, this, &SingleTaskMenu::taskFinished, Qt::UniqueConnection); connect(this, &SingleTaskMenu::offerAdd, m_p_header, &MenuHeader::offerAdd, Qt::UniqueConnection); connect(m_p_header, &MenuHeader::addClicked, this, &SingleTaskMenu::addTask, Qt::UniqueConnection); emit offerAdd(m_anonymous || m_app.patientSelected()); }
//根据状态插入到相应队列中 void VC::PushTask(const TaskPtr& task) { TaskState ts = task->GetTaskState(); assert(ts == TASK_WAIT || ts == TASK_RUN); if(ts == TASK_WAIT) m_wait_queue.PushBack(task); else m_run_queue.PushBack(task); }
// release m_used_cpu and m_used_memory of group bool GroupPool::ReleaseUsedResource(const TaskPtr& task_ptr) { GroupPtr group_ptr = GetGroupPtr(task_ptr->GetGroupName()); if (!group_ptr) { LOG4CPLUS_ERROR(logger, "No this group, group name: " << task_ptr->GetGroupName()); return false; } double factor = 1.0; int32_t priority = task_ptr->GetRawPriority(); if (JOB_RAW_PRIO_PRIVILEGE == priority) { factor = m_privilege_factor; } group_ptr->SetUsedCPU(task_ptr->GetNeedCpu() * factor, true); group_ptr->SetUsedMemory(task_ptr->GetNeedMemory() * factor, true); return true; }
void SingleTaskMenu::addTask() { // The task we create here needs to stay in scope for the duration of the // editing! The simplest way is to use a member object to hold the pointer. TaskFactoryPtr factory = m_app.factory(); TaskPtr task = factory->create(m_tablename); if (!task->isAnonymous()) { int patient_id = m_app.currentPatientId(); if (patient_id == DbConst::NONEXISTENT_PK) { qCritical() << Q_FUNC_INFO << "- no patient selected"; return; } task->setPatient(m_app.currentPatientId()); } task->save(); OpenableWidget* widget = task->editor(); m_app.open(widget, task, true); }
void Peer::HandleTaskResult(WMessagePtr pWMsg) { MessagePtr pMsg = pWMsg->GetMsg(); Log(CONSOLE, L"Handling Task Result from Peer: %d, taskid: %d\n", pMsg->m_fromPeerId, pMsg->m_taskId); PeerNodePtr pNode = pMsg->m_conn->GetPeerNode(); // get the task and forward the message std::map<int, TaskPtr>::iterator it; it = m_taskList.find(pMsg->m_taskId); if( it != m_taskList.end() ) { TaskPtr pTask = (*it).second; pTask->GetMessageQueue()->PutMessage(pWMsg); } else { Log(CONSOLE, L"No Task Related to Task Result Message Received for taskId: %d\n", pMsg->m_taskId); } }
void TaskQueue::onTaskProcessed() { do { TaskPtr task; { QMutexLocker lock(&_tasksToFinishMutex); if (_tasksToFinish.isEmpty()) break; task = _tasksToFinish.front(); _tasksToFinish.pop_front(); } task->finish(); } while (true); if (_stopTimer) { QMutexLocker lock(&_tasksToProcessMutex); if (_tasksToProcess.isEmpty()) { _stopTimer->start(); } } }
MenuItem::MenuItem(const TaskMenuItem& taskmenuitem, CamcopsApp& app) // m_title: below { setDefaults(); m_task_tablename = taskmenuitem.tablename; TaskPtr task = app.factory()->create(m_task_tablename); if (task == nullptr) { m_title = tr("UNKNOWN TASK") + ": " + taskmenuitem.tablename; m_implemented = false; return; } m_title = task->menutitle(); m_subtitle = task->menusubtitle(); m_crippled = task->isCrippled(); if (task->isAnonymous()) { m_icon = UiFunc::iconFilename(UiConst::ICON_ANONYMOUS); } }
TaskId TaskQueue::addTask(TaskPtr task) { { QMutexLocker lock(&_tasksToProcessMutex); _tasksToProcess.push_back(task); } wakeThread(); return task->id(); }
void ThreadGroup::schedule(TaskPtr task) { apex_assert_hard(!destroyed_); std::unique_lock<std::recursive_mutex> tasks_lock(tasks_mtx_); if (!tasks_.empty()) { // for(const TaskPtr& t : tasks_) { for (auto it = tasks_.begin(); it != tasks_.end(); ++it) { const TaskPtr& t = *it; if (t.get() == task.get()) { return; } } } tasks_.insert(task); task->setScheduled(true); work_available_.notify_all(); }
void TaskQueueWorker::onTaskAdded() { if (_inTaskAdded) return; _inTaskAdded = true; bool someTasksLeft = false; do { TaskPtr task; { QMutexLocker lock(&_queue->_tasksToProcessMutex); if (!_queue->_tasksToProcess.isEmpty()) { task = _queue->_tasksToProcess.front(); } } if (task) { task->process(); bool emitTaskProcessed = false; { QMutexLocker lockToProcess(&_queue->_tasksToProcessMutex); if (!_queue->_tasksToProcess.isEmpty() && _queue->_tasksToProcess.front() == task) { _queue->_tasksToProcess.pop_front(); someTasksLeft = !_queue->_tasksToProcess.isEmpty(); QMutexLocker lockToFinish(&_queue->_tasksToFinishMutex); emitTaskProcessed = _queue->_tasksToFinish.isEmpty(); _queue->_tasksToFinish.push_back(task); } } if (emitTaskProcessed) { emit taskProcessed(); } } QCoreApplication::processEvents(); } while (someTasksLeft && !thread()->isInterruptionRequested()); _inTaskAdded = false; }
//---------------------------------------------------------------- // TaskRunner::TPrivate::threadLoop // void TaskRunner::TPrivate::threadLoop() { std::list<TaskPtr> next; Task::TimePoint next_time_point; while (true) { // grab the next set of tasks: { boost::unique_lock<boost::mutex> lock(mutex_); while (tasks_.empty()) { // sleep until there is at least one task in the queue: cond_.wait(lock); boost::this_thread::interruption_point(); } ready(tasks_, next, next_time_point); } while (!next.empty()) { boost::this_thread::interruption_point(); TaskPtr task = next.front(); next.pop_front(); task->run(); } Task::TimePoint now = boost::chrono::steady_clock::now(); if (now < next_time_point) { boost::chrono::nanoseconds ns = next_time_point - now; boost::this_thread::sleep_for(ns); } } }
bool ThreadGroup::executeNextTask() { std::unique_lock<std::recursive_mutex> tasks_lock(tasks_mtx_); if (!tasks_.empty()) { TaskPtr task = *tasks_.begin(); tasks_.erase(tasks_.begin()); task->setScheduled(false); tasks_lock.unlock(); { std::unique_lock<std::recursive_mutex> state_lock(state_mtx_); if (running_) { state_lock.unlock(); executeTask(task); return true; } } } return false; }
void TaskManager::addTask(TaskPtr task) { std::list<TaskPtr>::iterator it = tasks_.begin(); // attempt to find task std::list<TaskPtr>::iterator found = std::find_if(tasks_.begin(), tasks_.end(), std::bind2nd(TaskNameEq(), task->getName()) ); // if found task (meaning task w/ same name exists) throw exception if(found != tasks_.end()) { throw PreconditionException("Attempted to add duplicate task \"" + task->getName() + "\"."); } task->onStart(); // called whenever a task is being started // find the first task in the list with a lower priority while(it != tasks_.end() && (*it)->getPriority() <= task->getPriority()) { ++it; } tasks_.insert(it, task); // insert task after iterator }
int32_t DefaultScheduler::ScheduleOneTask(const TaskPtr& task_ptr) { // set task_ad & hard constraints ClassAdPtr taskad_hard_constraint(new ClassAd(*task_ptr->GetTaskHardClassAd())); string taskad_hard = ClassAdComplement::AdTostring(taskad_hard_constraint); // set soft constraints vector<string> soft_list; list<ClassAdPtr> classad_soft_list = task_ptr->GetTaskSoftList(); for (list<ClassAdPtr>::iterator it = classad_soft_list.begin(); it != classad_soft_list.end(); ++it) { string soft_constraint = ClassAdComplement::AdTostring(*it); soft_list.push_back(soft_constraint); } // match string result; if (SendRequestConstraint(taskad_hard, soft_list, result) != 0) { // TODO // continue; return -1; } if ("" == result) { // TODO return -1; } int32_t task_id = task_ptr->GetTaskId(); int32_t job_id = task_ptr->GetJobId(); JobPtr job_ptr = JobPoolI::Instance()->GetJobPtr(job_id); printf("Match result:\n job_id: %d, task_id: %d, result: %s\n", job_id, task_id, result.c_str()); // new action event EventPtr event(new StartTaskEvent(job_id, task_id, result, taskad_hard)); // Push event into Queue EventDispatcherI::Instance()->Dispatch(event->GetType())->PushBack(event); // write map & task vector<string> vec_ip_port; StringUtility::Split(result.c_str(), vec_ip_port, ":"); if (vec_ip_port.size() != 2) { LOG4CPLUS_ERROR(logger, "result is illegal" << result); return -1; } job_ptr->WriteMap(task_id, vec_ip_port[0]); task_ptr->SetEsIp(vec_ip_port[0]); // add used resource GroupPoolI::Instance()->AddUsedResource(task_ptr); task_ptr->TaskStarting(); return 0; }
TaskId TaskQueue::addTask(TaskPtr task) { { QMutexLocker lock(&_tasksToProcessMutex); _tasksToProcess.push_back(task); } if (!_thread) { _thread = new QThread(); _worker = new TaskQueueWorker(this); _worker->moveToThread(_thread); connect(this, SIGNAL(taskAdded()), _worker, SLOT(onTaskAdded())); connect(_worker, SIGNAL(taskProcessed()), this, SLOT(onTaskProcessed())); _thread->start(); } if (_stopTimer) _stopTimer->stop(); emit taskAdded(); return task->id(); }