/* clearjobs.p */

/* Copyright RSA, The Security Division of EMC 2006, all rights reserved */

/* This 4GL script will attempt to clear the status
 * of all scheduled jobs, setting them to NOT RUNNING.
 * This may adversely affect your environment if there
 * are jobs that really are running.
 *
 * This script is unable to affect jobs that are locked 
 * by the database.
 *
 * All configuration is controlled by editing this file (the CONFIGURATION section,
 * only).
 */




/***************************************************\
|* CONFIGURATION                                   *|
|*                                                 *|
|* Edit settings in this section to change how this*|
|* script runs                                     *|
\***************************************************/


/**********************************************
 * If you want to send the output of the script
 * to a file, set outputToFile to TRUE, and 
 * filename to the full path of the file.  
 *
 * If you are running on Windows, make
 * sure all backslashes are double-backslashes
 * (i.e. c:\\ace).  If you are running on Unix,
 * use single forward slashes (/tmp/tokens.txt).
 *
 * If outputToFile is set to FALSE, all 
 * messages will be displayed as popups.
 */
DEFINE VARIABLE outputToFile AS LOGICAL INITIAL TRUE.

&IF OPSYS = "WIN32" &THEN
DEFINE VARIABLE filename AS CHARACTER INITIAL "c:\\clearjobs.txt".
&ELSE
DEFINE VARIABLE filename AS CHARACTER INITIAL "/tmp/clearjobs.txt".
&ENDIF.


/**********************************************
 * If you want to not actually do any work,
 * and just list the job statuses, set this 
 * to TRUE.
 */
DEFINE VARIABLE justListJobs AS LOGICAL INITIAL FALSE.



/**********************************************
 * This variable controls the text that will
 * be prepended to the last job's status.
 */
DEFINE VARIABLE statusUpdatedString AS CHARACTER INITIAL
  "THIS JOB WAS FORCIBLY RESET TO A STATE OF NOT RUNNING: ".


/**********************************************
 * Do not edit anything below this line       *
 **********************************************/

DEFINE BUFFER job FOR SDSchedJob.
DEFINE VARIABLE statusAlreadyUpdated AS INTEGER INITIAL 0 NO-UNDO.
DEFINE VARIABLE statusString AS CHARACTER INITIAL "" NO-UNDO.
DEFINE VARIABLE EXIT_STATUS_RUNNING AS INTEGER INITIAL 2147483647 NO-UNDO. /* 7FFFFFFF */
DEFINE VARIABLE EXIT_STATUS_NOT_RUN AS INTEGER INITIAL 1879048191 NO-UNDO. /* 6FFFFFFF */
DEFINE VARIABLE EXIT_STATUS_DELETING AS INTEGER INITIAL 1610612735 NO-UNDO. /* 5FFFFFFF */



IF outputToFile = TRUE THEN
DO:
   OUTPUT TO VALUE(filename) APPEND.
END.

MESSAGE "Starting clearjobs.p".

IF justListJobs = FALSE THEN
DO:
   MESSAGE "Will attempt to clear the status of all scheduled jobs.".
END.


FOR EACH job WHERE job.chJobClass = "ldapsync" EXCLUSIVE-LOCK:
   
   IF NOT AVAILABLE(job) THEN
   DO:
      MESSAGE job.chJobName " is not available.  Unable to clear status.".
      NEXT.
   END.

   IF LOCKED(job) THEN
   DO:
      MESSAGE job.chJobName " is locked by the database.  Unable to clear status".
      NEXT.
   END.
   
   if( job.iExitStatus = EXIT_STATUS_NOT_RUN ) THEN
   DO:
     MESSAGE "job " job.chJobName " was never run.  Skipping it.".
     NEXT.
   END.

   IF( job.iExitStatus = EXIT_STATUS_DELETING ) THEN
   DO:
     MESSAGE "job " job.chJobName " is being deleted.  Skipping it.".
   END.

   IF( job.iExitStatus <> EXIT_STATUS_RUNNING ) THEN
   DO:
     MESSAGE "job " + job.chJobName + " already exited with status " 
             + STRING(job.iExitStatus).
   END.

   IF justListJobs = TRUE THEN
   DO:
      MESSAGE "Found job: " job.chJobName " in running state.".
      NEXT.
   END.

   MESSAGE "Found job: " job.chJobName " in running state. Resetting it".

   /* Everything seems okay, so update the job and its status */
   job.iExitStatus = -1.
   
   statusAlreadyUpdated = INDEX(job.chSummary, statusUpdatedString).
   IF ( statusAlreadyUpdated = 0 ) THEN
   DO:
      job.chSummary = statusUpdatedString + job.chSummary.
   END.
END.
