webtask.php 5.73 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2006-2018 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
#
class WebTask {
    var	$webtask;
27
    var $decoded = null;
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

    #
    # Constructor by lookup on unique ID
    #
    function WebTask($task_id) {
	$safe_id = addslashes($task_id);

	$query_result =
	    DBQueryWarn("select * from web_tasks ".
			"where task_id='$safe_id'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->webtask = NULL;
	    return;
	}
	$this->webtask = mysql_fetch_array($query_result);
    }

    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->webtask);
    }

    # Lookup by imageid
    function Lookup($id) {
	$foo = new WebTask($id);

	if (! $foo->IsValid())
	    return null;

	return $foo;
    }

    # Lookup by object.
    function LookupByObject($uuid) {
	$query_result =
	    DBQueryWarn("select task_id from web_tasks ".
			"where object_uuid='$uuid'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    return null;
	}
	$row = mysql_fetch_array($query_result);
	$idx = $row['task_id'];
	
	return WebTask::Lookup($idx);
    }

76 77 78 79 80 81 82 83 84 85 86
    #
    # Create an anonymous web task (not associated with an object). This
    # is useful when using a webtask to create a new object via a backend
    # script.
    #
    function CreateAnonymous() {
        $task_id = WebTask::GenerateID();

        $query_result = 
            DBQueryWarn("insert into web_tasks set task_id='$task_id', ".
                        "  created=now(), object_uuid='$task_id'");
Leigh Stoller's avatar
Leigh Stoller committed
87 88
        
	if (!$query_result) {
89 90 91 92
            return null;
        }
        return WebTask::Lookup($task_id);
    }
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    #
    # And a normal webtask.
    #
    function Create($uuid) {
        $task_id = WebTask::GenerateID();

        $query_result = 
            DBQueryWarn("insert into web_tasks set task_id='$task_id', ".
                        "  created=now(), object_uuid='$uuid'");
        
	if (!$query_result) {
            return null;
        }
        return WebTask::Lookup($task_id);
    }
108

Leigh Stoller's avatar
Leigh Stoller committed
109
    function Refresh() {
110 111 112 113 114 115 116 117 118 119 120 121 122
	if (! $this->IsValid())
	    return -1;
        $task_id = $this->task_id();

	$query_result =
	    DBQueryWarn("select * from web_tasks ".
			"where task_id='$task_id'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->webtask = NULL;
	    return -1;
	}
	$this->webtask = mysql_fetch_array($query_result);
123
        $this->decoded = null;
124 125 126
        return 0;
    }

127 128 129 130 131 132 133
    # We delete from the web interface.
    function Delete() {
	$task_id = $this->task_id();
	DBQueryWarn("delete from web_tasks where task_id='$task_id'");
	return 0;
    }

134 135 136 137 138 139 140 141 142 143 144
    # Reset to clean state.
    function Reset() {
        $task_id = $this->task_id();

        DBQueryFatal("update web_tasks set ".
                     " exited=null,process_id=0,exitcode=0,task_data=''".
                     "where task_id='$task_id'");

        return $this->Refresh();
    }

145 146 147 148 149 150 151 152 153 154 155 156 157
    # accessors
    function field($name) {
	return (is_null($this->webtask) ? -1 : $this->webtask[$name]);
    }
    function task_id()		{ return $this->field("task_id"); }
    function created()		{ return $this->field("created"); }
    function modified()		{ return $this->field("modified"); }
    function process_id()	{ return $this->field("process_id"); }
    function object_uuid()	{ return $this->field("object_uuid"); }
    function exitcode()		{ return $this->field("exitcode"); }
    function exited()		{ return $this->field("exited"); }
    function task_data()	{ return $this->field("task_data"); }

158 159 160 161 162 163 164 165 166
    function TaskDataObject() {
	if ($this->task_data()) {
	    return json_decode($this->task_data(), false);
	}
	else {
	    return new stdClass();
	}
    }

167 168 169 170 171 172 173 174 175 176 177
    #
    # Return the task data as a real object intead of JSON
    #
    function TaskData() {
	if ($this->task_data()) {
	    return json_decode($this->task_data(), true);
	}
	else {
	    return array();
	}
    }
178 179 180 181 182 183 184 185 186 187 188 189
    # Return a specific value from the data.
    function TaskValue($key) {
	if ($this->task_data()) {
	    if (! $this->decoded) {
		$this->decoded = json_decode($this->task_data(), true);
	    }
	    if (array_key_exists($key, $this->decoded)) {
		return $this->decoded[$key];
	    }
	}
	return null;
    }
190 191 192 193 194 195 196 197 198 199 200 201
    function SetTaskValue($key, $value) {
        if (! $this->decoded) {
            if ($this->task_data()) {
		$this->decoded = json_decode($this->task_data(), true);
	    }
            else {
                $this->decoded = array();
            }
        }
        $this->decoded[$key] = $value;
	return $value;
    }
202 203 204 205 206 207 208

    function ValidTaskID($id) {
	if (preg_match("/^[-\w]+$/", $id)) {
	    return TRUE;
	}
	return FALSE;
    }
209 210 211 212

    function GenerateID() {
	return md5(uniqid(rand(),1));
    }
213 214 215 216 217 218 219 220 221 222 223 224 225 226

    # convenience function
    function output($value = null) {
        if ($value) {
            return $this->SetTaskValue("output", $value);
        }
        return $this->TaskValue("output");
    }
    function code($value = null) {
        if ($value) {
            return $this->SetTaskValue("code", $value);
        }
        return $this->TaskValue("code");
    }
227 228
}
?>