1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "job_manager.h"
19 class CFinishedJob
: public CJobManager::IJob
22 bool finished() { return true; }
23 std::string
getShortStatus() { return _ShortStatus
; }
24 std::string
getStatus() { return _Status
; }
25 void display(NLMISC::CLog
* log
=NLMISC::InfoLog
) { log
->displayNL("%s",_Status
.c_str()); }
28 CFinishedJob(CJobManager::IJob
* theFinishedJob
)
30 if (theFinishedJob
==NULL
)
32 _Status
=theFinishedJob
->getStatus();
33 _ShortStatus
=theFinishedJob
->getShortStatus();
38 std::string _ShortStatus
;
42 CJobManager
* CJobManager::getInstance()
44 static CJobManager
* mgr
=NULL
;
52 CJobManager::CJobManager()
55 _JobUpdatesPerUpdate
=1;
58 void CJobManager::serviceUpdate()
63 for (uint32 count
=0;count
<_JobUpdatesPerUpdate
&&!_UnfinishedJobs
.empty();++count
)
65 nlassert(_UnfinishedJobs
.front()<_Jobs
.size());
66 NLMISC::CSmartPtr
<IJob
>& theJob
= _Jobs
[_UnfinishedJobs
.front()];
67 if (theJob
->finished())
69 // delete the job and replace it with a light weight 'finished job' marker
70 theJob
= new CFinishedJob(theJob
);
72 // remove this job from the list of unfinished jobs
73 _UnfinishedJobs
.pop_front();
75 // decrement the updates counter to counteract the auto incrment
86 uint32
CJobManager::addJob(NLMISC::CSmartPtr
<CJobManager::IJob
> job
)
89 uint32 id
= (uint32
)_Jobs
.size();
90 _UnfinishedJobs
.push_back(id
);
95 void CJobManager::promoteJob(uint32 idx
)
97 TUnfinishedJobs::iterator it
;
98 for (it
=_UnfinishedJobs
.begin(); it
!=_UnfinishedJobs
.end(); ++it
)
102 _UnfinishedJobs
.erase(it
);
103 _UnfinishedJobs
.push_front(idx
);
107 nlwarning("Failed to promote job with ID %d as not found in unfinished jobs list",idx
);
110 void CJobManager::pause()
115 void CJobManager::resume()
120 void CJobManager::setJobUpdatesPerUpdate(uint32 count
)
122 _JobUpdatesPerUpdate
= count
;
123 if (count
==0 || count
>100)
124 nlwarning("Suspicious value of JobUpdatesPerUpdate: %d",count
);
127 uint32
CJobManager::getJobUpdatesPerUpdate()
129 return _JobUpdatesPerUpdate
;
132 std::string
CJobManager::getStatus()
136 if (_Paused
) result
+="[Paused] ";
138 if (!_UnfinishedJobs
.empty())
140 uint32 idx
=_UnfinishedJobs
.front();
141 nlassert(idx
<_Jobs
.size());
142 result
+=_Jobs
[idx
]->getStatus();
145 result
+=NLMISC::toString(" [Updates per cycle: %d]",_JobUpdatesPerUpdate
);
150 void CJobManager::listJobs(NLMISC::CLog
* log
)
152 for (uint32 i
=0;i
< _Jobs
.size(); ++i
)
154 if (!_Jobs
[i
]->finished())
155 nlinfo("%4d*: %s",i
,_Jobs
[i
]->getStatus().c_str());
157 nlinfo("%d unfinished jobs (%d in total)",_UnfinishedJobs
.size(),_Jobs
.size());
160 void CJobManager::listJobHistory(NLMISC::CLog
* log
)
162 for (uint32 i
=0;i
< _Jobs
.size(); ++i
)
164 nlinfo("%4d%c: %s",i
,_Jobs
[i
]->finished()? ' ': '*',_Jobs
[i
]->getStatus().c_str());
166 nlinfo("%d unfinished jobs (%d in total)",_UnfinishedJobs
.size(),_Jobs
.size());
169 void CJobManager::displayCurrentJob(NLMISC::CLog
* log
)
171 if (!_UnfinishedJobs
.empty())
172 displayJob(_UnfinishedJobs
.front(),log
);
175 void CJobManager::displayJob(uint32 jobId
,NLMISC::CLog
* log
)
177 nlassert(jobId
<_Jobs
.size());
178 _Jobs
[jobId
]->display(log
);