Loading...
Searching...
No Matches
XrdCl::TaskManager Class Reference

`#include <XrdClTaskManager.hh>`

Collaboration diagram for XrdCl::TaskManager:

## Public Member Functions

| TaskManager ()
| Constructor.
| ~TaskManager ()
| Destructor.
void | RegisterTask (Task *task, time_t time, bool own=true)
void | RunTasks ()
| Run the tasks - this loops infinitely.
bool | Start ()
| Start the manager.
bool | Stop ()
void | UnregisterTask (Task *task)

## Detailed Description

Run short tasks at a given time in the future

The task manager just runs one extra thread so the execution of one tasks may interfere with the execution of another

Definition at line 75 of file XrdClTaskManager.hh.

## Constructor & Destructor Documentation

## TaskManager()

XrdCl::TaskManager::TaskManager | ( |  | ) |

Constructor.

Definition at line 48 of file XrdClTaskManager.cc.

: pResolution(1), pRunnerThread(0), pRunning(false)
{}

## ~TaskManager()

XrdCl::TaskManager::~TaskManager | ( |  | ) |

Destructor.

Definition at line 54 of file XrdClTaskManager.cc.

{
TaskSet::iterator it, itE;
for( it = pTasks.begin(); it != pTasks.end(); ++it )
if( it->own )
delete it->task;
}

## Member Function Documentation

## RegisterTask()

void XrdCl::TaskManager::RegisterTask | ( | Task * | task,
|  | time_t | time,
|  | bool | own = true )

Run the given task at the given time.

Parameters

  task | task to be run
  time | time at which the task should be run
  own | determines whether the task object should be destroyed when no longer needed

Definition at line 127 of file XrdClTaskManager.cc.

{
Log *log = DefaultEnv::GetLog();
log->Debug( TaskMgrMsg, "Registering task: \"%s\" to be run at: [%s]",
task->GetName().c_str(), Utils::TimeToString(time).c_str() );
XrdSysMutexHelper scopedLock( pMutex );
pTasks.insert( TaskHelper( task, time, own ) );
}
XrdCl::DefaultEnv::GetLog
static Log * GetLog()
Get default log.
Definition XrdClDefaultEnv.cc:493
XrdCl::Utils::TimeToString
static std::string TimeToString(time_t timestamp)
Convert timestamp to a string.
Definition XrdClUtils.cc:256
XrdCl::TaskMgrMsg
const uint64_t TaskMgrMsg
Definition XrdClConstants.hh:38
XrdGlobal::Log
XrdSysError Log
Definition XrdConfig.cc:113

References XrdCl::Log::Debug(), XrdCl::DefaultEnv::GetLog(), XrdCl::Task::GetName(), XrdCl::TaskMgrMsg, and XrdCl::Utils::TimeToString().

Referenced by XrdCl::DefaultEnv::GetPostMaster(), and XrdCl::XRootDMsgHandler::Process().

Here is the call graph for this function:
Here is the caller graph for this function:

## RunTasks()

void XrdCl::TaskManager::RunTasks | ( |  | ) |

Run the tasks - this loops infinitely.

Definition at line 153 of file XrdClTaskManager.cc.

{
Log *log = DefaultEnv::GetLog();
//--------------------------------------------------------------------------
// We want the thread to be cancelable only when we sleep between tasks
// execution
//--------------------------------------------------------------------------
pthread_setcanceltype( PTHREAD_CANCEL_DEFERRED, 0 );
for(;;)
{
pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, 0 );
pMutex.Lock();
//------------------------------------------------------------------------
// Remove the tasks from the active set - super inefficient,
// but, hopefully, never really necessary. We first need to build a list
// of iterators because it is impossible to remove elements from
// a multiset when iterating over it
//------------------------------------------------------------------------
TaskList::iterator listIt = pToBeUnregistered.begin();
TaskSet::iterator it, itE;
std::list<TaskSet::iterator> iteratorList;
std::list<TaskSet::iterator>::iterator itRem;
for( ; listIt != pToBeUnregistered.end(); ++listIt )
{
for( it = pTasks.begin(); it != pTasks.end(); ++it )
{
if( it->task == *listIt )
iteratorList.push_back( it );
}
}
for( itRem = iteratorList.begin(); itRem != iteratorList.end(); ++itRem )
{
Task *tsk = (*itRem)->task;
bool own = (*itRem)->own;
log->Debug( TaskMgrMsg, "Removing task: \"%s\"", tsk->GetName().c_str() );
pTasks.erase( *itRem );
if( own )
delete tsk;
}
pToBeUnregistered.clear();
//------------------------------------------------------------------------
// Select the tasks to be run
//------------------------------------------------------------------------
time_t now = time(0);
std::list<TaskHelper> toRun;
std::list<TaskHelper>::iterator trIt;
it = pTasks.begin();
itE = pTasks.upper_bound( TaskHelper( 0, now ) );
for( ; it != itE; ++it )
toRun.push_back( TaskHelper( it->task, 0, it->own ) );
pTasks.erase( pTasks.begin(), itE );
pMutex.UnLock();
//------------------------------------------------------------------------
// Run the tasks and reinsert them if necessary
//------------------------------------------------------------------------
for( trIt = toRun.begin(); trIt != toRun.end(); ++trIt )
{
log->Dump( TaskMgrMsg, "Running task: \"%s\"",
trIt->task->GetName().c_str() );
time_t schedule = trIt->task->Run( now );
if( schedule )
{
log->Dump( TaskMgrMsg, "Will rerun task \"%s\" at [%s]",
trIt->task->GetName().c_str(),
Utils::TimeToString(schedule).c_str() );
pMutex.Lock();
pTasks.insert( TaskHelper( trIt->task, schedule, trIt->own ) );
pMutex.UnLock();
}
else
{
log->Debug( TaskMgrMsg, "Done with task: \"%s\"",
trIt->task->GetName().c_str() );
if( trIt->own )
delete trIt->task;
}
}
//------------------------------------------------------------------------
// Enable the cancellation and go to sleep
//------------------------------------------------------------------------
pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, 0 );
pthread_testcancel();
XrdSysTimer::Wait( pResolution*1000 );
}
}
XrdSysTimer::Wait
static void Wait(int milliseconds)
Definition XrdSysTimer.cc:227

References XrdCl::Log::Debug(), XrdCl::Log::Dump(), XrdCl::DefaultEnv::GetLog(), XrdCl::Task::GetName(), XrdCl::TaskMgrMsg, XrdCl::Utils::TimeToString(), and XrdSysTimer::Wait().

Referenced by RunRunnerThread().

Here is the call graph for this function:
Here is the caller graph for this function:

## Start()

bool XrdCl::TaskManager::Start | ( |  | ) |

Start the manager.

Definition at line 65 of file XrdClTaskManager.cc.

{
XrdSysMutexHelper scopedLock( pOpMutex );
Log *log = DefaultEnv::GetLog();
log->Debug( TaskMgrMsg, "Starting the task manager..." );
if( pRunning )
{
log->Error( TaskMgrMsg, "The task manager is already running" );
return false;
}
int ret = ::pthread_create( &pRunnerThread, 0, ::RunRunnerThread, this );
if( ret != 0 )
{
log->Error( TaskMgrMsg, "Unable to spawn the task runner thread: %s",
XrdSysE2T( errno ) );
return false;
}
pRunning = true;
log->Debug( TaskMgrMsg, "Task manager started" );
return true;
}
RunRunnerThread
static void * RunRunnerThread(void *arg)
Definition XrdClJobManager.cc:30
XrdSysE2T
const char * XrdSysE2T(int errcode)
Definition XrdSysE2T.cc:104

References XrdCl::Log::Debug(), XrdCl::Log::Error(), XrdCl::DefaultEnv::GetLog(), RunRunnerThread(), XrdCl::TaskMgrMsg, and XrdSysE2T().

Here is the call graph for this function:

## Stop()

bool XrdCl::TaskManager::Stop | ( |  | ) |

Stop the manager

Will wait until the currently running task completes

Definition at line 92 of file XrdClTaskManager.cc.

{
XrdSysMutexHelper scopedLock( pOpMutex );
Log *log = DefaultEnv::GetLog();
log->Debug( TaskMgrMsg, "Stopping the task manager..." );
if( !pRunning )
{
log->Error( TaskMgrMsg, "The task manager is not running" );
return false;
}
if( ::pthread_cancel( pRunnerThread ) != 0 )
{
log->Error( TaskMgrMsg, "Unable to cancel the task runner thread: %s",
XrdSysE2T( errno ) );
return false;
}
void *threadRet;
int ret = pthread_join( pRunnerThread, (void **)&threadRet );
if( ret != 0 )
{
log->Error( TaskMgrMsg, "Failed to join the task runner thread: %s",
XrdSysE2T( errno ) );
return false;
}
pRunning = false;
log->Debug( TaskMgrMsg, "Task manager stopped" );
return true;
}

References XrdCl::Log::Debug(), XrdCl::Log::Error(), XrdCl::DefaultEnv::GetLog(), XrdCl::TaskMgrMsg, and XrdSysE2T().

Here is the call graph for this function:

## UnregisterTask()

void XrdCl::TaskManager::UnregisterTask | ( | Task * | task | ) |

Remove a task, the unregistration process is asynchronous and may be performed at any point in the future, the function just queues the request. Unregistered task gets destroyed if it was owned by the task manager.

Definition at line 141 of file XrdClTaskManager.cc.

{
Log *log = DefaultEnv::GetLog();
log->Debug( TaskMgrMsg, "Requesting unregistration of: \"%s\"",
task->GetName().c_str() );
XrdSysMutexHelper scopedLock( pMutex );
pToBeUnregistered.push_back( task );
}

References XrdCl::Log::Debug(), XrdCl::DefaultEnv::GetLog(), XrdCl::Task::GetName(), and XrdCl::TaskMgrMsg.

Here is the call graph for this function:

The documentation for this class was generated from the following files:

- XrdClTaskManager.hh
- XrdClTaskManager.cc