dca_interface  6.3.4
Common shutdown functions - safely shut down

As detailed in the example programs found under samples/url_sample_extended or samples/customdbsample_extended, it is necessary to create two threads for the scheduling and update mechanisms.

The threads are usually in a loop and call the schedule() and performUpdate() functions:

void updateTask( void *myData )
{
const MyDcaContext *myDcaContext =
reinterprete_cast< MyDcaContext *>( myData );
if( myDcaContext ) {
while( !myDcaContext->inShutdown ) {
dca::UpdateResults myUpdateResults;
myDcaContext->updateModule.performUpdate( false,
myUpdateResults );
if( !fr ) {
// got an error... try to re-initialize the DCA
return;
}
// enumerate myUpdateResults...
sleep( 1000 ); // wait 1 second until next call to performUpdate()
}
}
}
// subscriber to catch the events of the DcaInstance::schedule function
{
public:
virtual void onEvent( dca::ScheduleActionType actionType,
dca::ScheduleModuleId moduleId, const std::string& version,
const std::string& text )
{
// Handle the event...
}
};
void schedulerTask( void *myData )
{
const MyDcaContext *myDcaContext =
reinterprete_cast< MyDcaContext *>( myData );
if( myDcaContext ) {
while( !myDcaContext->inShutdown ) {
myDcaContext->dcaInstance.schedule( &mySubscriber );
if( !fr ) {
// got an error... try to re-initialize the DCA
return;
}
sleep( 5000 ); // wait some seconds until next call to schedule()
}
}
}

Both threads must have an end condition. In this case, the class MyContext contains a variable inShutdown. This variable is set to true in the destructor of MyContext.

The MyContext destructor also calls the functions dca::UpdateModule::cancelUpdate() and dca::DcaInstance::signal() before the dca::DcaInstance objects are destroyed. The cancelUpdate() function cancels all currently active downloads and uploads, while the signal() function aborts all other long-lasting processes.

The following code sample demonstrates this:

class MyDcaContext
{
public:
MyDcaContext() : inShutDown (false)
{ }
virtual ~MyDcaContext()
{
// signal DCA worker threads to shutdown
inShutDown = true;
// this cancels the download or update task if it is currently running
updateModule.cancelUpdate();
// this cancels all long-lasting functions and forces them to return immediately
// such as database merge processes...
dcaInstance.signal( DCA_SIG_ABORT );
// ...
// wait for worker threads to finish
// ...
// all other DCA objects are destructed in reverse construction order
}
// initialize the DCA objects
virtual bool Init();
// Construction order is important!
// the DCA instance must always be created first.
dca::DcaInstance dcaInstance;
dca::InitData initData;
dca::License license;
dca::UpdateModule updateModule;
dca::DbConnectionData connectData;
dca::DbConnection dbConnection;
dca::UrlClassification urlClassification;
dca::UrlDbClassifier urlDbClassifier;
volatile bool inShutDown;
...
};

After both functions have been successfully called, it is necessary to wait for the worker threads to end before shutting down the dca::DcaInstance objects.

Example implementation of a schedule event subscriber.
The update module is used to download and install DCA content and engine updates.
Definition: base_classes.h:917
Encapsulates the results of an update process.
Definition: base_classes.h:998
const unsigned int DCA_SIG_ABORT
Currently the only supported signal parameter for the DcaInstance::signal() function.
Stores the connection data for a database.
Definition: base_classes.h:815
ScheduleModuleId
This defines the module id of a schedule event.
Definition: base_classes.h:511
ScheduleActionType
This defines the action type of a schedule event.
Definition: base_classes.h:495
Database connection class for a local or remote database.
Definition: base_classes.h:859
virtual void onEvent(dca::ScheduleActionType actionType, dca::ScheduleModuleId moduleId, const std::string &version, const std::string &text)
Implementation of interface class method onEvent.
Main class for the URL classification.
URL database classifier class.
Use a License to initialize a classification package or a toolbox package.
Definition: base_classes.h:560
Encapsulates the init and deinit of the DCA API.
Definition: base_classes.h:315
Standard function result.
Definition: base_classes.h:148
An interface for schedule event notifications. Derive a class from this interface and implement onEve...
Definition: base_classes.h:526
This structure is used to initialize the DcaInstance.
Definition: base_classes.h:264