Merge branch 'master' of ssh://git.code.sf.net/p/foam-extend/foam-extend-3.2
[foam-extend-3.2.git] / src / foam / global / JobInfo / JobInfo.C
blob259c57be8b20ac421686e0dd0f4c52c47b231c85
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation, either version 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "JobInfo.H"
27 #include "OSspecific.H"
28 #include "clock.H"
29 #include "OFstream.H"
30 #include "Pstream.H"
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 Foam::debug::infoSwitch
35 Foam::JobInfo::writeJobInfo
37     "writeJobInfo",
38     0
41 Foam::JobInfo Foam::jobInfo;
44 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
46 // Null constructor
47 Foam::JobInfo::JobInfo()
49     runningJobPath_(),
50     finishedJobPath_(),
51     cpuTime_()
53     name() = "JobInfo";
55     if (writeJobInfo && Pstream::master())
56     {
57         string baseDir = getEnv("FOAM_JOB_DIR");
58         string jobFile = hostName() + '.' + Foam::name(pid());
60         fileName runningDir(baseDir/"runningJobs");
61         fileName finishedDir(baseDir/"finishedJobs");
63         runningJobPath_  = runningDir/jobFile;
64         finishedJobPath_ = finishedDir/jobFile;
66         if (baseDir.empty())
67         {
68             FatalErrorIn("JobInfo::JobInfo()")
69                 << "Cannot get JobInfo directory $FOAM_JOB_DIR"
70                 << Foam::exit(FatalError);
71         }
73         if (!isDir(runningDir) && !mkDir(runningDir))
74         {
75             FatalErrorIn("JobInfo::JobInfo()")
76                 << "Cannot make JobInfo directory " << runningDir
77                 << Foam::exit(FatalError);
78         }
80         if (!isDir(finishedDir) && !mkDir(finishedDir))
81         {
82             FatalErrorIn("JobInfo::JobInfo()")
83                 << "Cannot make JobInfo directory " << finishedDir
84                 << Foam::exit(FatalError);
85         }
86     }
88     constructed = true;
92 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
94 Foam::JobInfo::~JobInfo()
96     if (writeJobInfo && constructed && Pstream::master())
97     {
98         mv(runningJobPath_, finishedJobPath_);
99     }
101     constructed = false;
105 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
107 bool Foam::JobInfo::write(Ostream& os) const
109     if (writeJobInfo && Pstream::master())
110     {
111         if (os.good())
112         {
113             dictionary::write(os, false);
114             return true;
115         }
116         else
117         {
118             return false;
119         }
120     }
121     else
122     {
123         return true;
124     }
128 void Foam::JobInfo::write() const
130     if (writeJobInfo && Pstream::master())
131     {
132         if (!write(OFstream(runningJobPath_)()))
133         {
134             FatalErrorIn("JobInfo::write() const")
135                 << "Failed to write to JobInfo file "
136                 << runningJobPath_
137                 << Foam::exit(FatalError);
138         }
139     }
143 void Foam::JobInfo::end(const word& terminationType)
145     if (writeJobInfo && constructed && Pstream::master())
146     {
147         add("cpuTime", cpuTime_.elapsedCpuTime());
148         add("endDate", clock::date());
149         add("endTime", clock::clockTime());
151         if (!found("termination"))
152         {
153             add("termination", terminationType);
154         }
156         rm(runningJobPath_);
157         write(OFstream(finishedJobPath_)());
158     }
160     constructed = false;
164 void Foam::JobInfo::end()
166     end("normal");
170 void Foam::JobInfo::exit()
172     end("exit");
176 void Foam::JobInfo::abort()
178     end("abort");
182 void Foam::JobInfo::signalEnd() const
184     if (writeJobInfo && constructed && Pstream::master())
185     {
186         mv(runningJobPath_, finishedJobPath_);
187     }
189     constructed = false;
193 // ************************************************************************* //