mirror of
https://github.com/mongodb/mongo.git
synced 2024-12-01 09:32:32 +01:00
made PeriodicTask more explicity
This commit is contained in:
parent
fc2f62330f
commit
ec722585a9
@ -109,7 +109,7 @@ namespace mongo {
|
||||
return false;
|
||||
}
|
||||
|
||||
void WriteBackManager::Cleaner::doWork() {
|
||||
void WriteBackManager::Cleaner::taskDoWork() {
|
||||
for ( int i=0; i<1000; i++ ) {
|
||||
if ( ! writeBackManager.cleanupOldQueues() )
|
||||
break;
|
||||
|
@ -93,8 +93,8 @@ namespace mongo {
|
||||
|
||||
class Cleaner : public PeriodicTask {
|
||||
public:
|
||||
virtual string name() const { return "WriteBackManager::cleaner"; }
|
||||
virtual void doWork();
|
||||
virtual string taskName() const { return "WriteBackManager::cleaner"; }
|
||||
virtual void taskDoWork();
|
||||
};
|
||||
|
||||
Cleaner _cleaner;
|
||||
|
@ -18,6 +18,7 @@
|
||||
#include "pch.h"
|
||||
|
||||
#include "concurrency/mutex.h"
|
||||
#include "concurrency/spin_lock.h"
|
||||
|
||||
#include "background.h"
|
||||
#include "time_support.h"
|
||||
@ -125,38 +126,61 @@ namespace mongo {
|
||||
PeriodicTask::PeriodicTask() {
|
||||
if ( ! theRunner )
|
||||
theRunner = new Runner();
|
||||
theRunner->_tasks.push_back( this );
|
||||
theRunner->add( this );
|
||||
}
|
||||
|
||||
PeriodicTask::~PeriodicTask() {
|
||||
theRunner->remove( this );
|
||||
}
|
||||
|
||||
void PeriodicTask::Runner::add( PeriodicTask* task ) {
|
||||
scoped_spinlock lk( _lock );
|
||||
_tasks.push_back( task );
|
||||
}
|
||||
|
||||
void PeriodicTask::Runner::remove( PeriodicTask* task ) {
|
||||
scoped_spinlock lk( _lock );
|
||||
for ( size_t i=0; i<_tasks.size(); i++ ) {
|
||||
if ( _tasks[i] == task ) {
|
||||
_tasks[i] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PeriodicTask::Runner::run() {
|
||||
int sleeptime = 60;
|
||||
DEV sleeptime = 5; // to catch race conditions
|
||||
|
||||
while ( ! inShutdown() ) {
|
||||
|
||||
sleepsecs( 60 );
|
||||
sleepsecs( sleeptime );
|
||||
|
||||
scoped_spinlock lk( _lock );
|
||||
|
||||
size_t size = _tasks.size();
|
||||
|
||||
for ( size_t i=0; i<size; i++ ) {
|
||||
PeriodicTask * t = _tasks[i];
|
||||
if ( ! t )
|
||||
continue;
|
||||
|
||||
if ( inShutdown() )
|
||||
break;
|
||||
|
||||
PeriodicTask * t = _tasks[i];
|
||||
|
||||
|
||||
Timer timer;
|
||||
try {
|
||||
t->doWork();
|
||||
t->taskDoWork();
|
||||
}
|
||||
catch ( std::exception& e ) {
|
||||
error() << "task: " << t->name() << " failed: " << e.what() << endl;
|
||||
error() << "task: " << t->taskName() << " failed: " << e.what() << endl;
|
||||
}
|
||||
catch ( ... ) {
|
||||
error() << "task: " << t->name() << " failed with unknown error" << endl;
|
||||
error() << "task: " << t->taskName() << " failed with unknown error" << endl;
|
||||
}
|
||||
|
||||
int ms = timer.millis();
|
||||
LOG( ms <= 3 ) << "task: " << t->name() << " took: " << ms << "ms" << endl;
|
||||
LOG( ms <= 3 ) << "task: " << t->taskName() << " took: " << ms << "ms" << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "concurrency/spin_lock.h"
|
||||
|
||||
namespace mongo {
|
||||
|
||||
/**
|
||||
@ -117,8 +119,8 @@ namespace mongo {
|
||||
PeriodicTask();
|
||||
virtual ~PeriodicTask();
|
||||
|
||||
virtual void doWork() = 0;
|
||||
virtual string name() const = 0;
|
||||
virtual void taskDoWork() = 0;
|
||||
virtual string taskName() const = 0;
|
||||
|
||||
class Runner : public BackgroundJob {
|
||||
public:
|
||||
@ -127,13 +129,20 @@ namespace mongo {
|
||||
virtual string name() const { return "PeriodicTask::Runner"; }
|
||||
|
||||
virtual void run();
|
||||
|
||||
void add( PeriodicTask* task );
|
||||
void remove( PeriodicTask* task );
|
||||
|
||||
private:
|
||||
|
||||
SpinLock _lock;
|
||||
|
||||
// these are NOT owned by Runner
|
||||
// Runner will not delete these
|
||||
// this never gets smaller
|
||||
// only fields replaced with nulls
|
||||
vector<PeriodicTask*> _tasks;
|
||||
|
||||
friend class PeriodicTask;
|
||||
};
|
||||
|
||||
static Runner* theRunner;
|
||||
|
Loading…
Reference in New Issue
Block a user