WebTask.pm.in 7.11 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
Leigh Stoller's avatar
Leigh Stoller committed
3
# Copyright (c) 2013-2015 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 27 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
# 
# {{{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/>.
# 
# }}}
#
package WebTask;

use strict;
use Carp;
use Exporter;
use SelfLoader ();
use vars qw(@ISA @EXPORT $AUTOLOAD);
@ISA    = qw(Exporter);
@EXPORT = qw( );

use emdb;
use libtestbed;
use JSON;
use Data::Dumper;
use overload ('""' => 'Stringify');

# Configure variables
my $TB	     = "@prefix@";

#
# Lookup by id
#
sub Lookup($$)
{
    my ($class, $id) = @_;

    if ($id !~ /^[-\w]+$/) {
	return undef;
    }
    my $query_result =
	DBQueryWarn("select *,UNIX_TIMESTAMP(created) as created ".
		    "  from web_tasks ".
		    "where task_id='$id'");

    return undef
	if (!$query_result || !$query_result->numrows);
      
    my $self             = {};
    $self->{'DBROW'}     = $query_result->fetchrow_hashref();
    $self->{'AUTOSTORE'} = 0;

    #
    # Turn the data into a perl array so we can mess with it.
    #
    $self->{'DATA'}    = {};
    if (defined($self->{'DBROW'}->{'task_data'}) &&
	$self->{'DBROW'}->{'task_data'} ne "") {
	$self->{'DATA'} =
	    eval { decode_json($self->{'DBROW'}->{'task_data'}); };
	if ($@) {
	    print STDERR "Could not json decode task data\n";
	    return undef
	}
    }
    bless($self, $class);
    return $self;
}

AUTOLOAD {
    my $self  = $_[0];
    my $type  = ref($self) or croak "$self is not an object";
    my $name  = $AUTOLOAD;
    $name =~ s/.*://;   # strip fully-qualified portion

    # A DB row proxy method call.
    if (exists($self->{'DBROW'}->{$name})) {
	return $self->{'DBROW'}->{$name};
    }
    # Or it is for a task data.
    if (scalar(@_) == 2) {
	$self->{'DATA'}->{$name} = $_[1];
	$self->Store()
	    if ($self->{'AUTOSTORE'});
	return $_[1];
    }
    elsif (exists($self->{'DATA'}->{$name})) {
	return $self->{'DATA'}->{$name};
    }
    return undef;
}

# Break circular reference someplace to avoid exit errors.
sub DESTROY {
    my $self = shift;

    $self->{'DBROW'} = undef;
    $self->{'DATA'}  = undef;
}

# Mark the AUTOSTORE flag.
sub AutoStore($$)
{
    my ($self, $flag) = @_;
    $self->{'AUTOSTORE'} = $flag;
}

#
# Create a new web task. The task id is optional, it allows the
# web interface to pass in the task ID it wants us to use.
#
sub Create($$;$)
{
    my ($class, $object_uuid, $task_id) = @_;

128 129 130 131 132
    if (defined($object_uuid)) {
	DBQueryWarn("delete from web_tasks ".
		    "where object_uuid='$object_uuid'")
	    or return undef;
    }
133 134 135

    $task_id = TBGenSecretKey()
	if (!defined($task_id));
136 137 138
    # XXX
    $object_uuid = $task_id
	if (!defined($object_uuid));
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

    DBQueryWarn("insert into web_tasks set task_id='$task_id', ".
		"  created=now(), object_uuid='$object_uuid'")
	or return undef;

    return WebTask->Lookup($task_id);
}

sub Delete($)
{
    my ($self)  = @_;
    my $task_id = $self->task_id();

    DBQueryWarn("delete from web_tasks where task_id='$task_id'")
	or return -1;

    return 0;
}

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
sub DeleteByObject($$)
{
    my ($class, $uuid) = @_;

    DBQueryWarn("delete from web_tasks where object_uuid='$uuid'")
	or return -1;
    
    return 0;
}

#
# Utility function to lookup and create if it does not exists.
#
sub LookupOrCreate($$$)
{
    my ($class, $object_uuid, $webtask_id) = @_;

    my $webtask = WebTask->Lookup($webtask_id);
    if (!defined($webtask)) {
	$webtask = WebTask->Create($object_uuid, $webtask_id);
    }
    return $webtask;
}

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
#
# Lookup by object id. We can get into a problem here if there
# is more then one.
#
sub LookupByObject($$)
{
    my ($class, $uuid) = @_;

    my $query_result =
	DBQueryWarn("select task_id from web_tasks where object_uuid='$uuid'");
    return undef
	if (!$query_result || !$query_result->numrows);

    my ($task_id) = $query_result->fetchrow_array();
    
    return WebTask->Lookup($task_id);
}

#
# Write the data back to the DB.
#
sub Store($)
{
    my ($self) = @_;
    
    my $id       = $self->{'DBROW'}->{'task_id'};
    my $data     = eval { encode_json($self->{'DATA'}); };
    if ($@) {
	print STDERR "Could not json encode task data\n";
	return -1;
    }
    $data = emdb::DBQuoteSpecial($data);
    DBQueryWarn("update web_tasks set modified=now(),task_data=$data ".
		"where task_id='$id'")
	or return -1;

    return 0;
}

#
# Refresh the object from the database.
#
sub Refresh($)
{
    my ($self)  = @_;
    my $id      = $self->task_id();

    my $query_result =
	DBQueryWarn("select *,UNIX_TIMESTAMP(created) as created ".
		    "  from web_tasks ".
		    "where task_id='$id'");

    return -1
	if (!$query_result || !$query_result->numrows);
      
    $self->{'DBROW'}   = $query_result->fetchrow_hashref();

    #
    # Turn the data into a perl array so we can mess with it.
    #
    $self->{'DATA'}    = {};
243 244 245 246
    if (defined($self->{'DBROW'}->{'task_data'}) &&
	$self->{'DBROW'}->{'task_data'} ne "") {
	$self->{'DATA'} =
	    eval { decode_json($self->{'DBROW'}->{'task_data'}); };
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	if ($@) {
	    print STDERR "Could not json decode task data\n";
	    return -1;
	}
    }
    return 0;
}

#
# Stringify for output.
#
sub Stringify($)
{
    my ($self) = @_;
    
    my $id      = $self->task_id();

264
    return "[WebTask: $id]";
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
}

# Debugging.
sub Dump($)
{
    my ($self) = @_;
    
    print STDERR Dumper($self->{'DATA'}) . "\n";
}

# Set the process ID.
sub SetProcessID($$)
{
    my ($self, $pid) = @_;
    my $task_id      = $self->task_id();

    DBQueryWarn("update web_tasks set process_id='$pid' ".
		"where task_id='$task_id'")
	or return -1;

    $self->{'DBROW'}->{'process_id'} = $pid;
    return 0;
}

289 290 291 292 293 294 295 296 297 298 299 300 301 302
# Set the object uuid.
sub SetObject($$)
{
    my ($self, $uuid) = @_;
    my $task_id       = $self->task_id();

    DBQueryWarn("update web_tasks set object_uuid='$uuid' ".
		"where task_id='$task_id'")
	or return -1;

    $self->{'DBROW'}->{'object_uuid'} = $uuid;
    return 0;
}

303 304 305 306 307 308 309 310 311 312
#
# Mark as exited. We want to leave the row around long enough for
# anyone waiting to notice it has finished. We will have to GC these
# on a periodic basis.
#
sub Exited($$)
{
    my ($self, $exitcode) = @_;
    my $task_id      = $self->task_id();

313
    $self->Store();
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    DBQueryWarn("update web_tasks set process_id=0, ".
		"  exited=now(), exitcode='$exitcode' ".
		"where task_id='$task_id'")
	or return -1;

    $self->{'DBROW'}->{'process_id'} = 0;
    $self->{'DBROW'}->{'exitcode'}   = $exitcode;
    $self->{'DBROW'}->{'exited'}     = time();
    return 0;
}

sub HasExited($)
{
    my ($self) = @_;

    return (defined($self->exited()) ? 1 : 0);
}

# _Always_ make sure that this 1 is at the end of the file...
1;