2007-10-16 17:12:48 +00:00
< ? php
2007-10-16 23:02:13 +00:00
function bossa_lookup ( $id , $table ) {
$result = mysql_query ( " select * from $table where id=' $id ' " );
if ( ! $result ) return null ;
$obj = mysql_fetch_object ( $result );
mysql_free_result ( $result );
return $obj ;
}
class BossaApp {
function insert () {
if ( ! $this -> long_jobs ) $this -> long_jobs = 0 ;
2007-10-16 17:12:48 +00:00
$now = time ();
2007-10-16 23:02:13 +00:00
$query = " insert into bossa_app (create_time, name, user_friendly_name, long_jobs, start_url) values ( $now , ' $this->name ', ' $this->user_friendly_name ', $this->long_jobs , ' $this->start_url ') " ;
2007-10-16 17:12:48 +00:00
$result = mysql_query ( $query );
if ( ! $result ) return false ;
2007-10-16 23:02:13 +00:00
$this -> id = mysql_insert_id ();
2007-10-16 17:12:48 +00:00
return true ;
}
2007-10-16 23:02:13 +00:00
static function lookup_name ( $name ) {
2007-10-16 17:12:48 +00:00
$result = mysql_query ( " select * from bossa_app where name=' $name ' " );
if ( ! $result ) return null ;
$app = mysql_fetch_object ( $result );
mysql_free_result ( $result );
return $app ;
}
2007-10-16 23:02:13 +00:00
static function lookup_id ( $id ) {
return bossa_lookup ( $id , 'bossa_app' );
2007-10-16 17:12:48 +00:00
}
2007-10-16 23:02:13 +00:00
}
2007-10-16 17:12:48 +00:00
2007-10-16 23:02:13 +00:00
class BossaJob {
function insert () {
2007-10-16 17:12:48 +00:00
$now = time ();
2007-10-16 23:02:13 +00:00
$query = " insert into bossa_job (create_time, name, app_id, info, batch, time_estimate, time_limit, more_needed, npending, nsuccess, nsuccess_needed) values ( $now , ' $this->name ', $this->app_id , ' $this->info ', $this->batch , $this->time_estimate , $this->time_limit , 1, 0, 0, $this->nsuccess_needed ) " ;
2007-10-16 17:12:48 +00:00
$result = mysql_query ( $query );
if ( ! $result ) {
echo " $query\n " ;
return false ;
}
2007-10-16 23:02:13 +00:00
$this -> id = mysql_insert_id ();
2007-10-16 17:12:48 +00:00
return true ;
}
2007-10-16 23:02:13 +00:00
function update ( $clause ) {
return mysql_query ( " update bossa_job set $clause where id= $this->id " );
}
static function lookup_id ( $id ) {
return bossa_lookup ( $id , 'bossa_job' );
}
}
2007-10-16 17:12:48 +00:00
2007-10-16 23:02:13 +00:00
class BossaJobInst {
function insert () {
2007-10-16 17:12:48 +00:00
$now = time ();
2007-10-16 23:02:13 +00:00
$query = " insert into bossa_job_inst (create_time, job_id, user_id) values ( $now , $this->job_id , $this->user_id ) " ;
2007-10-16 17:12:48 +00:00
$result = mysql_query ( $query );
if ( ! $result ) {
echo " $query\n " ;
return false ;
}
2007-10-16 23:02:13 +00:00
$this -> id = mysql_insert_id ();
2007-10-16 17:12:48 +00:00
return true ;
}
2007-10-16 23:02:13 +00:00
static function lookup_id ( $id ) {
return bossa_lookup ( $id , 'bossa_job_inst' );
}
function update ( $clause ) {
return mysql_query ( " update bossa_job_inst set $clause where id= $this->id " );
}
2007-10-16 17:12:48 +00:00
// Assign a job from the given app to the given user.
// Returns the job instance or NULL.
//
2007-10-16 23:02:13 +00:00
static function assign ( $app , $user ) {
2007-10-16 17:12:48 +00:00
// this query skips jobs for which this user
// has already been assigned an instance
//
$query = " select bossa_job.* from bossa_job left join bossa_job_inst on bossa_job_inst.job_id = bossa_job.id where bossa_job.more_needed<>0 and bossa_job_inst.user_id is null limit 1 " ;
echo " $query\n " ;
$result = mysql_query ( $query );
if ( ! $result ) return null ;
$job = mysql_fetch_object ( $result );
mysql_free_result ( $result );
echo " <hr> " ;
print_r ( $job );
echo " <hr> " ;
2007-10-16 23:02:13 +00:00
$ji = new BossaJobInst ();
2007-10-16 17:12:48 +00:00
$ji -> user_id = $user -> id ;
$ji -> job_id = $job -> id ;
2007-10-16 23:02:13 +00:00
if ( ! $ji -> insert ()) {
2007-10-16 17:12:48 +00:00
echo mysql_error ();
return null ;
}
return $ji ;
}
2007-10-16 23:02:13 +00:00
// The given job instance has completed
//
function completed ( $job ) {
$this -> finish_time = time ();
$this -> update ( " finish_time= $this->finish_time , info=' $this->info ' " );
$job -> npending -- ;
$job -> nsuccess ++ ;
$job -> more_needed = ( $job -> npending + $job -> nsuccess < $job -> nsuccess_needed );
return BossaJob :: update ( " npending= $job->npending , nsuccess= $job->nsuccess , more_needed= $job->more_needed " );
}
// The given job instance has timed out.
//
function timed_out ( $job ) {
$job -> npending -- ;
$job -> more_needed = ( $job -> npending + $job -> nsuccess < $job -> nsuccess_needed );
return BossaJob :: update ( " npending= $job->npending , more_needed= $job->more_needed " );
}
2007-10-16 17:12:48 +00:00
}
?>