Mercurial > hg > orthanc
comparison OrthancServer/LuaScripting.cpp @ 2675:3fc310ceb6d4 jobs
lua callbacks for jobs
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 11 Jun 2018 20:26:24 +0200 |
parents | 389d050a2e66 |
children | 815e30657dad |
comparison
equal
deleted
inserted
replaced
2674:373b44af938f | 2675:3fc310ceb6d4 |
---|---|
185 } | 185 } |
186 } | 186 } |
187 }; | 187 }; |
188 | 188 |
189 | 189 |
190 class LuaScripting::JobEvent : public LuaScripting::IEvent | |
191 { | |
192 public: | |
193 enum Type | |
194 { | |
195 Type_Failure, | |
196 Type_Submitted, | |
197 Type_Success | |
198 }; | |
199 | |
200 private: | |
201 Type type_; | |
202 std::string jobId_; | |
203 | |
204 public: | |
205 JobEvent(Type type, | |
206 const std::string& jobId) : | |
207 type_(type), | |
208 jobId_(jobId) | |
209 { | |
210 } | |
211 | |
212 virtual void Apply(LuaScripting& that) | |
213 { | |
214 std::string functionName; | |
215 | |
216 switch (type_) | |
217 { | |
218 case Type_Failure: | |
219 functionName = "OnJobFailure"; | |
220 break; | |
221 | |
222 case Type_Submitted: | |
223 functionName = "OnJobSubmitted"; | |
224 break; | |
225 | |
226 case Type_Success: | |
227 functionName = "OnJobSuccess"; | |
228 break; | |
229 | |
230 default: | |
231 throw OrthancException(ErrorCode_InternalError); | |
232 } | |
233 | |
234 { | |
235 LuaScripting::Lock lock(that); | |
236 | |
237 if (lock.GetLua().IsExistingFunction(functionName.c_str())) | |
238 { | |
239 LuaFunctionCall call(lock.GetLua(), functionName.c_str()); | |
240 call.PushString(jobId_); | |
241 call.Execute(); | |
242 } | |
243 } | |
244 } | |
245 }; | |
246 | |
247 | |
190 ServerContext* LuaScripting::GetServerContext(lua_State *state) | 248 ServerContext* LuaScripting::GetServerContext(lua_State *state) |
191 { | 249 { |
192 const void* value = LuaContext::GetGlobalVariable(state, "_ServerContext"); | 250 const void* value = LuaContext::GetGlobalVariable(state, "_ServerContext"); |
193 return const_cast<ServerContext*>(reinterpret_cast<const ServerContext*>(value)); | 251 return const_cast<ServerContext*>(reinterpret_cast<const ServerContext*>(value)); |
194 } | 252 } |
691 SystemToolbox::ReadFile(script, path); | 749 SystemToolbox::ReadFile(script, path); |
692 | 750 |
693 lock.GetLua().Execute(script); | 751 lock.GetLua().Execute(script); |
694 } | 752 } |
695 } | 753 } |
754 | |
755 | |
756 void LuaScripting::SignalJobSubmitted(const std::string& jobId) | |
757 { | |
758 pendingEvents_.Enqueue(new JobEvent(JobEvent::Type_Submitted, jobId)); | |
759 } | |
760 | |
761 | |
762 void LuaScripting::SignalJobSuccess(const std::string& jobId) | |
763 { | |
764 pendingEvents_.Enqueue(new JobEvent(JobEvent::Type_Success, jobId)); | |
765 } | |
766 | |
767 | |
768 void LuaScripting::SignalJobFailure(const std::string& jobId) | |
769 { | |
770 pendingEvents_.Enqueue(new JobEvent(JobEvent::Type_Failure, jobId)); | |
771 } | |
696 } | 772 } |