14 #ifndef REDUCINGTASKTTLMANAGER_HPP_
15 #define REDUCINGTASKTTLMANAGER_HPP_
20 #include <Poco/Timestamp.h>
32 void addTaskTTL(
unsigned long long taskId,
unsigned int ttl ) {
38 auto iter = _runningTasks.find( taskId );
40 if( iter == _runningTasks.end() ) {
42 _runningTasks[ taskId ] = std::make_pair( Poco::Timestamp(), ttl );
45 iter->second.second = ttl;
50 auto iter = _runningTasks.find( taskId );
51 if( iter != _runningTasks.end() ) {
54 addTerminatedTask(taskId );
56 _runningTasks.erase( iter );
60 unsigned int getTaskTTL(
unsigned long long taskId )
const {
61 auto iter = _runningTasks.find( taskId );
62 if( iter != _runningTasks.end() ) {
63 return iter->second.second;
70 std::vector< unsigned long long > expiredTasks;
74 for(
int i = 0, n = expiredTasks.size(); i < n; i++ ) {
75 _runningTasks.erase( expiredTasks[i] );
77 addTerminatedTask( expiredTasks[i] );
80 return expiredTasks.size();
84 return _terminatedTasks.find( taskId ) != _terminatedTasks.end();
88 unsigned int i = 0, n = _terminatedTasksTime.size();
90 for( ; i < n && i < maxRemoveItems && _terminatedTasksTime[i].second.isElapsed( minTerminateTime *
ONE_THOUSAND_MICROSECONDS ); i++ );
93 for(
unsigned int j = 0; j < i; j++ ) {
94 _terminatedTasks.erase( _terminatedTasksTime[j].first );
96 _terminatedTasksTime.erase( _terminatedTasksTime.begin(), _terminatedTasksTime.begin() + i );
102 std::map< unsigned long long, std::pair< Poco::Timestamp, unsigned int > >::const_iterator iter = _runningTasks.find( taskId );
104 return (iter == _runningTasks.end() )?
false: iter->second.first.isElapsed( ( ( ttl > 0 )?ttl:iter->second.second ) *
ONE_THOUSAND_MICROSECONDS );
107 void getExceededTTLTasks(
unsigned int maxTaskNumber, std::vector<unsigned long long>& exceededTTLTasks,
unsigned int ttl = 0 )
const {
108 std::map< unsigned long long, std::pair< Poco::Timestamp, unsigned int > >::const_iterator iter;
109 unsigned int taskCount = 0;
111 for( iter = _runningTasks.begin(); iter != _runningTasks.end(); ++iter ) {
113 exceededTTLTasks.push_back( iter->first );
115 if( taskCount >= maxTaskNumber ) {
122 void addTerminatedTask(
unsigned long long taskId ) {
123 _terminatedTasks.insert( taskId );
124 _terminatedTasksTime.push_back( std::make_pair( taskId, Poco::Timestamp() ) );
128 std::map< unsigned long long, std::pair< Poco::Timestamp, unsigned int > > _runningTasks;
129 std::set< unsigned long long > _terminatedTasks;
131 std::vector< std::pair< unsigned long long, Poco::Timestamp > > _terminatedTasksTime;