comparison OrthancServer/Sources/LuaScripting.cpp @ 5027:06ffe03d008a

merge
author Alain Mazy <am@osimis.io>
date Tue, 21 Jun 2022 11:00:14 +0200
parents bafef43b6f37 c8c98389080f
children 0ea402b4d901
comparison
equal deleted inserted replaced
5026:bafef43b6f37 5027:06ffe03d008a
32 #include "../../OrthancFramework/Sources/Logging.h" 32 #include "../../OrthancFramework/Sources/Logging.h"
33 #include "../../OrthancFramework/Sources/Lua/LuaFunctionCall.h" 33 #include "../../OrthancFramework/Sources/Lua/LuaFunctionCall.h"
34 34
35 #include <OrthancServerResources.h> 35 #include <OrthancServerResources.h>
36 36
37 static const char* ON_HEART_BEAT = "OnHeartBeat";
38
37 39
38 namespace Orthanc 40 namespace Orthanc
39 { 41 {
40 class LuaScripting::IEvent : public IDynamicObject 42 class LuaScripting::IEvent : public IDynamicObject
41 { 43 {
783 } 785 }
784 } 786 }
785 787
786 void LuaScripting::HeartBeatThread(LuaScripting* that) 788 void LuaScripting::HeartBeatThread(LuaScripting* that)
787 { 789 {
788 static const boost::posix_time::time_duration PERIODICITY = 790 static const unsigned int GRANULARITY = 100; // In milliseconds
791
792 const boost::posix_time::time_duration PERIODICITY =
789 boost::posix_time::seconds(that->heartBeatPeriod_); 793 boost::posix_time::seconds(that->heartBeatPeriod_);
790
791 unsigned int sleepDelay = 100;
792 794
793 boost::posix_time::ptime next = 795 boost::posix_time::ptime next =
794 boost::posix_time::microsec_clock::universal_time() + PERIODICITY; 796 boost::posix_time::microsec_clock::universal_time() + PERIODICITY;
795 797
796 bool shouldStop = false; 798 bool shouldStop = false;
797 799
798 while (!shouldStop) 800 while (!shouldStop)
799 { 801 {
800 boost::this_thread::sleep(boost::posix_time::milliseconds(sleepDelay)); 802 boost::this_thread::sleep(boost::posix_time::milliseconds(GRANULARITY));
801 803
802 if (boost::posix_time::microsec_clock::universal_time() >= next) 804 if (boost::posix_time::microsec_clock::universal_time() >= next)
803 { 805 {
804 LuaScripting::Lock lock(*that); 806 LuaScripting::Lock lock(*that);
805 807
806 if (lock.GetLua().IsExistingFunction("OnHeartBeat")) 808 if (lock.GetLua().IsExistingFunction(ON_HEART_BEAT))
807 { 809 {
808 LuaFunctionCall call(lock.GetLua(), "OnHeartBeat"); 810 LuaFunctionCall call(lock.GetLua(), ON_HEART_BEAT);
809 call.Execute(); 811 call.Execute();
810 } 812 }
811 813
812 next = boost::posix_time::microsec_clock::universal_time() + PERIODICITY; 814 next = boost::posix_time::microsec_clock::universal_time() + PERIODICITY;
813 } 815 }
814 816
815 boost::recursive_mutex::scoped_lock lock(that->mutex_); 817 {
816 shouldStop = that->state_ == State_Done; 818 boost::recursive_mutex::scoped_lock lock(that->mutex_);
817 } 819 shouldStop = (that->state_ == State_Done);
818 820 }
821 }
819 } 822 }
820 823
821 void LuaScripting::EventThread(LuaScripting* that) 824 void LuaScripting::EventThread(LuaScripting* that)
822 { 825 {
823 for (;;) 826 for (;;)
865 LOG(INFO) << "Starting the Lua engine"; 868 LOG(INFO) << "Starting the Lua engine";
866 eventThread_ = boost::thread(EventThread, this); 869 eventThread_ = boost::thread(EventThread, this);
867 870
868 LuaScripting::Lock luaLock(*this); 871 LuaScripting::Lock luaLock(*this);
869 872
870 if (heartBeatPeriod_ > 0 && luaLock.GetLua().IsExistingFunction("OnHeartBeat")) 873 if (heartBeatPeriod_ > 0 && luaLock.GetLua().IsExistingFunction(ON_HEART_BEAT))
871 { 874 {
872 LOG(INFO) << "Starting the Lua HeartBeat thread with a period of " << heartBeatPeriod_ << " seconds"; 875 LOG(INFO) << "Starting the Lua HeartBeat thread with a period of " << heartBeatPeriod_ << " seconds";
873 heartBeatThread_ = boost::thread(HeartBeatThread, this); 876 heartBeatThread_ = boost::thread(HeartBeatThread, this);
874 } 877 }
875 state_ = State_Running; 878 state_ = State_Running;