From af49eaf625de186bb3264e0acde16591aa5bd097 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@Germany>
Date: Mon, 3 Apr 2017 08:28:15 +0200
Subject: [PATCH] ENH: default jobControl now under the
 '~/.OpenFOAM/jobControl' directory

- this implies that jobControl is a user-resource for OpenFOAM.
  It was previously located under $WM_PROJECT_INST_DIR/jobControl,
  but few users will have write access there.

- an unset FOAM_JOB_DIR variable is treated as "~/.OpenFOAM/jobControl",
  which can partially reduce environment clutter.

- provide argList::noJobInfo() to conveniently suppress job-info on an
  individual basis for short-running utilities (eg, foamListTimes) to
  avoid unneeded clutter.
---
 .../foamDictionary/foamDictionary.C           |   1 +
 .../foamListTimes/foamListTimes.C             |   1 +
 bin/foamCheckJobs                             |  12 +-
 bin/foamPrintJobs                             |   8 +-
 etc/bashrc                                    |   4 +-
 etc/config.csh/compiler                       |   2 +-
 etc/config.csh/settings                       |   6 +-
 etc/config.sh/compiler                        |   2 +-
 etc/config.sh/settings                        |   6 +-
 etc/cshrc                                     |   4 +-
 src/OpenFOAM/global/JobInfo/JobInfo.C         | 141 +++++++++---------
 src/OpenFOAM/global/JobInfo/JobInfo.H         |  35 +++--
 src/OpenFOAM/global/argList/argList.C         |   8 +-
 src/OpenFOAM/global/argList/argList.H         |   5 +-
 14 files changed, 123 insertions(+), 112 deletions(-)

diff --git a/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C b/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C
index ce7d047f51d..f28c0e12890 100644
--- a/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C
+++ b/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C
@@ -241,6 +241,7 @@ int main(int argc, char *argv[])
     argList::addNote("manipulates dictionaries");
 
     argList::noBanner();
+    argList::noJobInfo();
     argList::validArgs.append("dictionary");
     argList::addBoolOption("keywords", "list keywords");
     argList::addOption("entry", "name", "report/select the named entry");
diff --git a/applications/utilities/miscellaneous/foamListTimes/foamListTimes.C b/applications/utilities/miscellaneous/foamListTimes/foamListTimes.C
index 48be875408d..6313dbbad91 100644
--- a/applications/utilities/miscellaneous/foamListTimes/foamListTimes.C
+++ b/applications/utilities/miscellaneous/foamListTimes/foamListTimes.C
@@ -59,6 +59,7 @@ int main(int argc, char *argv[])
     timeSelector::addOptions(true, true);
     argList::noBanner();
     argList::noParallel();
+    argList::noJobInfo();
     argList::addBoolOption
     (
         "processor",
diff --git a/bin/foamCheckJobs b/bin/foamCheckJobs
index 02923b92897..e4142df73b2 100755
--- a/bin/foamCheckJobs
+++ b/bin/foamCheckJobs
@@ -36,7 +36,6 @@
 #     file yet. Not supported by this script yet)
 #
 #------------------------------------------------------------------------------
-
 Script=${0##*/}
 
 #-------------------------------------------------------------------------------
@@ -52,6 +51,8 @@ TMPFILE=/tmp/${Script}$$.tmp
 MACHDIR=$HOME/.OpenFOAM/${Script}
 DEFSTATEFILE=$HOME/.OpenFOAM/foamCheckJobs.out
 
+# The default is "~/.OpenFOAM/jobControl"
+: ${FOAM_JOB_DIR:=$HOME/.OpenFOAM/jobControl}
 
 if [ `uname -s` = Linux ]
 then
@@ -77,7 +78,7 @@ The output from checking all running jobs is collected in an optional
 file.
 
 FILES:
-    \$FOAM_JOB_DIR/runningJobs    locks for running  processes
+    \$FOAM_JOB_DIR/runningJobs   locks for running  processes
                   /finishedJobs  locks for finished processes
 USAGE
     exit 1
@@ -196,13 +197,6 @@ fi
 
 #- Check a few things
 
-if [ ! "$FOAM_JOB_DIR" ]
-then
-    echo "$Script : FOAM_JOB_DIR environment variable not set."
-    echo
-    exit 1
-fi
-
 if [ ! -d "$FOAM_JOB_DIR" ]
 then
     echo "$Script : directory does not exist."
diff --git a/bin/foamPrintJobs b/bin/foamPrintJobs
index 7213e95e1f7..58882edab9d 100755
--- a/bin/foamPrintJobs
+++ b/bin/foamPrintJobs
@@ -34,6 +34,8 @@ Script=${0##*/}
 JOBSTRING='%4s %8s %20s %10s %8s %4s %12s %12s %20s\n'
 DEFSTATEFILE=$HOME/.OpenFOAM/foamCheckJobs.out
 
+# The default is "~/.OpenFOAM/jobControl"
+: ${FOAM_JOB_DIR:=$HOME/.OpenFOAM/jobControl}
 
 #-------------------------------------------------------------------------------
 #
@@ -120,12 +122,6 @@ fi
 
 #- Check a few things
 
-if [ ! "$FOAM_JOB_DIR" ]
-then
-    echo "$Script : FOAM_JOB_DIR environment variable not set."
-    echo
-    exit 1
-fi
 if [ ! -d "$FOAM_JOB_DIR" ]
 then
     echo "$Script : directory does not exist."
diff --git a/etc/bashrc b/etc/bashrc
index a84c2508c53..741ca553000 100644
--- a/etc/bashrc
+++ b/etc/bashrc
@@ -25,8 +25,8 @@
 #     etc/bashrc
 #
 # Description
-#     Startup file for OpenFOAM
-#     Sourced from ~/.profile or ~/.bashrc
+#     Set the environment for OpenFOAM when using a POSIX shell.
+#     To be sourced manually or from the ~/.profile or ~/.bashrc files.
 #     Should be usable by any POSIX-compliant shell (eg, dash, ksh)
 #
 #------------------------------------------------------------------------------
diff --git a/etc/config.csh/compiler b/etc/config.csh/compiler
index f043508d85f..2f1f7f04801 100644
--- a/etc/config.csh/compiler
+++ b/etc/config.csh/compiler
@@ -25,7 +25,7 @@
 #     etc/config.csh/compiler
 #
 # Description
-#     Startup file for custom compiler versions for OpenFOAM
+#     Setup file for custom compiler versions for OpenFOAM
 #     Sourced from OpenFOAM-<VERSION>/etc/config.csh/settings
 #
 #------------------------------------------------------------------------------
diff --git a/etc/config.csh/settings b/etc/config.csh/settings
index fdf04a26156..4cbcefc0f0d 100644
--- a/etc/config.csh/settings
+++ b/etc/config.csh/settings
@@ -25,7 +25,7 @@
 #     etc/config.csh/settings
 #
 # Description
-#     Startup file for OpenFOAM
+#     Setup file for OpenFOAM
 #     Sourced from OpenFOAM-<VERSION>/etc/cshrc
 #
 #------------------------------------------------------------------------------
@@ -137,8 +137,8 @@ endsw
 
 #------------------------------------------------------------------------------
 
-# Location of the jobControl directory
-setenv FOAM_JOB_DIR $WM_PROJECT_INST_DIR/jobControl
+# Optional jobControl directory: unset is equivalent to ~/.OpenFOAM/jobControl
+# setenv FOAM_JOB_DIR "$HOME/.OpenFOAM/jobControl"
 
 # wmake configuration
 setenv WM_DIR $WM_PROJECT_DIR/wmake
diff --git a/etc/config.sh/compiler b/etc/config.sh/compiler
index 8e9122a8d5b..2235619f774 100644
--- a/etc/config.sh/compiler
+++ b/etc/config.sh/compiler
@@ -25,7 +25,7 @@
 #     etc/config.sh/compiler
 #
 # Description
-#     Startup file for custom compiler versions for OpenFOAM
+#     Setup file for custom compiler versions for OpenFOAM
 #     Sourced from OpenFOAM-<VERSION>/etc/config.sh/settings
 #
 #------------------------------------------------------------------------------
diff --git a/etc/config.sh/settings b/etc/config.sh/settings
index d7a596505df..77059d3f4c8 100644
--- a/etc/config.sh/settings
+++ b/etc/config.sh/settings
@@ -25,7 +25,7 @@
 #     etc/config.sh/settings
 #
 # Description
-#     Startup file for OpenFOAM
+#     Setup file for OpenFOAM
 #     Sourced from OpenFOAM-<VERSION>/etc/bashrc
 #
 #------------------------------------------------------------------------------
@@ -140,8 +140,8 @@ esac
 
 #------------------------------------------------------------------------------
 
-# Location of the jobControl directory
-export FOAM_JOB_DIR=$WM_PROJECT_INST_DIR/jobControl
+# Optional jobControl directory: unset is equivalent to ~/.OpenFOAM/jobControl
+# export FOAM_JOB_DIR="$HOME/.OpenFOAM/jobControl"
 
 # wmake configuration
 export WM_DIR=$WM_PROJECT_DIR/wmake
diff --git a/etc/cshrc b/etc/cshrc
index 5d79c0c8fde..a8278c526cc 100644
--- a/etc/cshrc
+++ b/etc/cshrc
@@ -25,8 +25,8 @@
 #     etc/cshrc
 #
 # Description
-#     Startup file for OpenFOAM
-#     Sourced from ~/.login or ~/.cshrc
+#     Set the environment for OpenFOAM when using a cshell.
+#     To be sourced manually or from the ~/.login or ~/.cshrc files.
 #
 #------------------------------------------------------------------------------
 
diff --git a/src/OpenFOAM/global/JobInfo/JobInfo.C b/src/OpenFOAM/global/JobInfo/JobInfo.C
index 7d17218841f..bc0dbd147a5 100644
--- a/src/OpenFOAM/global/JobInfo/JobInfo.C
+++ b/src/OpenFOAM/global/JobInfo/JobInfo.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -28,52 +28,97 @@ License
 #include "clock.H"
 #include "OFstream.H"
 #include "Pstream.H"
+#include "foamVersion.H"
 
 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
 
 bool Foam::JobInfo::writeJobInfo(Foam::debug::infoSwitch("writeJobInfo", 0));
 Foam::JobInfo Foam::jobInfo;
 
+// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
+
+bool Foam::JobInfo::write(Ostream& os) const
+{
+    if (writeJobInfo && Pstream::master())
+    {
+        if (os.good())
+        {
+            dictionary::write(os, false);
+            return true;
+        }
+        else
+        {
+            return false;
+        }
+    }
+    else
+    {
+        return true;
+    }
+}
+
+
+void Foam::JobInfo::end(const word& terminationType)
+{
+    if (writeJobInfo && constructed && Pstream::master())
+    {
+        add("cpuTime", cpuTime_.elapsedCpuTime());
+        add("endDate", clock::date());
+        add("endTime", clock::clockTime());
+
+        if (!found("termination"))
+        {
+            add("termination", terminationType);
+        }
+
+        Foam::rm(runningDir_/jobFileName_);
+        write(OFstream(finishedDir_/jobFileName_)());
+    }
+
+    constructed = false;
+}
+
 
 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
 
 Foam::JobInfo::JobInfo()
 :
-    runningJobPath_(),
-    finishedJobPath_(),
+    jobFileName_(),
+    runningDir_(),
+    finishedDir_(),
     cpuTime_()
 {
     name() = "JobInfo";
 
     if (writeJobInfo && Pstream::master())
     {
-        string baseDir = getEnv("FOAM_JOB_DIR");
-        string jobFile = hostName() + '.' + Foam::name(pid());
-
-        fileName runningDir(baseDir/"runningJobs");
-        fileName finishedDir(baseDir/"finishedJobs");
+        string jobDir = getEnv("FOAM_JOB_DIR");
+        if (jobDir.empty())
+        {
+            // Fallback: ~/.OpenFOAM/jobControl
+            jobDir = home()/WM_USER_RESOURCE_DIRNAME/"jobControl";
+        }
 
-        runningJobPath_  = runningDir/jobFile;
-        finishedJobPath_ = finishedDir/jobFile;
+        jobFileName_ = hostName() + '.' + Foam::name(pid());
+        runningDir_  = jobDir/"runningJobs";
+        finishedDir_ = jobDir/"finishedJobs";
 
-        if (baseDir.empty())
+        if (!isDir(jobDir) && !mkDir(jobDir))
         {
             FatalErrorInFunction
-                << "Cannot get JobInfo directory $FOAM_JOB_DIR"
+                << "No JobInfo directory:  FOAM_JOB_DIR=" << jobDir
                 << Foam::exit(FatalError);
         }
-
-        if (!isDir(runningDir) && !mkDir(runningDir))
+        if (!isDir(runningDir_) && !mkDir(runningDir_))
         {
             FatalErrorInFunction
-                << "Cannot make JobInfo directory " << runningDir
+                << "No JobInfo directory:  " << runningDir_
                 << Foam::exit(FatalError);
         }
-
-        if (!isDir(finishedDir) && !mkDir(finishedDir))
+        if (!isDir(finishedDir_) && !mkDir(finishedDir_))
         {
             FatalErrorInFunction
-                << "Cannot make JobInfo directory " << finishedDir
+                << "No JobInfo directory:  " << finishedDir_
                 << Foam::exit(FatalError);
         }
     }
@@ -86,74 +131,27 @@ Foam::JobInfo::JobInfo()
 
 Foam::JobInfo::~JobInfo()
 {
-    if (writeJobInfo && constructed && Pstream::master())
-    {
-        mv(runningJobPath_, finishedJobPath_);
-    }
-
-    constructed = false;
+    signalEnd();
 }
 
 
 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
 
-bool Foam::JobInfo::write(Ostream& os) const
-{
-    if (writeJobInfo && Pstream::master())
-    {
-        if (os.good())
-        {
-            dictionary::write(os, false);
-            return true;
-        }
-        else
-        {
-            return false;
-        }
-    }
-    else
-    {
-        return true;
-    }
-}
-
-
 void Foam::JobInfo::write() const
 {
-    if (writeJobInfo && Pstream::master())
+    if (writeJobInfo && constructed && Pstream::master())
     {
-        if (!write(OFstream(runningJobPath_)()))
+        const fileName output = runningDir_/jobFileName_;
+        if (!write(OFstream(output)()))
         {
             FatalErrorInFunction
-                << "Failed to write to JobInfo file "
-                << runningJobPath_
+                << "Failed to write to JobInfo file " << output
                 << Foam::exit(FatalError);
         }
     }
 }
 
 
-void Foam::JobInfo::end(const word& terminationType)
-{
-    if (writeJobInfo && constructed && Pstream::master())
-    {
-        add("cpuTime", cpuTime_.elapsedCpuTime());
-        add("endDate", clock::date());
-        add("endTime", clock::clockTime());
-
-        if (!found("termination"))
-        {
-            add("termination", terminationType);
-        }
-
-        rm(runningJobPath_);
-        write(OFstream(finishedJobPath_)());
-    }
-
-    constructed = false;
-}
-
-
 void Foam::JobInfo::end()
 {
     end("normal");
@@ -176,9 +174,8 @@ void Foam::JobInfo::signalEnd() const
 {
     if (writeJobInfo && constructed && Pstream::master())
     {
-        mv(runningJobPath_, finishedJobPath_);
+        Foam::mv(runningDir_/jobFileName_, finishedDir_/jobFileName_);
     }
-
     constructed = false;
 }
 
diff --git a/src/OpenFOAM/global/JobInfo/JobInfo.H b/src/OpenFOAM/global/JobInfo/JobInfo.H
index 5549af62739..f2b02e7023a 100644
--- a/src/OpenFOAM/global/JobInfo/JobInfo.H
+++ b/src/OpenFOAM/global/JobInfo/JobInfo.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -31,6 +31,8 @@ Description
       - $FOAM_JOB_DIR/runningJobs
       - $FOAM_JOB_DIR/finishedJobs
 
+    If FOAM_JOB_DIR is unset, defaults to ~/.OpenFOAM/jobControl
+
 SourceFiles
     JobInfo.C
 
@@ -57,21 +59,28 @@ class JobInfo
     public dictionary
 {
     // Private data
-        fileName runningJobPath_;
-        fileName finishedJobPath_;
+
+        //- The name of the job file
+        string   jobFileName_;
+        fileName runningDir_;
+        fileName finishedDir_;
         cpuTime  cpuTime_;
 
     // Private Member Functions
 
-        bool write(Ostream&) const;
+        bool write(Ostream& os) const;
         void end(const word& terminationType);
 
 
 public:
 
+    //- Global value for constructed job info
     static bool constructed;
+
+    //- Global value for writeJobInfo enabled
     static bool writeJobInfo;
 
+
     // Constructors
 
         //- Construct null
@@ -79,22 +88,26 @@ public:
 
 
     //- Destructor
+    //  Update job info and relocate the file from running to finished.
     ~JobInfo();
 
 
     // Member Functions
 
-        // Write
-
-            void write() const;
+        //- Write the job info to its file in the runningJobs directory
+        void write() const;
 
-            void end();
+        //- End with "termination=normal"
+        void end();
 
-            void exit();
+        //- End with "termination=exit"
+        void exit();
 
-            void abort();
+        //- End with "termination=abort"
+        void abort();
 
-            void signalEnd() const;
+        //- Update job info and relocate the file from running to finished.
+        void signalEnd() const;
 };
 
 
diff --git a/src/OpenFOAM/global/argList/argList.C b/src/OpenFOAM/global/argList/argList.C
index 11643ce1153..1139bbb8f16 100644
--- a/src/OpenFOAM/global/argList/argList.C
+++ b/src/OpenFOAM/global/argList/argList.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  | Copyright (C) 2015-2016 OpenCFD Ltd.
+     \\/     M anipulation  | Copyright (C) 2015-2017 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -173,6 +173,12 @@ void Foam::argList::noFunctionObjects(bool addWithOption)
 }
 
 
+void Foam::argList::noJobInfo()
+{
+    JobInfo::writeJobInfo = false;
+}
+
+
 void Foam::argList::noParallel()
 {
     removeOption("parallel");
diff --git a/src/OpenFOAM/global/argList/argList.H b/src/OpenFOAM/global/argList/argList.H
index 81612f2857e..f2635744536 100644
--- a/src/OpenFOAM/global/argList/argList.H
+++ b/src/OpenFOAM/global/argList/argList.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  | Copyright (C) 2016 OpenCFD Ltd.
+     \\/     M anipulation  | Copyright (C) 2016-2017 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -352,6 +352,9 @@ public:
             //  optionally adding a 'withFunctionObjects' option instead
             static void noFunctionObjects(bool addWithOption = false);
 
+            //- Suppress JobInfo, overriding controlDict setting
+            static void noJobInfo();
+
             //- Remove the parallel options
             static void noParallel();
 
-- 
GitLab