process.js 9.39 KB
Newer Older
Marco Mariani's avatar
Marco Mariani committed
1 2 3 4 5 6 7
/*jslint undef: true */
/*global $, window, $SCRIPT_ROOT, setRunningState, setCookie, getCookie, deleteCookie */
/*global currentState: true, running: true, $current: true, processType: true, currentProcess: true */
/*global sendStop: true, processState: true, openedlogpage: true, logReadingPosition: true, speed: true */
/*global isRunning: true */
/* vim: set et sts=4: */

8
//Global Traitment!!!
Marco Mariani's avatar
Marco Mariani committed
9 10

var url = $SCRIPT_ROOT + "/slapgridResult";
11 12 13 14 15
var currentState = false;
var running = true;
var processType = "";
var currentProcess;
var sendStop = false;
16
var forcedStop = false;
17 18 19 20
var processState = "Checking"; //define slapgrid running state
var openedlogpage = ""; //content software or instance if the current page is software or instance log, otherwise nothing
var logReadingPosition = 0;
var speed = 5000;
21 22
var maxLogSize = 100000; //Define the max limit of log to display  ~ 2500 lines
var currentLogSize = 0; //Define the size of log actually displayed
Marco Mariani's avatar
Marco Mariani committed
23 24 25
var isRunning = function () {
    "use strict";
    if (running) {
Cédric de Saint Martin's avatar
Cédric de Saint Martin committed
26
        $("#error").Popup("Slapgrid is currently running!",
Marco Mariani's avatar
Marco Mariani committed
27 28 29 30 31 32 33 34 35 36 37
                          {type: 'alert', duration: 3000});
    }
    return running;
};

function setSpeed(value) {
    "use strict";
    if (openedlogpage === "") {
        speed = 5000;
    } else {
        speed = value;
38 39
    }
}
Marco Mariani's avatar
Marco Mariani committed
40 41 42 43 44

function clearAll(setStop) {
    "use strict";
    currentState = false;
    running = setStop;
45
}
Marco Mariani's avatar
Marco Mariani committed
46

Marco Mariani's avatar
Marco Mariani committed
47 48 49 50
function removeFirstLog() {
    "use strict";
    currentLogSize -= parseInt($("#salpgridLog p:first-child").attr('rel'), 10);
    $("#salpgridLog p:first-child").remove();
51 52
}

Marco Mariani's avatar
Marco Mariani committed
53 54
function getRunningState() {
    "use strict";
Marco Mariani's avatar
Marco Mariani committed
55 56 57 58
    var size = 0,
        log_info = "",
        param = {
            position: logReadingPosition,
59
            log: (processState !== "Checking" && openedlogpage !== "") ? processType.toLowerCase() : ""
Marco Mariani's avatar
Marco Mariani committed
60
        },
Marco Mariani's avatar
Marco Mariani committed
61 62
        jqxhr = $.post(url, param, function (data) {
            setRunningState(data);
63
            size = data.content.position - logReadingPosition;
Marco Mariani's avatar
Marco Mariani committed
64
            if (logReadingPosition !== 0 && data.content.truncated) {
65 66
                log_info = "<p  class='info' rel='0'>SLAPRUNNER INFO: SLAPGRID-LOG HAS BEEN TRUNCATED HERE. To see full log reload your log page</p>";
            }
Marco Mariani's avatar
Marco Mariani committed
67 68
            logReadingPosition = data.content.position;
            if (data.content.content !== "") {
69
                if (data.content.content !== "") {
70
                    $("#salpgridLog").append(log_info + "<p rel='" + size + "'>" + data.content.content.toHtmlChar() + "</p>");
71 72 73
                    $("#salpgridLog")
                        .scrollTop($("#salpgridLog")[0].scrollHeight - $("#salpgridLog").height());
                }
Marco Mariani's avatar
Marco Mariani committed
74 75 76 77
            }
            if (running && processState === "Checking" && openedlogpage !== "") {
                $("#salpgridLog").show();
                $("#manualLog").hide();
78 79
                $("#slapstate").show();
                $("#openloglist").hide();
Marco Mariani's avatar
Marco Mariani committed
80 81
            }
            processState = running ? "Running" : "Stopped";
82
            currentLogSize += parseInt(size, 10);
Marco Mariani's avatar
Marco Mariani committed
83
            if (currentLogSize > maxLogSize) {
84 85
                //Remove the first element into log div
                removeFirstLog();
Marco Mariani's avatar
Marco Mariani committed
86
                if (currentLogSize > maxLogSize) {
87 88 89
                    removeFirstLog(); //in cas of previous <p/> size is 0
                }
            }
Marco Mariani's avatar
Marco Mariani committed
90
        }).error(function () {
Marco Mariani's avatar
Marco Mariani committed
91 92
            clearAll(false);
        });
93
}
Marco Mariani's avatar
Marco Mariani committed
94 95 96 97 98

function stopProcess() {
    "use strict";
    if (sendStop) {
        return;
99
    }
Marco Mariani's avatar
Marco Mariani committed
100 101 102 103
    if (running) {
        sendStop = true;

        var urlfor = $SCRIPT_ROOT + "stopSlapgrid",
104
            type = "slapgrid-sr";
Marco Mariani's avatar
Marco Mariani committed
105

106
        if (processType === "Instance") {
107
            type = "slapgrid-cp";
Marco Mariani's avatar
Marco Mariani committed
108 109 110 111 112 113 114 115 116 117 118 119
        }
        $.post(urlfor, {type: type}, function (data) {
            //if (data.result) {
                //$("#error").Popup("Failled to run Slapgrid", {type:'error', duration:3000}); });
            //}
        })
            .error(function () {
                $("#error").Popup("Failed to stop Slapgrid process", {type: 'error', duration: 3000});
            })
            .complete(function () {
                sendStop = false;
                processState = "Stopped";
120
                forcedStop = true;
Marco Mariani's avatar
Marco Mariani committed
121
            });
122 123
    }
}
Marco Mariani's avatar
Marco Mariani committed
124 125 126 127

function bindRun() {
    "use strict";
    $("#softrun").click(function () {
128
        if ($(this).hasClass('slapos_stop')) {
Marco Mariani's avatar
Marco Mariani committed
129 130 131 132
            stopProcess();
        } else {
            if (!isRunning()) {
                setCookie("slapgridCMD", "Software");
133
                window.location.href = $SCRIPT_ROOT + "/viewLog?logfile=software.log";
Marco Mariani's avatar
Marco Mariani committed
134 135 136 137 138
            }
        }
        return false;
    });
    $("#instrun").click(function () {
139
        if ($("#softrun").hasClass('slapos_stop')) {
Marco Mariani's avatar
Marco Mariani committed
140 141 142 143
            stopProcess();
        } else {
            if (!isRunning()) {
                setCookie("slapgridCMD", "Instance");
144
                if (window.location.pathname === "/viewLog")
145
                     window.location.href = $SCRIPT_ROOT + "/viewLog?logfile=instance.log";
Marco Mariani's avatar
Marco Mariani committed
146 147 148 149
            }
        }
        return false;
    });
150
}
Marco Mariani's avatar
Marco Mariani committed
151

152 153 154 155 156 157 158
function updateStatus(elt, val) {
  "use strict";
  var src = '#' + elt + '_run_state', value = 'state_' + val;
  $(src).removeClass();
  $(src).addClass(value);
  switch (val) {
    case "waiting":
159
      $(src).children('p').text("Queue");
160 161 162 163 164 165 166 167 168
      break;
    case "stopped":
      $(src).children('p').text("Stopped by user");
      break;
    case "terminated":
      $(src).children('p').text("Complete");
      break;
    case "running":
      $(src).children('p').text("Processing");
169 170
      processType = elt;
      getRunningState()
171 172
      break;
  }
173 174 175 176 177 178 179
  // in case of failure
  if ($("#salpgridLog").text().indexOf("Failed to run buildout profile") !== -1) {
    var src = '#' + elt + '_run_state', value = 'state_' + "stopped";
    $(src).removeClass();
    $(src).addClass(value);
    $(src).children('p').text("Buildout Failed");
  }
180 181
}

Marco Mariani's avatar
Marco Mariani committed
182 183 184 185 186 187 188 189
function setRunningState(data) {
    "use strict";
    if (data.result) {
        if (!currentState) {
            $("#running").show();
            running = true;
            //change run menu title and style
            if (data.software) {
190 191 192 193 194
                if ( $("#running").children('span').length === 0 ) {
                  $("#softrun").removeClass('slapos_run');
                  $("#softrun").addClass('slapos_stop');
                  $("#running img").before('<p id="running_info" class="software">Building software...</p>');
                }
Marco Mariani's avatar
Marco Mariani committed
195 196 197
                processType = "Software";
            }
            if (data.instance) {
198 199 200 201 202 203
              ///Draft!!
                if ( $("#running").children('span').length === 0 ) {
                  $("#softrun").removeClass('slapos_run');
                  $("#softrun").addClass('slapos_stop');
                  $("#running img").before('<p id="running_info" class="instance">Running instance...</p>');
                }
204 205 206 207 208 209 210
            		if (processType === "Software") {
                    running = false;
                    $("#running_info").remove();
                    $("#softrun").addClass('slapos_run');
                    $("#softrun").removeClass('slapos_stop');
                    $("#instrun").click();
            		}
Marco Mariani's avatar
Marco Mariani committed
211 212 213 214
                processType = "Instance";
            }
        }
    } else {
215 216 217 218 219 220 221 222 223 224 225 226 227
        if ( $("#running").is(":visible") ) {
          $("#error").Popup("Slapgrid finished running your " + processType + " Profile", {type: 'info', duration: 3000});
          if ( forcedStop ) {
            updateStatus('instance', 'stopped');
            updateStatus('software', 'stopped');
          }
          else {
            updateStatus(processType.toLowerCase(), 'terminated');
          }
          //Update window!!!
          $("#slapswitch").attr('rel', 'opend');
          $("#slapswitch").text('Access application');
        }
Marco Mariani's avatar
Marco Mariani committed
228
        $("#running").hide();
229
        $("#running_info").remove();
Marco Mariani's avatar
Marco Mariani committed
230
        running = false; //nothing is currently running
231 232 233 234
        $("#softrun").removeClass('slapos_stop');
        $("#softrun").addClass('slapos_run');
        if ( $("#running").children('span').length > 0 ) {
          $("#running").children('p').remove();
Marco Mariani's avatar
Marco Mariani committed
235
        }
236
        currentState = false;
237
    }
Marco Mariani's avatar
Marco Mariani committed
238
    currentState = data.result;
239 240
}

Marco Mariani's avatar
Marco Mariani committed
241 242 243 244 245 246 247 248 249
function runProcess(urlfor, data) {
    "use strict";
    if (!isRunning()) {
        running = true;
        processState = "Running";
        currentProcess = $.post(urlfor)
            .error(function () {
                $("#error").Popup("Failled to run Slapgrid", {type: 'error', duration: 3000});
            });
250 251 252
        if ( $("#running_info").children('span').length > 0 ) {
          $("#running_info").children('p').remove();
        }
Marco Mariani's avatar
Marco Mariani committed
253 254 255
    }
}

Alain Takoudjou's avatar
Alain Takoudjou committed
256
//setInterval('GetStateRegularly()', 5000);
257 258 259 260
function GetStateRegularly() {
    getRunningState();
}

Marco Mariani's avatar
Marco Mariani committed
261 262 263 264 265 266
function checkSavedCmd() {
    "use strict";
    var result = getCookie("slapgridCMD");
    if (!result) {
        return false;
    }
267
    forcedStop = false;
Marco Mariani's avatar
Marco Mariani committed
268 269 270 271
    if (result === "Software") {
        running = false;
        runProcess(($SCRIPT_ROOT + "/runSoftwareProfile"),
                   {result: true, instance: false, software: true});
272 273
        updateStatus('software', 'running');
        updateStatus('instance', 'waiting');
Marco Mariani's avatar
Marco Mariani committed
274 275 276 277
    } else if (result === "Instance") {
        running = false;
        runProcess(($SCRIPT_ROOT + "/runInstanceProfile"),
                   {result: true, instance: true, software: false});
278 279
        updateStatus('software', 'terminated');
        updateStatus('instance', 'running');
Marco Mariani's avatar
Marco Mariani committed
280 281 282 283
    }
    deleteCookie("slapgridCMD");
    return (result !== null);
}