maybe fixed the scheduler bug (#2034?)

This commit is contained in:
fgerlits 2006-11-30 18:27:24 +00:00
parent 9a4c0ca65f
commit 221a45419e
3 changed files with 43 additions and 33 deletions

View File

@ -97,35 +97,40 @@ SchedulerThread :: getNextEvent(Ptr<ptime>::Ref when) throw ()
void void
SchedulerThread :: nextStep(Ptr<ptime>::Ref now) throw () SchedulerThread :: nextStep(Ptr<ptime>::Ref now) throw ()
{ {
if (!nextEvent.get()) { if (nextEvent) {
return; if (imminent(now, nextInitTime)) {
} debug() << "event init coming" << std::endl;
try {
if (imminent(now, nextInitTime)) { nextEvent->initialize();
debug() << "next event init coming" << std::endl; } catch (std::exception &e) {
try { // cancel event by getting the next event after this was
nextEvent->initialize(); // supposed to finish
} catch (std::exception &e) { getNextEvent(nextEventEnd);
// cancel event by getting the next event after this was // TODO: log error
// supposed to finish std::cerr << "event initialization error: " << e.what()
getNextEvent(nextEventEnd); << std::endl;
// TODO: log error }
std::cerr << "event initialization error: " << e.what() } else if (imminent(now, nextEventTime)) {
<< std::endl; debug() << "event start coming" << std::endl;
Ptr<time_duration>::Ref timeLeft(new time_duration(*nextEventTime
- *now));
TimeConversion::sleep(timeLeft);
nextEvent->start();
currentEvent = nextEvent;
currentEventEnd = nextEventEnd;
Ptr<ptime>::Ref inASecond(new ptime(*now + seconds(1)));
getNextEvent(inASecond);
} }
} else if (imminent(now, nextEventTime)) { }
debug() << "next event start coming" << std::endl;
Ptr<time_duration>::Ref timeLeft(new time_duration(*nextEventTime if (currentEvent && imminent(now, nextEventEnd)) {
debug() << "event end coming" << std::endl;
Ptr<time_duration>::Ref timeLeft(new time_duration(*currentEventEnd
- *now)); - *now));
TimeConversion::sleep(timeLeft); TimeConversion::sleep(timeLeft);
nextEvent->start(); currentEvent->stop();
} else if (imminent(now, nextEventEnd)) { currentEvent->deInitialize();
debug() << "next event end coming" << std::endl; currentEvent.reset();
Ptr<time_duration>::Ref timeLeft(new time_duration(*nextEventEnd
- *now));
TimeConversion::sleep(timeLeft);
nextEvent->stop();
nextEvent->deInitialize();
} }
} }

View File

@ -85,6 +85,11 @@ class SchedulerThread : public virtual RunnableInterface
*/ */
Ptr<EventContainerInterface>::Ref eventContainer; Ptr<EventContainerInterface>::Ref eventContainer;
/**
* The event which is being running now.
*/
Ptr<ScheduledEventInterface>::Ref currentEvent;
/** /**
* The next event to execute. * The next event to execute.
*/ */
@ -100,6 +105,11 @@ class SchedulerThread : public virtual RunnableInterface
*/ */
Ptr<ptime>::Ref nextInitTime; Ptr<ptime>::Ref nextInitTime;
/**
* The ending time of the current event.
*/
Ptr<ptime>::Ref currentEventEnd;
/** /**
* The ending time of the next event. * The ending time of the next event.
*/ */

View File

@ -97,7 +97,6 @@ PlaylistEvent :: initialize(void) throw (std::exception)
{ {
DEBUG_BLOCK DEBUG_BLOCK
std::cerr << "PlaylistEvent :: initialize BEGIN\n";
if (state != created) { if (state != created) {
throw std::logic_error("PlaylistEvent in bad state"); throw std::logic_error("PlaylistEvent in bad state");
} }
@ -108,7 +107,6 @@ std::cerr << "PlaylistEvent :: initialize BEGIN\n";
->getId())); ->getId()));
try { try {
playlist = storage->acquirePlaylist(sessionId, playlistId); playlist = storage->acquirePlaylist(sessionId, playlistId);
std::cerr << "PlaylistEvent :: initialize acquired playlist\n";
} catch (Core::XmlRpcException &e) { } catch (Core::XmlRpcException &e) {
std::string errorMessage = "storage server error: "; std::string errorMessage = "storage server error: ";
errorMessage += e.what(); errorMessage += e.what();
@ -126,6 +124,8 @@ std::cerr << "PlaylistEvent :: initialize acquired playlist\n";
void void
PlaylistEvent :: deInitialize(void) throw () PlaylistEvent :: deInitialize(void) throw ()
{ {
DEBUG_BLOCK
if (state != stopped) { if (state != stopped) {
// TODO: handle error? // TODO: handle error?
return; return;
@ -139,7 +139,6 @@ PlaylistEvent :: deInitialize(void) throw ()
} }
playlist.reset(); playlist.reset();
state = deInitialized; state = deInitialized;
std::cerr << "PlaylistEvent :: deInitialize END\n";
} }
@ -151,7 +150,6 @@ PlaylistEvent :: start(void) throw ()
{ {
DEBUG_BLOCK DEBUG_BLOCK
std::cerr << "PlaylistEvent :: start BEGIN\n";
if (state != initialized) { if (state != initialized) {
// TODO: handle error? // TODO: handle error?
return; return;
@ -160,7 +158,6 @@ std::cerr << "PlaylistEvent :: start BEGIN\n";
try { try {
audioPlayer->open(*playlist->getUri()); audioPlayer->open(*playlist->getUri());
audioPlayer->start(); audioPlayer->start();
std::cerr << "PlaylistEvent :: audio player started\n";
playLog->addPlayLogEntry(playlist->getId(), TimeConversion::now()); playLog->addPlayLogEntry(playlist->getId(), TimeConversion::now());
} catch (std::invalid_argument &e) { } catch (std::invalid_argument &e) {
@ -182,7 +179,6 @@ PlaylistEvent :: stop(void) throw ()
{ {
DEBUG_BLOCK DEBUG_BLOCK
std::cerr << "PlaylistEvent :: stop BEGIN\n";
if (state != running) { if (state != running) {
// TODO: handle error? // TODO: handle error?
return; return;
@ -191,7 +187,6 @@ std::cerr << "PlaylistEvent :: stop BEGIN\n";
try { try {
audioPlayer->stop(); audioPlayer->stop();
audioPlayer->close(); audioPlayer->close();
std::cerr << "PlaylistEvent :: audio player stopped\n";
} catch (std::logic_error &e) { } catch (std::logic_error &e) {
// TODO: handle error // TODO: handle error
// NOTE: this may not be an error, because the user may have stopped // NOTE: this may not be an error, because the user may have stopped