Mercurial > hg > orthanc
comparison OrthancServer/Plugins/Samples/Housekeeper/Plugin.cpp @ 4979:f316413027fd more-tags
renamed DbOptizer into Housekeeper + add Scheduler & triggers
author | Alain Mazy <am@osimis.io> |
---|---|
date | Mon, 25 Apr 2022 15:30:51 +0200 |
parents | OrthancServer/Plugins/Samples/DbOptimizer/Plugin.cpp@1b76853e1797 |
children | d0c34145320c |
comparison
equal
deleted
inserted
replaced
4978:2cfa50d8eb60 | 4979:f316413027fd |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium | |
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium | |
7 * | |
8 * This program is free software: you can redistribute it and/or | |
9 * modify it under the terms of the GNU General Public License as | |
10 * published by the Free Software Foundation, either version 3 of the | |
11 * License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 **/ | |
21 | |
22 | |
23 #include "../../../../OrthancFramework/Sources/Compatibility.h" | |
24 #include "../Common/OrthancPluginCppWrapper.h" | |
25 | |
26 #include <boost/thread.hpp> | |
27 #include <boost/algorithm/string.hpp> | |
28 #include <json/value.h> | |
29 #include <json/writer.h> | |
30 #include <string.h> | |
31 #include <iostream> | |
32 #include <algorithm> | |
33 #include <map> | |
34 #include <list> | |
35 #include <time.h> | |
36 | |
37 static int globalPropertyId_ = 0; | |
38 static bool force_ = false; | |
39 static uint throttleDelay_ = 0; | |
40 static std::unique_ptr<boost::thread> workerThread_; | |
41 static bool workerThreadShouldStop_ = false; | |
42 static bool triggerOnStorageCompressionChange_ = true; | |
43 static bool triggerOnMainDicomTagsChange_ = true; | |
44 static bool triggerOnUnnecessaryDicomAsJsonFiles_ = true; | |
45 | |
46 | |
47 struct RunningPeriod | |
48 { | |
49 int fromHour_; | |
50 int toHour_; | |
51 int weekday_; | |
52 | |
53 RunningPeriod(const std::string& weekday, const std::string& period) | |
54 { | |
55 if (weekday == "Monday") | |
56 { | |
57 weekday_ = 1; | |
58 } | |
59 else if (weekday == "Tuesday") | |
60 { | |
61 weekday_ = 2; | |
62 } | |
63 else if (weekday == "Wednesday") | |
64 { | |
65 weekday_ = 3; | |
66 } | |
67 else if (weekday == "Thursday") | |
68 { | |
69 weekday_ = 4; | |
70 } | |
71 else if (weekday == "Friday") | |
72 { | |
73 weekday_ = 5; | |
74 } | |
75 else if (weekday == "Saturday") | |
76 { | |
77 weekday_ = 6; | |
78 } | |
79 else if (weekday == "Sunday") | |
80 { | |
81 weekday_ = 0; | |
82 } | |
83 else | |
84 { | |
85 OrthancPlugins::LogWarning("Housekeeper: invalid schedule: unknown 'day': " + weekday); | |
86 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
87 } | |
88 | |
89 std::vector<std::string> hours; | |
90 boost::split(hours, period, boost::is_any_of("-")); | |
91 | |
92 fromHour_ = boost::lexical_cast<int>(hours[0]); | |
93 toHour_ = boost::lexical_cast<int>(hours[1]); | |
94 } | |
95 | |
96 bool isInPeriod() const | |
97 { | |
98 time_t now = time(NULL); | |
99 tm* nowLocalTime = localtime(&now); | |
100 | |
101 if (nowLocalTime->tm_wday != weekday_) | |
102 { | |
103 return false; | |
104 } | |
105 | |
106 if (nowLocalTime->tm_hour >= fromHour_ && nowLocalTime->tm_hour < toHour_) | |
107 { | |
108 return true; | |
109 } | |
110 | |
111 return false; | |
112 } | |
113 }; | |
114 | |
115 struct RunningPeriods | |
116 { | |
117 std::list<RunningPeriod> runningPeriods_; | |
118 | |
119 void load(const Json::Value& scheduleConfiguration) | |
120 { | |
121 // "Monday": ["0-6", "20-24"], | |
122 | |
123 Json::Value::Members names = scheduleConfiguration.getMemberNames(); | |
124 | |
125 for (Json::Value::Members::const_iterator it = names.begin(); | |
126 it != names.end(); it++) | |
127 { | |
128 for (Json::Value::ArrayIndex i = 0; i < scheduleConfiguration[*it].size(); i++) | |
129 { | |
130 runningPeriods_.push_back(RunningPeriod(*it, scheduleConfiguration[*it][i].asString())); | |
131 } | |
132 } | |
133 } | |
134 | |
135 bool isInPeriod() | |
136 { | |
137 if (runningPeriods_.size() == 0) | |
138 { | |
139 return true; // if no config: always run | |
140 } | |
141 | |
142 for (std::list<RunningPeriod>::const_iterator it = runningPeriods_.begin(); | |
143 it != runningPeriods_.end(); it++) | |
144 { | |
145 if (it->isInPeriod()) | |
146 { | |
147 return true; | |
148 } | |
149 } | |
150 return false; | |
151 } | |
152 }; | |
153 | |
154 RunningPeriods runningPeriods_; | |
155 | |
156 struct DbConfiguration | |
157 { | |
158 std::string orthancVersion; | |
159 std::map<OrthancPluginResourceType, std::string> mainDicomTagsSignature; | |
160 bool storageCompressionEnabled; | |
161 | |
162 DbConfiguration() | |
163 : storageCompressionEnabled(false) | |
164 { | |
165 } | |
166 | |
167 bool IsDefined() const | |
168 { | |
169 return !orthancVersion.empty() && mainDicomTagsSignature.size() == 4; | |
170 } | |
171 | |
172 void Clear() | |
173 { | |
174 orthancVersion.clear(); | |
175 mainDicomTagsSignature.clear(); | |
176 } | |
177 | |
178 void ToJson(Json::Value& target) | |
179 { | |
180 if (!IsDefined()) | |
181 { | |
182 target = Json::nullValue; | |
183 } | |
184 else | |
185 { | |
186 Json::Value signatures; | |
187 | |
188 target = Json::objectValue; | |
189 | |
190 // default main dicom tags signature are the one from Orthanc 1.4.2 (last time the list was changed): | |
191 signatures["Patient"] = mainDicomTagsSignature[OrthancPluginResourceType_Patient]; | |
192 signatures["Study"] = mainDicomTagsSignature[OrthancPluginResourceType_Study]; | |
193 signatures["Series"] = mainDicomTagsSignature[OrthancPluginResourceType_Series]; | |
194 signatures["Instance"] = mainDicomTagsSignature[OrthancPluginResourceType_Instance]; | |
195 | |
196 target["MainDicomTagsSignature"] = signatures; | |
197 target["OrthancVersion"] = orthancVersion; | |
198 target["StorageCompressionEnabled"] = storageCompressionEnabled; | |
199 } | |
200 } | |
201 | |
202 void FromJson(Json::Value& source) | |
203 { | |
204 if (!source.isNull()) | |
205 { | |
206 orthancVersion = source["OrthancVersion"].asString(); | |
207 | |
208 const Json::Value& signatures = source["MainDicomTagsSignature"]; | |
209 mainDicomTagsSignature[OrthancPluginResourceType_Patient] = signatures["Patient"].asString(); | |
210 mainDicomTagsSignature[OrthancPluginResourceType_Study] = signatures["Study"].asString(); | |
211 mainDicomTagsSignature[OrthancPluginResourceType_Series] = signatures["Series"].asString(); | |
212 mainDicomTagsSignature[OrthancPluginResourceType_Instance] = signatures["Instance"].asString(); | |
213 | |
214 storageCompressionEnabled = source["StorageCompressionEnabled"].asBool(); | |
215 } | |
216 } | |
217 }; | |
218 | |
219 struct PluginStatus | |
220 { | |
221 int statusVersion; | |
222 int64_t lastProcessedChange; | |
223 int64_t lastChangeToProcess; | |
224 | |
225 DbConfiguration currentlyProcessingConfiguration; // last configuration being processed (has not reached last change yet) | |
226 DbConfiguration lastProcessedConfiguration; // last configuration that has been fully processed (till last change) | |
227 | |
228 PluginStatus() | |
229 : statusVersion(1), | |
230 lastProcessedChange(-1), | |
231 lastChangeToProcess(-1) | |
232 { | |
233 } | |
234 | |
235 void ToJson(Json::Value& target) | |
236 { | |
237 target = Json::objectValue; | |
238 | |
239 target["Version"] = statusVersion; | |
240 target["LastProcessedChange"] = Json::Value::Int64(lastProcessedChange); | |
241 target["LastChangeToProcess"] = Json::Value::Int64(lastChangeToProcess); | |
242 | |
243 currentlyProcessingConfiguration.ToJson(target["CurrentlyProcessingConfiguration"]); | |
244 lastProcessedConfiguration.ToJson(target["LastProcessedConfiguration"]); | |
245 } | |
246 | |
247 void FromJson(Json::Value& source) | |
248 { | |
249 statusVersion = source["Version"].asInt(); | |
250 lastProcessedChange = source["LastProcessedChange"].asInt64(); | |
251 lastChangeToProcess = source["LastChangeToProcess"].asInt64(); | |
252 | |
253 Json::Value& current = source["CurrentlyProcessingConfiguration"]; | |
254 Json::Value& last = source["LastProcessedConfiguration"]; | |
255 | |
256 currentlyProcessingConfiguration.FromJson(current); | |
257 lastProcessedConfiguration.FromJson(last); | |
258 } | |
259 }; | |
260 | |
261 | |
262 static void ReadStatusFromDb(PluginStatus& pluginStatus) | |
263 { | |
264 OrthancPlugins::OrthancString globalPropertyContent; | |
265 | |
266 globalPropertyContent.Assign(OrthancPluginGetGlobalProperty(OrthancPlugins::GetGlobalContext(), | |
267 globalPropertyId_, | |
268 "")); | |
269 | |
270 if (!globalPropertyContent.IsNullOrEmpty()) | |
271 { | |
272 Json::Value jsonStatus; | |
273 globalPropertyContent.ToJson(jsonStatus); | |
274 pluginStatus.FromJson(jsonStatus); | |
275 } | |
276 else | |
277 { | |
278 // default config | |
279 pluginStatus.statusVersion = 1; | |
280 pluginStatus.lastProcessedChange = -1; | |
281 pluginStatus.lastChangeToProcess = -1; | |
282 | |
283 pluginStatus.currentlyProcessingConfiguration.orthancVersion = "1.9.0"; // when we don't know, we assume some files were stored with Orthanc 1.9.0 (last version saving the dicom-as-json files) | |
284 | |
285 // default main dicom tags signature are the one from Orthanc 1.4.2 (last time the list was changed): | |
286 pluginStatus.currentlyProcessingConfiguration.mainDicomTagsSignature[OrthancPluginResourceType_Patient] = "0010,0010;0010,0020;0010,0030;0010,0040;0010,1000"; | |
287 pluginStatus.currentlyProcessingConfiguration.mainDicomTagsSignature[OrthancPluginResourceType_Study] = "0008,0020;0008,0030;0008,0050;0008,0080;0008,0090;0008,1030;0020,000d;0020,0010;0032,1032;0032,1060"; | |
288 pluginStatus.currentlyProcessingConfiguration.mainDicomTagsSignature[OrthancPluginResourceType_Series] = "0008,0021;0008,0031;0008,0060;0008,0070;0008,1010;0008,103e;0008,1070;0018,0010;0018,0015;0018,0024;0018,1030;0018,1090;0018,1400;0020,000e;0020,0011;0020,0037;0020,0105;0020,1002;0040,0254;0054,0081;0054,0101;0054,1000"; | |
289 pluginStatus.currentlyProcessingConfiguration.mainDicomTagsSignature[OrthancPluginResourceType_Instance] = "0008,0012;0008,0013;0008,0018;0020,0012;0020,0013;0020,0032;0020,0037;0020,0100;0020,4000;0028,0008;0054,1330"; | |
290 } | |
291 } | |
292 | |
293 static void SaveStatusInDb(PluginStatus& pluginStatus) | |
294 { | |
295 Json::Value jsonStatus; | |
296 pluginStatus.ToJson(jsonStatus); | |
297 | |
298 Json::StreamWriterBuilder builder; | |
299 builder.settings_["indentation"] = " "; | |
300 std::string serializedStatus = Json::writeString(builder, jsonStatus); | |
301 | |
302 OrthancPluginSetGlobalProperty(OrthancPlugins::GetGlobalContext(), | |
303 globalPropertyId_, | |
304 serializedStatus.c_str()); | |
305 } | |
306 | |
307 static void GetCurrentDbConfiguration(DbConfiguration& configuration) | |
308 { | |
309 Json::Value signatures; | |
310 Json::Value systemInfo; | |
311 | |
312 OrthancPlugins::RestApiGet(systemInfo, "/system", false); | |
313 configuration.mainDicomTagsSignature[OrthancPluginResourceType_Patient] = systemInfo["MainDicomTags"]["Patient"].asString(); | |
314 configuration.mainDicomTagsSignature[OrthancPluginResourceType_Study] = systemInfo["MainDicomTags"]["Study"].asString(); | |
315 configuration.mainDicomTagsSignature[OrthancPluginResourceType_Series] = systemInfo["MainDicomTags"]["Series"].asString(); | |
316 configuration.mainDicomTagsSignature[OrthancPluginResourceType_Instance] = systemInfo["MainDicomTags"]["Instance"].asString(); | |
317 configuration.storageCompressionEnabled = systemInfo["StorageCompression"].asBool(); | |
318 | |
319 configuration.orthancVersion = OrthancPlugins::GetGlobalContext()->orthancVersion; | |
320 } | |
321 | |
322 static bool NeedsProcessing(const DbConfiguration& current, const DbConfiguration& last) | |
323 { | |
324 if (!last.IsDefined()) | |
325 { | |
326 return true; | |
327 } | |
328 | |
329 const char* lastVersion = last.orthancVersion.c_str(); | |
330 const std::map<OrthancPluginResourceType, std::string>& lastTags = last.mainDicomTagsSignature; | |
331 const std::map<OrthancPluginResourceType, std::string>& currentTags = current.mainDicomTagsSignature; | |
332 bool needsProcessing = false; | |
333 | |
334 if (!OrthancPlugins::CheckMinimalVersion(lastVersion, 1, 9, 1)) | |
335 { | |
336 if (triggerOnUnnecessaryDicomAsJsonFiles_) | |
337 { | |
338 OrthancPlugins::LogWarning("Housekeeper: your storage might still contain some dicom-as-json files -> will perform housekeeping"); | |
339 needsProcessing = true; | |
340 } | |
341 else | |
342 { | |
343 OrthancPlugins::LogWarning("Housekeeper: your storage might still contain some dicom-as-json files but the trigger has been disabled"); | |
344 } | |
345 } | |
346 | |
347 if (lastTags.at(OrthancPluginResourceType_Patient) != currentTags.at(OrthancPluginResourceType_Patient)) | |
348 { | |
349 if (triggerOnMainDicomTagsChange_) | |
350 { | |
351 OrthancPlugins::LogWarning("Housekeeper: Patient main dicom tags have changed, -> will perform housekeeping"); | |
352 needsProcessing = true; | |
353 } | |
354 else | |
355 { | |
356 OrthancPlugins::LogWarning("Housekeeper: Patient main dicom tags have changed but the trigger is disabled"); | |
357 } | |
358 } | |
359 | |
360 if (lastTags.at(OrthancPluginResourceType_Study) != currentTags.at(OrthancPluginResourceType_Study)) | |
361 { | |
362 if (triggerOnMainDicomTagsChange_) | |
363 { | |
364 OrthancPlugins::LogWarning("Housekeeper: Study main dicom tags have changed, -> will perform housekeeping"); | |
365 needsProcessing = true; | |
366 } | |
367 else | |
368 { | |
369 OrthancPlugins::LogWarning("Housekeeper: Study main dicom tags have changed but the trigger is disabled"); | |
370 } | |
371 } | |
372 | |
373 if (lastTags.at(OrthancPluginResourceType_Series) != currentTags.at(OrthancPluginResourceType_Series)) | |
374 { | |
375 if (triggerOnMainDicomTagsChange_) | |
376 { | |
377 OrthancPlugins::LogWarning("Housekeeper: Series main dicom tags have changed, -> will perform housekeeping"); | |
378 needsProcessing = true; | |
379 } | |
380 else | |
381 { | |
382 OrthancPlugins::LogWarning("Housekeeper: Series main dicom tags have changed but the trigger is disabled"); | |
383 } | |
384 } | |
385 | |
386 if (lastTags.at(OrthancPluginResourceType_Instance) != currentTags.at(OrthancPluginResourceType_Instance)) | |
387 { | |
388 if (triggerOnMainDicomTagsChange_) | |
389 { | |
390 OrthancPlugins::LogWarning("Housekeeper: Instance main dicom tags have changed, -> will perform housekeeping"); | |
391 needsProcessing = true; | |
392 } | |
393 else | |
394 { | |
395 OrthancPlugins::LogWarning("Housekeeper: Instance main dicom tags have changed but the trigger is disabled"); | |
396 } | |
397 } | |
398 | |
399 if (current.storageCompressionEnabled != last.storageCompressionEnabled) | |
400 { | |
401 if (triggerOnStorageCompressionChange_) | |
402 { | |
403 if (current.storageCompressionEnabled) | |
404 { | |
405 OrthancPlugins::LogWarning("Housekeeper: storage compression is now enabled -> will perform housekeeping"); | |
406 } | |
407 else | |
408 { | |
409 OrthancPlugins::LogWarning("Housekeeper: storage compression is now disabled -> will perform housekeeping"); | |
410 } | |
411 | |
412 needsProcessing = true; | |
413 } | |
414 else | |
415 { | |
416 OrthancPlugins::LogWarning("Housekeeper: storage compression has changed but the trigger is disabled"); | |
417 } | |
418 } | |
419 | |
420 return needsProcessing; | |
421 } | |
422 | |
423 static bool ProcessChanges(PluginStatus& pluginStatus, const DbConfiguration& currentDbConfiguration) | |
424 { | |
425 Json::Value changes; | |
426 | |
427 pluginStatus.currentlyProcessingConfiguration = currentDbConfiguration; | |
428 | |
429 OrthancPlugins::RestApiGet(changes, "/changes?since=" + boost::lexical_cast<std::string>(pluginStatus.lastProcessedChange) + "&limit=100", false); | |
430 | |
431 for (Json::ArrayIndex i = 0; i < changes["Changes"].size(); i++) | |
432 { | |
433 const Json::Value& change = changes["Changes"][i]; | |
434 int64_t seq = change["Seq"].asInt64(); | |
435 | |
436 if (change["ChangeType"] == "NewStudy") // some StableStudy might be missing if orthanc was shutdown during a StableAge -> consider only the NewStudy events that can not be missed | |
437 { | |
438 Json::Value result; | |
439 OrthancPlugins::RestApiPost(result, "/studies/" + change["ID"].asString() + "/reconstruct", std::string(""), false); | |
440 boost::this_thread::sleep(boost::posix_time::milliseconds(throttleDelay_ * 1000)); | |
441 } | |
442 | |
443 if (seq >= pluginStatus.lastChangeToProcess) // we are done ! | |
444 { | |
445 return true; | |
446 } | |
447 | |
448 pluginStatus.lastProcessedChange = seq; | |
449 } | |
450 | |
451 return false; | |
452 } | |
453 | |
454 | |
455 static void WorkerThread() | |
456 { | |
457 PluginStatus pluginStatus; | |
458 DbConfiguration currentDbConfiguration; | |
459 | |
460 OrthancPluginLogWarning(OrthancPlugins::GetGlobalContext(), "Starting Housekeeper worker thread"); | |
461 | |
462 ReadStatusFromDb(pluginStatus); | |
463 GetCurrentDbConfiguration(currentDbConfiguration); | |
464 | |
465 if (!NeedsProcessing(currentDbConfiguration, pluginStatus.lastProcessedConfiguration)) | |
466 { | |
467 OrthancPlugins::LogWarning("Housekeeper: everything has been processed already !"); | |
468 return; | |
469 } | |
470 | |
471 if (force_ || NeedsProcessing(currentDbConfiguration, pluginStatus.currentlyProcessingConfiguration)) | |
472 { | |
473 if (force_) | |
474 { | |
475 OrthancPlugins::LogWarning("Housekeeper: forcing execution -> will perform housekeeping"); | |
476 } | |
477 else | |
478 { | |
479 OrthancPlugins::LogWarning("Housekeeper: the DB configuration has changed since last run, will reprocess the whole DB !"); | |
480 } | |
481 | |
482 Json::Value changes; | |
483 OrthancPlugins::RestApiGet(changes, "/changes?last", false); | |
484 | |
485 pluginStatus.lastProcessedChange = 0; | |
486 pluginStatus.lastChangeToProcess = changes["Last"].asInt64(); // the last change is the last change at the time we start. We assume that every new ingested file will be constructed correctly | |
487 } | |
488 else | |
489 { | |
490 OrthancPlugins::LogWarning("Housekeeper: the DB configuration has not changed since last run, will continue processing changes"); | |
491 } | |
492 | |
493 bool completed = pluginStatus.lastChangeToProcess == 0; // if the DB is empty at start, no need to process anyting | |
494 bool loggedNotRightPeriodChangeMessage = false; | |
495 | |
496 while (!workerThreadShouldStop_ && !completed) | |
497 { | |
498 if (runningPeriods_.isInPeriod()) | |
499 { | |
500 completed = ProcessChanges(pluginStatus, currentDbConfiguration); | |
501 SaveStatusInDb(pluginStatus); | |
502 | |
503 if (!completed) | |
504 { | |
505 OrthancPlugins::LogInfo("Housekeeper: processed changes " + | |
506 boost::lexical_cast<std::string>(pluginStatus.lastProcessedChange) + | |
507 " / " + boost::lexical_cast<std::string>(pluginStatus.lastChangeToProcess)); | |
508 | |
509 boost::this_thread::sleep(boost::posix_time::milliseconds(throttleDelay_ * 100)); // wait 1/10 of the delay between changes | |
510 } | |
511 | |
512 loggedNotRightPeriodChangeMessage = false; | |
513 } | |
514 else | |
515 { | |
516 if (!loggedNotRightPeriodChangeMessage) | |
517 { | |
518 OrthancPlugins::LogInfo("Housekeeper: entering quiet period"); | |
519 loggedNotRightPeriodChangeMessage = true; | |
520 } | |
521 } | |
522 } | |
523 | |
524 if (completed) | |
525 { | |
526 pluginStatus.lastProcessedConfiguration = currentDbConfiguration; | |
527 pluginStatus.currentlyProcessingConfiguration.Clear(); | |
528 | |
529 pluginStatus.lastProcessedChange = -1; | |
530 pluginStatus.lastChangeToProcess = -1; | |
531 | |
532 SaveStatusInDb(pluginStatus); | |
533 | |
534 OrthancPluginLogWarning(OrthancPlugins::GetGlobalContext(), "Housekeeper: finished processing all changes"); | |
535 } | |
536 } | |
537 | |
538 extern "C" | |
539 { | |
540 OrthancPluginErrorCode OnChangeCallback(OrthancPluginChangeType changeType, | |
541 OrthancPluginResourceType resourceType, | |
542 const char* resourceId) | |
543 { | |
544 switch (changeType) | |
545 { | |
546 case OrthancPluginChangeType_OrthancStarted: | |
547 { | |
548 workerThread_.reset(new boost::thread(WorkerThread)); | |
549 return OrthancPluginErrorCode_Success; | |
550 } | |
551 case OrthancPluginChangeType_OrthancStopped: | |
552 { | |
553 if (workerThread_ && workerThread_->joinable()) | |
554 { | |
555 workerThreadShouldStop_ = true; | |
556 workerThread_->join(); | |
557 } | |
558 } | |
559 default: | |
560 return OrthancPluginErrorCode_Success; | |
561 } | |
562 } | |
563 | |
564 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* c) | |
565 { | |
566 OrthancPlugins::SetGlobalContext(c); | |
567 | |
568 /* Check the version of the Orthanc core */ | |
569 if (OrthancPluginCheckVersion(c) == 0) | |
570 { | |
571 OrthancPlugins::ReportMinimalOrthancVersion(ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER, | |
572 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER, | |
573 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER); | |
574 return -1; | |
575 } | |
576 | |
577 OrthancPlugins::LogWarning("Housekeeper plugin is initializing"); | |
578 OrthancPluginSetDescription(c, "Optimizes your DB and storage."); | |
579 | |
580 OrthancPlugins::OrthancConfiguration configuration; | |
581 | |
582 OrthancPlugins::OrthancConfiguration housekeeper; | |
583 configuration.GetSection(housekeeper, "Housekeeper"); | |
584 | |
585 bool enabled = housekeeper.GetBooleanValue("Enable", false); | |
586 if (enabled) | |
587 { | |
588 /* | |
589 { | |
590 "Housekeeper": { | |
591 | |
592 // Enables/disables the plugin | |
593 "Enable": false, | |
594 | |
595 // the Global Prooperty ID in which the plugin progress | |
596 // is stored. Must be > 1024 and must not be used by | |
597 // another plugin | |
598 "GlobalPropertyId": 1025, | |
599 | |
600 // Forces execution even if the plugin did not detect | |
601 // any changes in configuration | |
602 "Force": false, | |
603 | |
604 // Delay (in seconds) between reconstruction of 2 studies | |
605 // This avoids overloading Orthanc with the housekeeping | |
606 // process and leaves room for other operations. | |
607 "ThrottleDelay": 5, | |
608 | |
609 // Runs the plugin only at certain period of time. | |
610 // If not specified, the plugin runs all the time | |
611 // Examples: | |
612 // to run between 0AM and 6AM everyday + every night | |
613 // from 8PM to 12PM and 24h a day on the weekend: | |
614 // "Schedule": { | |
615 // "Monday": ["0-6", "20-24"], | |
616 // "Tuesday": ["0-6", "20-24"], | |
617 // "Wednesday": ["0-6", "20-24"], | |
618 // "Thursday": ["0-6", "20-24"], | |
619 // "Friday": ["0-6", "20-24"], | |
620 // "Saturday": ["0-24"], | |
621 // "Sunday": ["0-24"] | |
622 // }, | |
623 | |
624 // configure events that can trigger a housekeeping processing | |
625 "Triggers" : { | |
626 "StorageCompressionChange": true, | |
627 "MainDicomTagsChange": true, | |
628 "UnnecessaryDicomAsJsonFiles": true | |
629 } | |
630 | |
631 } | |
632 } | |
633 */ | |
634 | |
635 | |
636 globalPropertyId_ = housekeeper.GetIntegerValue("GlobalPropertyId", 1025); | |
637 force_ = housekeeper.GetBooleanValue("Force", false); | |
638 throttleDelay_ = housekeeper.GetUnsignedIntegerValue("ThrottleDelay", 5); | |
639 | |
640 if (housekeeper.GetJson().isMember("Triggers")) | |
641 { | |
642 triggerOnStorageCompressionChange_ = housekeeper.GetBooleanValue("StorageCompressionChange", true); | |
643 triggerOnMainDicomTagsChange_ = housekeeper.GetBooleanValue("MainDicomTagsChange", true); | |
644 triggerOnUnnecessaryDicomAsJsonFiles_ = housekeeper.GetBooleanValue("UnnecessaryDicomAsJsonFiles", true); | |
645 } | |
646 | |
647 if (housekeeper.GetJson().isMember("Schedule")) | |
648 { | |
649 runningPeriods_.load(housekeeper.GetJson()["Schedule"]); | |
650 } | |
651 | |
652 OrthancPluginRegisterOnChangeCallback(c, OnChangeCallback); | |
653 } | |
654 else | |
655 { | |
656 OrthancPlugins::LogWarning("Housekeeper plugin is disabled by the configuration file"); | |
657 } | |
658 | |
659 return 0; | |
660 } | |
661 | |
662 | |
663 ORTHANC_PLUGINS_API void OrthancPluginFinalize() | |
664 { | |
665 OrthancPlugins::LogWarning("Housekeeper plugin is finalizing"); | |
666 } | |
667 | |
668 | |
669 ORTHANC_PLUGINS_API const char* OrthancPluginGetName() | |
670 { | |
671 return "housekeeper"; | |
672 } | |
673 | |
674 | |
675 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion() | |
676 { | |
677 return DB_OPTIMIZER_VERSION; | |
678 } | |
679 } |