comparison OrthancServer/Sources/LuaScripting.cpp @ 5410:16cbfefa15e9

Solved a deadlock related to the Job Engine events and plugins
author Alain Mazy <am@osimis.io>
date Tue, 07 Nov 2023 12:52:37 +0100
parents 0ea402b4d901
children 9ffd6d18daf3
comparison
equal deleted inserted replaced
5409:68231ca4363a 5410:16cbfefa15e9
237 operations.CallLua(that, name); 237 operations.CallLua(that, name);
238 } 238 }
239 }; 239 };
240 240
241 241
242 class LuaScripting::JobEvent : public LuaScripting::IEvent 242 class LuaScripting::LuaJobEvent : public LuaScripting::IEvent
243 { 243 {
244 private:
245 JobEvent event_;
246
244 public: 247 public:
245 enum Type 248 LuaJobEvent(const JobEvent& event) :
246 { 249 event_(event)
247 Type_Failure,
248 Type_Submitted,
249 Type_Success
250 };
251
252 private:
253 Type type_;
254 std::string jobId_;
255
256 public:
257 JobEvent(Type type,
258 const std::string& jobId) :
259 type_(type),
260 jobId_(jobId)
261 { 250 {
262 } 251 }
263 252
264 virtual void Apply(LuaScripting& that) ORTHANC_OVERRIDE 253 virtual void Apply(LuaScripting& that) ORTHANC_OVERRIDE
265 { 254 {
266 std::string functionName; 255 std::string functionName;
267 256
268 switch (type_) 257 switch (event_.GetEventType())
269 { 258 {
270 case Type_Failure: 259 case JobEventType_Failure:
271 functionName = "OnJobFailure"; 260 functionName = "OnJobFailure";
272 break; 261 break;
273 262
274 case Type_Submitted: 263 case JobEventType_Submitted:
275 functionName = "OnJobSubmitted"; 264 functionName = "OnJobSubmitted";
276 break; 265 break;
277 266
278 case Type_Success: 267 case JobEventType_Success:
279 functionName = "OnJobSuccess"; 268 functionName = "OnJobSuccess";
280 break; 269 break;
281 270
282 default: 271 default:
283 throw OrthancException(ErrorCode_InternalError); 272 throw OrthancException(ErrorCode_InternalError);
287 LuaScripting::Lock lock(that); 276 LuaScripting::Lock lock(that);
288 277
289 if (lock.GetLua().IsExistingFunction(functionName.c_str())) 278 if (lock.GetLua().IsExistingFunction(functionName.c_str()))
290 { 279 {
291 LuaFunctionCall call(lock.GetLua(), functionName.c_str()); 280 LuaFunctionCall call(lock.GetLua(), functionName.c_str());
292 call.PushString(jobId_); 281 call.PushString(event_.GetJobId());
293 call.Execute(); 282 call.Execute();
294 } 283 }
295 } 284 }
296 } 285 }
297 }; 286 };
1054 lock.GetLua().Execute(script); 1043 lock.GetLua().Execute(script);
1055 } 1044 }
1056 } 1045 }
1057 1046
1058 1047
1059 void LuaScripting::SignalJobSubmitted(const std::string& jobId) 1048 void LuaScripting::SignalJobEvent(const JobEvent& event)
1060 { 1049 {
1061 pendingEvents_.Enqueue(new JobEvent(JobEvent::Type_Submitted, jobId)); 1050 // Lua has its own event thread and queue to dissociate it completely from the main JobEventsThread
1062 } 1051 pendingEvents_.Enqueue(new LuaJobEvent(event));
1063
1064
1065 void LuaScripting::SignalJobSuccess(const std::string& jobId)
1066 {
1067 pendingEvents_.Enqueue(new JobEvent(JobEvent::Type_Success, jobId));
1068 }
1069
1070
1071 void LuaScripting::SignalJobFailure(const std::string& jobId)
1072 {
1073 pendingEvents_.Enqueue(new JobEvent(JobEvent::Type_Failure, jobId));
1074 } 1052 }
1075 } 1053 }