Salome HOME
Error management when a job is created with wrong parameters.
[tools/ydefx.git] / src / cpp / Launcher.hxx
1 // Copyright (C) 2019  EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #ifndef YDEFX_LAUNCHER_H
20 #define YDEFX_LAUNCHER_H
21
22 #include "TMonoPyJob.hxx"
23
24 namespace ydefx
25 {
26 class Job;
27 class Launcher
28 {
29 public:
30   Launcher():_lastError(){}
31   const std::string& lastError()const{return _lastError;}
32
33   /*!
34    * Create and submit a new job.
35    * Return nullptr in case of failure. Check the error with lastError().
36    */
37   template <class ...Ts>
38   Job* submitMonoPyJob(const PyStudyFunction& fnScript,
39                        Sample<Ts...>& sample,
40                        const JobParametersProxy& params);
41
42   /*!
43    * Connect to an already created job.
44    * Return nullptr in case of failure. Check the error with lastError().
45    */
46   template <class ...Ts>
47   Job* connectJob(const std::string& jobDump, Sample<Ts...>& sample);
48
49   template <class ...Ts>
50   Job* submitMultiPyJob(const PyStudyFunction& fn,
51                         Sample<Ts...>& sample,
52                         const JobParametersProxy& params);
53
54 private:
55   std::string _lastError;
56 };
57
58 ////////////////////////////////////////////////////////////////////////////////
59 // Template implementations
60 ////////////////////////////////////////////////////////////////////////////////
61
62 template <class ...Ts>
63 Job* Launcher::submitMonoPyJob(const PyStudyFunction& fnScript,
64                     Sample<Ts...>& sample,
65                     const JobParametersProxy& params)
66 {
67   Job* result = nullptr;
68   _lastError = "";
69   try
70   {
71     result = new TMonoPyJob<Ts...>(fnScript, sample, params);
72   }
73   catch(std::exception& e)
74   {
75     if(result != nullptr)
76       delete result;
77     result = nullptr;
78     _lastError = e.what();
79     return result;
80   }
81
82   if(!result->lastError().empty())
83   {
84     _lastError  = result->lastError();
85     delete result;
86     result = nullptr;
87     return result;
88   }
89
90   if(!result->launch())
91   {
92     _lastError = "Failed to submit job.\n";
93     _lastError  += result->lastError();
94     delete result;
95     result = nullptr;
96   }
97   return result;
98 }
99
100 template <class ...Ts>
101 Job* Launcher::connectJob(const std::string& jobDump,
102                       Sample<Ts...>& sample)
103 {
104   Job* result = nullptr;
105   _lastError = "";
106   try
107   {
108     result = new TMonoPyJob<Ts...>(jobDump, sample);
109   }
110   catch(std::exception& e)
111   {
112     if(result != nullptr)
113       delete result;
114     result = nullptr;
115     _lastError = e.what();
116   }
117
118   return result;
119 }
120
121 template <class ...Ts>
122 Job* Launcher::submitMultiPyJob(const PyStudyFunction& fn,
123                       Sample<Ts...>& sample,
124                       const JobParametersProxy& params)
125 {
126   // TODO
127   _lastError = "Not implemented!";
128   return nullptr;
129 }
130
131 }
132 #endif // YDEFX_LAUNCHER_H