src/dird/ua_cmds.c
int wait_cmd(UAContext *ua, const char *cmd)
{
   JCR *jcr;
   int i;
   time_t stop_time = 0;

   /*
    * no args
    * Wait until no job is running
    */
   if (ua->argc == 1) {
      bmicrosleep(0, 200000);            /* let job actually start */
      for (bool running=true; running; ) {
         running = false;
         foreach_jcr(jcr) {
            if (!jcr->is_internal_job()) {
               running = true;
               break;
            }
         }
         endeach_jcr(jcr);

         if (running) {
            bmicrosleep(1, 0);
         }
      }
      return 1;
   }

   i = find_arg_with_value(ua, NT_("timeout"));
   if (i > 0 && ua->argv[i]) {
      stop_time = time(NULL) + str_to_int64(ua->argv[i]);
   }

   /* we have jobid, jobname or ujobid argument */

   uint32_t jobid = 0 ;

   if (!open_client_db(ua)) {
      ua->error_msg(_("ERR: Can't open db\n")) ;
      return 1;
   }

   for (int i=1; i<ua->argc; i++) {
      if (strcasecmp(ua->argk[i], "jobid") == 0) {
         if (!ua->argv[i]) {
            break;
         }
         jobid = str_to_int64(ua->argv[i]);
         break;
      } else if (strcasecmp(ua->argk[i], "jobname") == 0 ||
                 strcasecmp(ua->argk[i], "job") == 0) {
         if (!ua->argv[i]) {
            break;
         }
         jcr=get_jcr_by_partial_name(ua->argv[i]) ;
         if (jcr) {
            jobid = jcr->JobId ;
            free_jcr(jcr);
         }
         break;
      } else if (strcasecmp(ua->argk[i], "ujobid") == 0) {
         if (!ua->argv[i]) {
            break;
         }
         jcr=get_jcr_by_full_name(ua->argv[i]) ;
         if (jcr) {
            jobid = jcr->JobId ;
            free_jcr(jcr);
         }
         break;
      /* Wait for a mount request */
      } else if (strcasecmp(ua->argk[i], "mount") == 0) {
         for (bool waiting=false; !waiting; ) {
            foreach_jcr(jcr) {
               if (!jcr->is_internal_job() &&
                   (jcr->JobStatus == JS_WaitMedia || jcr->JobStatus == JS_WaitMount ||
                    jcr->SDJobStatus == JS_WaitMedia || jcr->SDJobStatus == JS_WaitMount))
               {
                  waiting = true;
                  break;
               }
            }
            endeach_jcr(jcr);
            if (waiting) {
               break;
            }
            if (stop_time && (time(NULL) >= stop_time)) {
               ua->warning_msg(_("Wait on mount timed out\n"));
               return 1;
            }
            bmicrosleep(1, 0);
         }
         return 1;
      }
   }

   if (jobid == 0) {
      ua->error_msg(_("ERR: Job was not found\n"));
      return 1 ;
   }

   /*
    * We wait the end of a specific job
    */

   bmicrosleep(0, 200000);            /* let job actually start */
   for (bool running=true; running; ) {
      running = false;

      jcr=get_jcr_by_id(jobid) ;

      if (jcr) {
         running = true ;
         free_jcr(jcr);
      }

      if (running) {
         bmicrosleep(1, 0);
      }
   }

   /*
    * We have to get JobStatus
    */

   int status ;
   char jobstatus = '?';        /* Unknown by default */
   char buf[256] ;

   bsnprintf(buf, sizeof(buf),
             "SELECT JobStatus FROM Job WHERE JobId='%i'", jobid);


   db_sql_query(ua->db, buf, status_handler, (void *)&jobstatus);

   switch (jobstatus) {
   case JS_Error:
      status = 1 ;         /* Warning */
      break;

   case JS_Incomplete:
   case JS_FatalError:
   case JS_ErrorTerminated:
   case JS_Canceled:
      status = 2 ;         /* Critical */
      break;

   case JS_Warnings:
   case JS_Terminated:
      status = 0 ;         /* Ok */
      break;

   default:
      status = 3 ;         /* Unknown */
      break;
   }

   ua->send_msg("JobId=%i\n", jobid) ;
   ua->send_msg("JobStatus=%s (%c)\n",
                job_status_to_str(jobstatus, 0),
                jobstatus) ;

   if (ua->gui || ua->api) {
      ua->send_msg("ExitStatus=%i\n", status) ;
   }

   return 1;
}
/*
 * Wait until no job is running
 */
Wait until no job is running
Wait until no job is running
int wait_cmd(UAContext *ua, const char *cmd)
{
   JCR *jcr;
   int i;
   time_t stop_time = 0;
   if (ua->argc == 1) {
      bmicrosleep(0, 200000);            
      for (bool running=true; running; ) {
         running = false;
         foreach_jcr(jcr) {
            if (!jcr->is_internal_job()) {
               running = true;
               break;
            }
         }
         endeach_jcr(jcr);
         if (running) {
            bmicrosleep(1, 0);
         }
      }
      return 1;
   }
   i = find_arg_with_value(ua, NT_("timeout"));
   if (i > 0 && ua->argv[i]) {
      stop_time = time(NULL) + str_to_int64(ua->argv[i]);
   }
   uint32_t jobid = 0 ;
   if (!open_client_db(ua)) {
      ua->error_msg(_("ERR: Can't open db\n")) ;
      return 1;
   }
   for (int i=1; i<ua->argc; i++) {
      if (strcasecmp(ua->argk[i], "jobid") == 0) {
         if (!ua->argv[i]) {
            break;
         }
         jobid = str_to_int64(ua->argv[i]);
         break;
      } else if (strcasecmp(ua->argk[i], "jobname") == 0 ||
                 strcasecmp(ua->argk[i], "job") == 0) {
         if (!ua->argv[i]) {
            break;
         }
         jcr=get_jcr_by_partial_name(ua->argv[i]) ;
         if (jcr) {
            jobid = jcr->JobId ;
            free_jcr(jcr);
         }
         break;
      } else if (strcasecmp(ua->argk[i], "ujobid") == 0) {
         if (!ua->argv[i]) {
            break;
         }
         jcr=get_jcr_by_full_name(ua->argv[i]) ;
         if (jcr) {
            jobid = jcr->JobId ;
            free_jcr(jcr);
         }
         break;
      } else if (strcasecmp(ua->argk[i], "mount") == 0) {
         for (bool waiting=false; !waiting; ) {
            foreach_jcr(jcr) {
               if (!jcr->is_internal_job() &&
                   (jcr->JobStatus == JS_WaitMedia || jcr->JobStatus == JS_WaitMount ||
                    jcr->SDJobStatus == JS_WaitMedia || jcr->SDJobStatus == JS_WaitMount))
               {
                  waiting = true;
                  break;
               }
            }
            endeach_jcr(jcr);
            if (waiting) {
               break;
            }
            if (stop_time && (time(NULL) >= stop_time)) {
               ua->warning_msg(_("Wait on mount timed out\n"));
               return 1;
            }
            bmicrosleep(1, 0);
         }
         return 1;
      }
   }
   if (jobid == 0) {
      ua->error_msg(_("ERR: Job was not found\n"));
      return 1 ;
   }
   bmicrosleep(0, 200000);            
   for (bool running=true; running; ) {
      running = false;
      jcr=get_jcr_by_id(jobid) ;
      if (jcr) {
         running = true ;
         free_jcr(jcr);
      }
      if (running) {
         bmicrosleep(1, 0);
      }
   }
   int status ;
   char jobstatus = '?';        
   char buf[256] ;
   bsnprintf(buf, sizeof(buf),
             "SELECT JobStatus FROM Job WHERE JobId='%i'", jobid);
   db_sql_query(ua->db, buf, status_handler, (void *)&jobstatus);
   switch (jobstatus) {
   case JS_Error:
      status = 1 ;         
      break;
   case JS_Incomplete:
   case JS_FatalError:
   case JS_ErrorTerminated:
   case JS_Canceled:
      status = 2 ;         
      break;
   case JS_Warnings:
   case JS_Terminated:
      status = 0 ;         
      break;
   default:
      status = 3 ;         
      break;
   }
   ua->send_msg("JobId=%i\n", jobid) ;
   ua->send_msg("JobStatus=%s (%c)\n",
                job_status_to_str(jobstatus, 0),
                jobstatus) ;
   if (ua->gui || ua->api) {
      ua->send_msg("ExitStatus=%i\n", status) ;
   }
   return 1;
}
int wait_cmd ( UAContext * ua , const char * cmd ) { JCR * jcr ; int i ; time_t stop_time = 0 ; if ( ua -> argc == 1 ) { bmicrosleep ( 0 , 200000 ) ; for ( bool running = true ; running ; ) { running = false ; foreach_jcr ( jcr ) { if ( ! jcr -> is_internal_job ( ) ) { running = true ; break ; } } endeach_jcr ( jcr ) ; if ( running ) { bmicrosleep ( 1 , 0 ) ; } } return 1 ; } i = find_arg_with_value ( ua , NT_ ( " " ) ) ; if ( i > 0 && ua -> argv [ i ] ) { stop_time = time ( NULL ) + str_to_int64 ( ua -> argv [ i ] ) ; } uint32_t jobid = 0 ; if ( ! open_client_db ( ua ) ) { ua -> error_msg ( _ ( " \n " ) ) ; return 1 ; } for ( int i = 1 ; i < ua -> argc ; i ++ ) { if ( strcasecmp ( ua -> argk [ i ] , " " ) == 0 ) { if ( ! ua -> argv [ i ] ) { break ; } jobid = str_to_int64 ( ua -> argv [ i ] ) ; break ; } else if ( strcasecmp ( ua -> argk [ i ] , " " ) == 0 || strcasecmp ( ua -> argk [ i ] , " " ) == 0 ) { if ( ! ua -> argv [ i ] ) { break ; } jcr = get_jcr_by_partial_name ( ua -> argv [ i ] ) ; if ( jcr ) { jobid = jcr -> JobId ; free_jcr ( jcr ) ; } break ; } else if ( strcasecmp ( ua -> argk [ i ] , " " ) == 0 ) { if ( ! ua -> argv [ i ] ) { break ; } jcr = get_jcr_by_full_name ( ua -> argv [ i ] ) ; if ( jcr ) { jobid = jcr -> JobId ; free_jcr ( jcr ) ; } break ; } else if ( strcasecmp ( ua -> argk [ i ] , " " ) == 0 ) { for ( bool waiting = false ; ! waiting ; ) { foreach_jcr ( jcr ) { if ( ! jcr -> is_internal_job ( ) && ( jcr -> JobStatus == JS_WaitMedia || jcr -> JobStatus == JS_WaitMount || jcr -> SDJobStatus == JS_WaitMedia || jcr -> SDJobStatus == JS_WaitMount ) ) { waiting = true ; break ; } } endeach_jcr ( jcr ) ; if ( waiting ) { break ; } if ( stop_time && ( time ( NULL ) >= stop_time ) ) { ua -> warning_msg ( _ ( " \n " ) ) ; return 1 ; } bmicrosleep ( 1 , 0 ) ; } return 1 ; } } if ( jobid == 0 ) { ua -> error_msg ( _ ( " \n " ) ) ; return 1 ; } bmicrosleep ( 0 , 200000 ) ; for ( bool running = true ; running ; ) { running = false ; jcr = get_jcr_by_id ( jobid ) ; if ( jcr ) { running = true ; free_jcr ( jcr ) ; } if ( running ) { bmicrosleep ( 1 , 0 ) ; } } int status ; char jobstatus = ' ' ; char buf [ 256 ] ; bsnprintf ( buf , sizeof ( buf ) , " " , jobid ) ; db_sql_query ( ua -> db , buf , status_handler , ( void * ) & jobstatus ) ; switch ( jobstatus ) { case JS_Error : status = 1 ; break ; case JS_Incomplete : case JS_FatalError : case JS_ErrorTerminated : case JS_Canceled : status = 2 ; break ; case JS_Warnings : case JS_Terminated : status = 0 ; break ; default : status = 3 ; break ; } ua -> send_msg ( " \n " , jobid ) ; ua -> send_msg ( " \n " , job_status_to_str ( jobstatus , 0 ) , jobstatus ) ; if ( ua -> gui || ua -> api ) { ua -> send_msg ( " \n " , status ) ; } return 1 ; }
Wait until no job is running
Wait until no job is running
/*
    * no args
    * Wait until no job is running
    */ /* let job actually start */ /* we have jobid, jobname or ujobid argument */ /* Wait for a mount request */ /*
    * We wait the end of a specific job
    */ /* let job actually start */ /*
    * We have to get JobStatus
    */ /* Unknown by default */ /* Warning */ /* Critical */ /* Ok */ /* Unknown */