Mercurial > hg > orthanc
comparison OrthancServer/Sources/LuaScripting.cpp @ 5017:0d61efc6256c lua-heart-beat
Added Lua OnHeartBeat()
author | Alain Mazy <am@osimis.io> |
---|---|
date | Mon, 13 Jun 2022 18:54:49 +0200 |
parents | 309fb4f02704 |
children | eb8ca3403983 |
comparison
equal
deleted
inserted
replaced
5016:c89ffa13173e | 5017:0d61efc6256c |
---|---|
757 } | 757 } |
758 | 758 |
759 | 759 |
760 LuaScripting::LuaScripting(ServerContext& context) : | 760 LuaScripting::LuaScripting(ServerContext& context) : |
761 context_(context), | 761 context_(context), |
762 state_(State_Setup) | 762 state_(State_Setup), |
763 heartBeatPeriod_(0) | |
763 { | 764 { |
764 lua_.SetGlobalVariable("_ServerContext", &context); | 765 lua_.SetGlobalVariable("_ServerContext", &context); |
765 lua_.RegisterFunction("RestApiGet", RestApiGet); | 766 lua_.RegisterFunction("RestApiGet", RestApiGet); |
766 lua_.RegisterFunction("RestApiPost", RestApiPost); | 767 lua_.RegisterFunction("RestApiPost", RestApiPost); |
767 lua_.RegisterFunction("RestApiPut", RestApiPut); | 768 lua_.RegisterFunction("RestApiPut", RestApiPut); |
780 LOG(ERROR) << "INTERNAL ERROR: LuaScripting::Stop() should be invoked manually to avoid mess in the destruction order!"; | 781 LOG(ERROR) << "INTERNAL ERROR: LuaScripting::Stop() should be invoked manually to avoid mess in the destruction order!"; |
781 Stop(); | 782 Stop(); |
782 } | 783 } |
783 } | 784 } |
784 | 785 |
786 void LuaScripting::HeartBeatThread(LuaScripting* that) | |
787 { | |
788 static const boost::posix_time::time_duration PERIODICITY = | |
789 boost::posix_time::seconds(that->heartBeatPeriod_); | |
790 | |
791 unsigned int sleepDelay = 100; | |
792 | |
793 boost::posix_time::ptime next = | |
794 boost::posix_time::microsec_clock::universal_time() + PERIODICITY; | |
795 | |
796 while (that->state_ != State_Done) | |
797 { | |
798 boost::this_thread::sleep(boost::posix_time::milliseconds(sleepDelay)); | |
799 | |
800 if (that->state_ != State_Done && | |
801 boost::posix_time::microsec_clock::universal_time() >= next) | |
802 { | |
803 LuaScripting::Lock lock(*that); | |
804 | |
805 if (lock.GetLua().IsExistingFunction("OnHeartBeat")) | |
806 { | |
807 LuaFunctionCall call(lock.GetLua(), "OnHeartBeat"); | |
808 call.Execute(); | |
809 } | |
810 | |
811 next = boost::posix_time::microsec_clock::universal_time() + PERIODICITY; | |
812 } | |
813 } | |
814 | |
815 } | |
785 | 816 |
786 void LuaScripting::EventThread(LuaScripting* that) | 817 void LuaScripting::EventThread(LuaScripting* that) |
787 { | 818 { |
788 for (;;) | 819 for (;;) |
789 { | 820 { |
827 } | 858 } |
828 else | 859 else |
829 { | 860 { |
830 LOG(INFO) << "Starting the Lua engine"; | 861 LOG(INFO) << "Starting the Lua engine"; |
831 eventThread_ = boost::thread(EventThread, this); | 862 eventThread_ = boost::thread(EventThread, this); |
863 | |
864 LuaScripting::Lock lock(*this); | |
865 | |
866 if (heartBeatPeriod_ > 0 && lock.GetLua().IsExistingFunction("OnHeartBeat")) | |
867 { | |
868 LOG(INFO) << "Starting the Lua HeartBeat thread with a period of " << heartBeatPeriod_ << " seconds"; | |
869 heartBeatThread_ = boost::thread(HeartBeatThread, this); | |
870 } | |
832 state_ = State_Running; | 871 state_ = State_Running; |
833 } | 872 } |
834 } | 873 } |
835 | 874 |
836 | 875 |
851 | 890 |
852 if (eventThread_.joinable()) | 891 if (eventThread_.joinable()) |
853 { | 892 { |
854 LOG(INFO) << "Stopping the Lua engine"; | 893 LOG(INFO) << "Stopping the Lua engine"; |
855 eventThread_.join(); | 894 eventThread_.join(); |
895 if (heartBeatThread_.joinable()) | |
896 { | |
897 heartBeatThread_.join(); | |
898 } | |
856 LOG(INFO) << "The Lua engine has stopped"; | 899 LOG(INFO) << "The Lua engine has stopped"; |
857 } | 900 } |
858 } | 901 } |
859 | 902 |
860 | 903 |
987 lua_.Execute(command); | 1030 lua_.Execute(command); |
988 } | 1031 } |
989 | 1032 |
990 std::list<std::string> luaScripts; | 1033 std::list<std::string> luaScripts; |
991 configLock.GetConfiguration().GetListOfStringsParameter(luaScripts, "LuaScripts"); | 1034 configLock.GetConfiguration().GetListOfStringsParameter(luaScripts, "LuaScripts"); |
1035 heartBeatPeriod_ = configLock.GetConfiguration().GetIntegerParameter("LuaHeartBeatPeriod", 0); | |
992 | 1036 |
993 LuaScripting::Lock lock(*this); | 1037 LuaScripting::Lock lock(*this); |
994 | 1038 |
995 for (std::list<std::string>::const_iterator | 1039 for (std::list<std::string>::const_iterator |
996 it = luaScripts.begin(); it != luaScripts.end(); ++it) | 1040 it = luaScripts.begin(); it != luaScripts.end(); ++it) |