From 221a45419ecc69045f83a9b669c948f4274a68a3 Mon Sep 17 00:00:00 2001 From: fgerlits Date: Thu, 30 Nov 2006 18:27:24 +0000 Subject: [PATCH] maybe fixed the scheduler bug (#2034?) --- .../eventScheduler/src/SchedulerThread.cxx | 57 ++++++++++--------- .../eventScheduler/src/SchedulerThread.h | 10 ++++ .../products/scheduler/src/PlaylistEvent.cxx | 9 +-- 3 files changed, 43 insertions(+), 33 deletions(-) diff --git a/campcaster/src/modules/eventScheduler/src/SchedulerThread.cxx b/campcaster/src/modules/eventScheduler/src/SchedulerThread.cxx index b645ee1cb..0cf824f74 100644 --- a/campcaster/src/modules/eventScheduler/src/SchedulerThread.cxx +++ b/campcaster/src/modules/eventScheduler/src/SchedulerThread.cxx @@ -97,35 +97,40 @@ SchedulerThread :: getNextEvent(Ptr::Ref when) throw () void SchedulerThread :: nextStep(Ptr::Ref now) throw () { - if (!nextEvent.get()) { - return; - } - - if (imminent(now, nextInitTime)) { - debug() << "next event init coming" << std::endl; - try { - nextEvent->initialize(); - } catch (std::exception &e) { - // cancel event by getting the next event after this was - // supposed to finish - getNextEvent(nextEventEnd); - // TODO: log error - std::cerr << "event initialization error: " << e.what() - << std::endl; + if (nextEvent) { + if (imminent(now, nextInitTime)) { + debug() << "event init coming" << std::endl; + try { + nextEvent->initialize(); + } catch (std::exception &e) { + // cancel event by getting the next event after this was + // supposed to finish + getNextEvent(nextEventEnd); + // TODO: log error + std::cerr << "event initialization error: " << e.what() + << std::endl; + } + } else if (imminent(now, nextEventTime)) { + debug() << "event start coming" << std::endl; + Ptr::Ref timeLeft(new time_duration(*nextEventTime + - *now)); + TimeConversion::sleep(timeLeft); + nextEvent->start(); + currentEvent = nextEvent; + currentEventEnd = nextEventEnd; + Ptr::Ref inASecond(new ptime(*now + seconds(1))); + getNextEvent(inASecond); } - } else if (imminent(now, nextEventTime)) { - debug() << "next event start coming" << std::endl; - Ptr::Ref timeLeft(new time_duration(*nextEventTime + } + + if (currentEvent && imminent(now, nextEventEnd)) { + debug() << "event end coming" << std::endl; + Ptr::Ref timeLeft(new time_duration(*currentEventEnd - *now)); TimeConversion::sleep(timeLeft); - nextEvent->start(); - } else if (imminent(now, nextEventEnd)) { - debug() << "next event end coming" << std::endl; - Ptr::Ref timeLeft(new time_duration(*nextEventEnd - - *now)); - TimeConversion::sleep(timeLeft); - nextEvent->stop(); - nextEvent->deInitialize(); + currentEvent->stop(); + currentEvent->deInitialize(); + currentEvent.reset(); } } diff --git a/campcaster/src/modules/eventScheduler/src/SchedulerThread.h b/campcaster/src/modules/eventScheduler/src/SchedulerThread.h index eb6b8cfc8..eab9cdfcc 100644 --- a/campcaster/src/modules/eventScheduler/src/SchedulerThread.h +++ b/campcaster/src/modules/eventScheduler/src/SchedulerThread.h @@ -85,6 +85,11 @@ class SchedulerThread : public virtual RunnableInterface */ Ptr::Ref eventContainer; + /** + * The event which is being running now. + */ + Ptr::Ref currentEvent; + /** * The next event to execute. */ @@ -100,6 +105,11 @@ class SchedulerThread : public virtual RunnableInterface */ Ptr::Ref nextInitTime; + /** + * The ending time of the current event. + */ + Ptr::Ref currentEventEnd; + /** * The ending time of the next event. */ diff --git a/campcaster/src/products/scheduler/src/PlaylistEvent.cxx b/campcaster/src/products/scheduler/src/PlaylistEvent.cxx index 2fb1d0cc9..57eb7e6c8 100644 --- a/campcaster/src/products/scheduler/src/PlaylistEvent.cxx +++ b/campcaster/src/products/scheduler/src/PlaylistEvent.cxx @@ -97,7 +97,6 @@ PlaylistEvent :: initialize(void) throw (std::exception) { DEBUG_BLOCK -std::cerr << "PlaylistEvent :: initialize BEGIN\n"; if (state != created) { throw std::logic_error("PlaylistEvent in bad state"); } @@ -108,7 +107,6 @@ std::cerr << "PlaylistEvent :: initialize BEGIN\n"; ->getId())); try { playlist = storage->acquirePlaylist(sessionId, playlistId); -std::cerr << "PlaylistEvent :: initialize acquired playlist\n"; } catch (Core::XmlRpcException &e) { std::string errorMessage = "storage server error: "; errorMessage += e.what(); @@ -126,6 +124,8 @@ std::cerr << "PlaylistEvent :: initialize acquired playlist\n"; void PlaylistEvent :: deInitialize(void) throw () { + DEBUG_BLOCK + if (state != stopped) { // TODO: handle error? return; @@ -139,7 +139,6 @@ PlaylistEvent :: deInitialize(void) throw () } playlist.reset(); state = deInitialized; -std::cerr << "PlaylistEvent :: deInitialize END\n"; } @@ -151,7 +150,6 @@ PlaylistEvent :: start(void) throw () { DEBUG_BLOCK -std::cerr << "PlaylistEvent :: start BEGIN\n"; if (state != initialized) { // TODO: handle error? return; @@ -160,7 +158,6 @@ std::cerr << "PlaylistEvent :: start BEGIN\n"; try { audioPlayer->open(*playlist->getUri()); audioPlayer->start(); -std::cerr << "PlaylistEvent :: audio player started\n"; playLog->addPlayLogEntry(playlist->getId(), TimeConversion::now()); } catch (std::invalid_argument &e) { @@ -182,7 +179,6 @@ PlaylistEvent :: stop(void) throw () { DEBUG_BLOCK -std::cerr << "PlaylistEvent :: stop BEGIN\n"; if (state != running) { // TODO: handle error? return; @@ -191,7 +187,6 @@ std::cerr << "PlaylistEvent :: stop BEGIN\n"; try { audioPlayer->stop(); audioPlayer->close(); -std::cerr << "PlaylistEvent :: audio player stopped\n"; } catch (std::logic_error &e) { // TODO: handle error // NOTE: this may not be an error, because the user may have stopped