1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
18 Revision 1.5 2005/11/21 09:03:48 byordano
21 Revision 1.4 2005/11/20 10:12:37 byordano
22 comments added to AliShuttleTrigger
28 // This class is to deal with DAQ LogBook and DAQ "end of run" notification.
29 // It has severeal two modes:
30 // 1) syncrhnized - Collect(), CollectNew() and CollectAll methods
31 // 2) asynchronized - Run() - starts listening for DAQ "end of run"
32 // notification by DIM service.
35 #include "AliShuttleTrigger.h"
37 #include <TSQLServer.h>
38 #include <TSQLResult.h>
40 #include <TObjArray.h>
44 #include "AliCDBStorage.h"
45 #include "AliCDBEntry.h"
47 #include "AliSimpleValue.h"
48 #include "AliShuttleConfig.h"
49 #include "AliShuttle.h"
50 #include "DATENotifier.h"
52 ClassImp(TerminateSignalHandler)
53 Bool_t TerminateSignalHandler::Notify() {
55 AliInfo("Terminate signal received ...");
56 fTrigger->Terminate();
61 ClassImp(AliShuttleTrigger)
62 AliShuttleTrigger::AliShuttleTrigger(const AliShuttleConfig* config,
63 AliCDBStorage* storage, UInt_t timeout, Int_t retries):
64 fConfig(config), fStorage(storage), fShuttle(NULL),
65 fNotified(kFALSE), fTerminate(kFALSE), fCondition(&fMutex),
66 fQuitSignalHandler(this, kSigQuit),
67 fInterruptSignalHandler(this, kSigInterrupt)
70 // config - pointer to the AliShuttleConfig object which represents
72 // storage - pointer to AliCDBStorage for the undelying CDBStorage
75 fShuttle = new AliShuttle(config, storage, timeout, retries);
77 gSystem->AddSignalHandler(&fQuitSignalHandler);
78 gSystem->AddSignalHandler(&fInterruptSignalHandler);
81 AliShuttleTrigger::~AliShuttleTrigger() {
83 gSystem->RemoveSignalHandler(&fQuitSignalHandler);
84 gSystem->RemoveSignalHandler(&fInterruptSignalHandler);
89 Bool_t AliShuttleTrigger::Notify() {
91 // Trigger CollectNew() methods in asynchronized (listen) mode.
92 // Usually called automaticly by DATENotifier on "end of run"
93 // notification event.
106 void AliShuttleTrigger::Terminate() {
108 // Stop triggers listen mode and exist from Run()
109 // Usually called automaticly by TerminateSignalHandler.
116 void AliShuttleTrigger::Run() {
118 // AliShuttleTrigger main loop for asynchronized (listen) mode.
119 // It spawns DIM service listener and waits for DAQ "end of run"
120 // notification. Calls CollectNew() on notification.
125 DATENotifier* notifier = new DATENotifier(this, "/DATE/LOGBOOK/UPDATE");
131 while (!(fNotified || fTerminate)) {
140 AliInfo("Terminated.");
150 Bool_t AliShuttleTrigger::RetrieveDATEEntries(const char* whereClause,
151 TObjArray& entries, Int_t& lastRun) {
154 sqlQuery += "select run, time_start, time_end from logbook ";
155 sqlQuery += whereClause;
156 sqlQuery += " order by run";
159 aServer = TSQLServer::Connect(fConfig->GetLogBookURI(),
160 fConfig->GetLogBookUser(),
161 fConfig->GetLogBookPassword());
163 AliError("Can't establish connection to DAQ log book DB!");
168 aResult = aServer->Query(sqlQuery);
170 AliError(Form("Can't execute query <%s>!", sqlQuery.Data()));
175 if (aResult->GetFieldCount() != 3) {
176 AliError("Invalid SQL result field number!");
185 while ((aRow = aResult->Next())) {
186 TString runString(aRow->GetField(0), aRow->GetFieldLength(0));
187 Int_t run = runString.Atoi();
189 TString startTimeString(aRow->GetField(1),
190 aRow->GetFieldLength(1));
191 Int_t startTime = startTimeString.Atoi();
193 AliWarning(Form("Zero StartTime for run <%d>!", run));
194 AliWarning("Going to skip this run!");
198 TString endTimeString(aRow->GetField(2),
199 aRow->GetFieldLength(2));
200 Int_t endTime = endTimeString.Atoi();
202 AliWarning(Form("Zero EndTime for run <%d>!", run));
203 AliWarning("Going to skip this run!");
207 if (startTime > endTime) {
208 AliWarning(Form("StartTime bigger than EndTime for run <%d>", run));
209 AliWarning("Going to skip this run!");
213 entries.AddLast(new DATEEntry(run, startTime, endTime));
230 Bool_t AliShuttleTrigger::RetrieveConditionsData(const TObjArray& dateEntries) {
232 Bool_t hasError = kFALSE;
234 TIter iter(&dateEntries);
236 while ((anEntry = (DATEEntry*) iter.Next())) {
237 if(!fShuttle->Process(anEntry->GetRun(),
238 anEntry->GetStartTime(),
239 anEntry->GetEndTime())) {
247 Bool_t AliShuttleTrigger::Collect(Int_t run) {
249 // Collects conditions date for the given run.
252 AliInfo(Form("Collecting conditions data for run <%d> ...", run));
254 TString whereClause("where run = ");
258 TObjArray dateEntries;
259 if (!RetrieveDATEEntries(whereClause, dateEntries, lastRun)) {
260 AliError("Can't retrieve entries from DAQ log book.");
264 if (!dateEntries.GetEntriesFast()) {
265 AliError(Form("There isn't entry for run <%d> in DAQ log book!",
270 if (dateEntries.GetEntriesFast() > 1) {
271 AliError(Form("There is more than one entry for run <%d> in DAQ log book", run));
275 if (!RetrieveConditionsData(dateEntries)) {
276 AliError("An error occured during conditions data retrieval!");
283 Bool_t AliShuttleTrigger::CollectNew() {
285 // Collects conditions data for all new run written to DAQ LogBook.
288 AliInfo("Collecting conditions data for new runs ...");
292 AliCDBEntry* cdbEntry = fStorage->Get("/SHUTTLE/SYSTEM/LASTRUN", 0);
294 TObject* anObject = cdbEntry->GetObject();
295 if (anObject == NULL ||
296 anObject->IsA() != AliSimpleValue::Class()) {
297 AliError("Invalid last run object stored to CDB!");
300 AliSimpleValue* simpleValue = (AliSimpleValue*) anObject;
301 lastRun = simpleValue->GetInt();
304 AliWarning("There isn't last run stored! Starting from run 0");
308 AliInfo(Form("Last run number <%d>", lastRun));
310 TString whereClause("where run > ");
311 whereClause += lastRun;
314 TObjArray dateEntries;
315 if (!RetrieveDATEEntries(whereClause, dateEntries, newLastRun)) {
316 AliError("Can't retrieve entries from DAQ log book.");
320 if (newLastRun > lastRun) {
321 AliSimpleValue lastRunObj(newLastRun);
322 AliCDBMetaData metaData;
323 AliCDBId cdbID(AliCDBPath("SHUTTLE", "SYSTEM", "LASTRUN"), 0, 0);
325 if (!fStorage->Put(&lastRunObj, cdbID, &metaData)) {
326 AliError("Can't store last run to CDB!");
331 if (!RetrieveConditionsData(dateEntries)) {
332 AliError("An error occured during conditions data retrieval!");
339 Bool_t AliShuttleTrigger::CollectAll() {
341 // Collects conditions data for all run written in DAQ LogBook.
344 AliInfo("Collecting conditions data for all runs ...");
347 TObjArray dateEntries;
348 if (!RetrieveDATEEntries("", dateEntries, lastRun)) {
349 AliError("Can't retrieve entries from DAQ log book.");
353 if (!RetrieveConditionsData(dateEntries)) {
354 AliError("An error occured during conditions data retrieval!");