Beispiel #1
0
function jobcancel($jobs, $error_json_exit = false)
{
    $GLOBALS['lasterror'] = "";
    $GLOBALS['lastnotice'] = "";
    global $use_db;
    global $db_errors;
    $appconfig = json_decode(file_get_contents("/home/abhishek/Desktop/GenApp/abhishektest/appconfig.json"), true);
    if (!isset($appconfig['resources'])) {
        $GLOBALS['lasterror'] = "Internal error: could not find resource configuration information in appconfig.json";
        require_once "mail.php";
        error_mail("[joblog.php jobcancel()] " . $GLOBALS['lasterror']);
        return false;
    }
    if (!db_connect($error_json_exit)) {
        $GLOBALS['lasterror'] = $db_errors;
        return false;
    }
    $context = new ZMQContext();
    $zmq_socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'abhishektest udp pusher');
    $zmq_socket->connect("tcp://" . $appconfig['messaging']['zmqhostip'] . ":" . $appconfig['messaging']['zmqport']);
    // $udp_socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
    $runs = $use_db->abhishektest->running->find(array("_id" => array('$in' => $jobs)));
    $fjobs = array_flip($jobs);
    $projectdirs = array();
    $tokillparent = array();
    $tokill = array();
    $kjobs = array();
    foreach ($runs as $v) {
        $uuid = $v['_id'];
        $job = $use_db->abhishektest->jobs->findOne(array("_id" => $uuid));
        $pids = $v['pid'];
        foreach ($pids as $k2 => $v2) {
            if ($v2['pid'] < 2) {
                require_once "mail.php";
                error_mail("[joblog.php jobcancel()] invalid pid for kill! " . $v2['pid']);
            } else {
                $where = $v2['where'];
                if ($v2['what'] == "parent") {
                    if (!isset($tokillparent[$where])) {
                        $tokillparent[$where] = array($v2['pid']);
                    } else {
                        $tokillparent[$where][] = $v2['pid'];
                    }
                } else {
                    if (!isset($tokill[$where])) {
                        $tokill[$where] = array($v2['pid']);
                    } else {
                        $tokill[$where][] = $v2['pid'];
                    }
                }
            }
        }
        unset($fjobs[$uuid]);
        $kjobs[$uuid] = true;
        // send messages also if running about "cancelled"
        // also manually clear job locks and push update to jobs as in jobrun.php
        if (!logjobupdate("cancelled", true, $error_json_exit, $uuid)) {
        }
        logstoprunning();
        $zmq_socket->send(json_encode(array("_uuid" => $uuid, "Notice" => "This job has been cancelled by user request", "_cancel" => "true", "_status" => "cancelled")));
        // $jsonmsg = json_encode( array( "_uuid" => $uuid,
        //                               "Notice" => "This job has been cancelled by user request",
        //                               "_status" => "cancelled" ) );
        // socket_sendto( $udp_socket, $jsonmsg, strlen( $jsonmsg ), 0, $appconfig['messaging'][ 'udphostip' ], $appconfig['messaging']['udpport'] );
        if (getprojectdir($uuid)) {
            $projectdirs[$GLOBALS['getprojectdir']] = true;
        }
    }
    foreach ($tokill as $k => $v) {
        if (!isset($appconfig['resources'][$k])) {
            $GLOBALS['lasterror'] .= "Resource {$k} missing from resource configuration information in appconfig.json";
            require_once "mail.php";
            error_mail("[joblog.php jobcancel()] " . $GLOBALS['lasterror']);
        } else {
            $kill = $appconfig['resources'][$k] . " /var/www/html/abhishektest/util/ga_killprocs.pl /var/www/html/abhishektest/log {$k} all " . implode(' ', $v);
            ob_start();
            exec($kill, $execout);
            ob_end_clean();
        }
    }
    foreach ($tokillparent as $k => $v) {
        if (!isset($appconfig['resources'][$k])) {
            $GLOBALS['lasterror'] .= "Resource {$k} missing from resource configuration information in appconfig.json";
            require_once "mail.php";
            error_mail("[joblog.php jobcancel()] " . $GLOBALS['lasterror']);
        } else {
            $kill = $appconfig['resources'][$k] . " /var/www/html/abhishektest/util/ga_killprocs.pl /var/www/html/abhishektest/log {$k} child " . implode(' ', $v);
            ob_start();
            exec($kill, $execout);
            ob_end_clean();
        }
    }
    foreach ($projectdirs as $k => $v) {
        clearprojectlock($k);
    }
    $msgs = count($kjobs) ? "Job id" . (count($kjobs) > 1 ? "s " : " ") . implode(",", array_keys($kjobs)) . " cancelled." : "";
    $msgs .= count($fjobs) ? "\nNotice; Job id" . (count($fjobs) > 1 ? "s " : " ") . implode(",", array_keys($fjobs)) . " not running so not cancelled." : "";
    $GLOBALS['lastnotice'] = $msgs;
    return true;
}
     if (!sizeof($jobs)) {
         $results["error"] = "Cancel requires at least one selected job";
         $anydone = true;
     } else {
         $results['messages'] = $GLOBALS[jobcancel($jobs) ? 'lastnotice' : 'lasterror'];
         $anydone = true;
     }
     break;
 case 'clearlock':
     if (!sizeof($jobs)) {
         $results["error"] = "Clear lock requires at least one selected job";
         $anydone = true;
     } else {
         $projectdirs = array();
         foreach ($jobs as $v) {
             if (getprojectdir($v)) {
                 $projectdirs[$GLOBALS['getprojectdir']] = true;
             } else {
                 $results['messages'] .= "Could not find project in database for id '{$v}'.\n";
                 $anydone = true;
             }
         }
         //          error_log( print_r( $projectdirs, true ), 3, "/tmp/mylog" );
         foreach ($projectdirs as $k => $v) {
             $dk = end(explode("/", $k));
             if (clearprojectlock($k)) {
                 $results['messages'] .= "Cleared project '{$dk}'.\n";
             } else {
                 $results['messages'] .= "Could NOT clear project '{$dk}': " . $GLOBALS['lasterror'] . $GLOBALS['lasterror'] . $GLOBALS['lastnotice'] . "\n";
             }
             $anydone = true;