FreeFOAM The Cross-Platform CFD Toolkit
JobInfo.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "JobInfo.H"
27 #include <OpenFOAM/OSspecific.H>
28 #include <OpenFOAM/clock.H>
29 #include <OpenFOAM/OFstream.H>
30 #include <OpenFOAM/Pstream.H>
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
36 
37 
38 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
39 
40 // Null constructor
42 :
43  runningJobPath_(),
44  finishedJobPath_(),
45  cpuTime_()
46 {
47  name() = "JobInfo";
48 
50  {
51  string baseDir;
52  if(env("FREEFOAM_JOB_DIR"))
53  {
54  baseDir = getEnv("FREEFOAM_JOB_DIR");
55  }
56  else if(env("FOAM_JOB_DIR"))
57  {
58  baseDir = getEnv("FOAM_JOB_DIR");
59  }
60  else if(debug::controlDict().found("foamJobDir"))
61  {
62  debug::controlDict().lookup("foamJobDir") >> baseDir;
63  }
64  else
65  {
66  baseDir = home()/".FreeFOAM"/"jobControl";
67  }
68 
69  string jobFile = hostName() + '.' + Foam::name(pid());
70 
71  fileName runningDir(baseDir/"runningJobs");
72  fileName finishedDir(baseDir/"finishedJobs");
73 
74  runningJobPath_ = runningDir/jobFile;
75  finishedJobPath_ = finishedDir/jobFile;
76 
77  if (baseDir.empty())
78  {
79  FatalErrorIn("JobInfo::JobInfo()")
80  << "Cannot get JobInfo directory $FOAM_JOB_DIR"
82  }
83 
84  if (!isDir(runningDir) && !mkDir(runningDir))
85  {
86  FatalErrorIn("JobInfo::JobInfo()")
87  << "Cannot make JobInfo directory " << runningDir
89  }
90 
91  if (!isDir(finishedDir) && !mkDir(finishedDir))
92  {
93  FatalErrorIn("JobInfo::JobInfo()")
94  << "Cannot make JobInfo directory " << finishedDir
96  }
97  }
98 
99  constructed = true;
100 }
101 
102 
103 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
104 
106 {
107  if (writeJobInfo && constructed && Pstream::master())
108  {
109  mv(runningJobPath_, finishedJobPath_);
110  }
111 
112  constructed = false;
113 }
114 
115 
116 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
117 
118 bool Foam::JobInfo::write(Ostream& os) const
119 {
120  if (writeJobInfo && Pstream::master())
121  {
122  if (os.good())
123  {
124  dictionary::write(os, false);
125  return true;
126  }
127  else
128  {
129  return false;
130  }
131  }
132  else
133  {
134  return true;
135  }
136 }
137 
138 
140 {
141  if (writeJobInfo && Pstream::master())
142  {
143  if (!write(OFstream(runningJobPath_)()))
144  {
145  FatalErrorIn("JobInfo::write() const")
146  << "Failed to write to JobInfo file "
147  << runningJobPath_
149  }
150  }
151 }
152 
153 
154 void Foam::JobInfo::end(const word& terminationType)
155 {
156  if (writeJobInfo && constructed && Pstream::master())
157  {
158  add("cpuTime", cpuTime_.elapsedCpuTime());
159  add("endDate", clock::date());
160  add("endTime", clock::clockTime());
161 
162  if (!found("termination"))
163  {
164  add("termination", terminationType);
165  }
166 
167  rm(runningJobPath_);
168  write(OFstream(finishedJobPath_)());
169  }
170 
171  constructed = false;
172 }
173 
174 
176 {
177  end("normal");
178 }
179 
180 
182 {
183  end("exit");
184 }
185 
186 
188 {
189  end("abort");
190 }
191 
192 
194 {
195  if (writeJobInfo && constructed && Pstream::master())
196  {
197  mv(runningJobPath_, finishedJobPath_);
198  }
199 
200  constructed = false;
201 }
202 
203 
204 // ************************ vim: set sw=4 sts=4 et: ************************ //