Skip to content

Commit

Permalink
made PeriodicTask more explicity
Browse files Browse the repository at this point in the history
  • Loading branch information
erh committed May 17, 2011
1 parent fc2f623 commit ec72258
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
2 changes: 1 addition & 1 deletion s/d_writeback.cpp
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions s/d_writeback.h
Expand Up @@ -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;
Expand Down
42 changes: 33 additions & 9 deletions util/background.cpp
Expand Up @@ -18,6 +18,7 @@
#include "pch.h"

#include "concurrency/mutex.h"
#include "concurrency/spin_lock.h"

#include "background.h"
#include "time_support.h"
Expand Down Expand Up @@ -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++ ) {
if ( inShutdown() )
break;

PeriodicTask * t = _tasks[i];
if ( ! t )
continue;

if ( inShutdown() )
break;

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;
}
}
}
Expand Down
15 changes: 12 additions & 3 deletions util/background.h
Expand Up @@ -17,6 +17,8 @@

#pragma once

#include "concurrency/spin_lock.h"

namespace mongo {

/**
Expand Down Expand Up @@ -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:
Expand All @@ -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;
Expand Down

0 comments on commit ec72258

Please sign in to comment.