From 4f90860001e1529fa288218933477de563b0bcaf Mon Sep 17 00:00:00 2001 From: Jakob Sack Date: Wed, 8 Aug 2012 23:39:30 +0200 Subject: Add initial version of backgroundjobs library --- lib/backgroundjobs/regulartask.php | 52 +++++++++++++++++ lib/backgroundjobs/scheduledtask.php | 106 ++++++++++++++++++++++++++++++++++ lib/backgroundjobs/worker.php | 109 +++++++++++++++++++++++++++++++++++ 3 files changed, 267 insertions(+) create mode 100644 lib/backgroundjobs/regulartask.php create mode 100644 lib/backgroundjobs/scheduledtask.php create mode 100644 lib/backgroundjobs/worker.php diff --git a/lib/backgroundjobs/regulartask.php b/lib/backgroundjobs/regulartask.php new file mode 100644 index 00000000000..38c1255d766 --- /dev/null +++ b/lib/backgroundjobs/regulartask.php @@ -0,0 +1,52 @@ +. +* +*/ + +/** + * This class manages the regular tasks. + */ +class OC_Backgroundjobs_RegularTask{ + static private $registered = array(); + + /** + * @brief creates a regular task + * @param $klass class name + * @param $method method name + * @return true + */ + static public function create( $klass, $method ){ + // Create the data structure + self::$registered["$klass-$method"] = array( $klass, $method ); + + // No chance for failure ;-) + return true; + } + + /** + * @brief gets all regular tasks + * @return associative array + * + * key is string "$klass-$method", value is array( $klass, $method ) + */ + static public function all(){ + return self::$registered; + } +} diff --git a/lib/backgroundjobs/scheduledtask.php b/lib/backgroundjobs/scheduledtask.php new file mode 100644 index 00000000000..48a863c9068 --- /dev/null +++ b/lib/backgroundjobs/scheduledtask.php @@ -0,0 +1,106 @@ +. +* +*/ + +/** + * This class manages our scheduled tasks. + */ +class OC_Backgroundjobs_ScheduledTask{ + /** + * @brief Gets one scheduled task + * @param $id ID of the task + * @return associative array + */ + public static function find( $id ){ + $stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*scheduledtasks WHERE id = ?' ); + $result = $stmt->execute(array($id)); + return $result->fetchRow(); + } + + /** + * @brief Gets all scheduled tasks + * @return array with associative arrays + */ + public static function all(){ + // Array for objects + $return = array(); + + // Get Data + $stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*scheduledtasks' ); + $result = $stmt->execute(array()); + while( $row = $result->fetchRow()){ + $return[] = $row; + } + + // Und weg damit + return $return; + } + + /** + * @brief Gets all scheduled tasks of a specific app + * @param $app app name + * @return array with associative arrays + */ + public static function whereAppIs( $app ){ + // Array for objects + $return = array(); + + // Get Data + $stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*scheduledtasks WHERE app = ?' ); + $result = $stmt->execute(array($app)); + while( $row = $result->fetchRow()){ + $return[] = $row; + } + + // Und weg damit + return $return; + } + + /** + * @brief schedules a task + * @param $app app name + * @param $klass class name + * @param $method method name + * @param $parameters all useful data as text + * @return id of task + */ + public static function add( $task, $klass, $method, $parameters ){ + $stmt = OC_DB::prepare( 'INSERT INTO *PREFIX*scheduledtasks (app, klass, method, parameters) VALUES(?,?,?,?)' ); + $result = $stmt->execute(array($app, $klass, $method, $parameters, time)); + + return OC_DB::insertid(); + } + + /** + * @brief deletes a scheduled task + * @param $id id of task + * @return true/false + * + * Deletes a report + */ + public static function delete( $id ){ + $stmt = OC_DB::prepare( 'DELETE FROM *PREFIX*scheduledtasks WHERE id = ?' ); + $result = $stmt->execute(array($id)); + + return true; + } +} diff --git a/lib/backgroundjobs/worker.php b/lib/backgroundjobs/worker.php new file mode 100644 index 00000000000..0a99c80ebdf --- /dev/null +++ b/lib/backgroundjobs/worker.php @@ -0,0 +1,109 @@ +. +* +*/ + +/** + * This class does the dirty work. + * + * TODO: locking in doAllSteps + */ +class OC_Backgroundjobs_Worker{ + /** + * @brief executes all tasks + * @return boolean + * + * This method executes all regular tasks and then all scheduled tasks. + * This method should be called by cli scripts that do not let the user + * wait. + */ + public static function doAllSteps(){ + // Do our regular work + $regular_tasks = OC_Backgroundjobs_RegularTask::all(); + foreach( $regular_tasks as $key => $value ){ + call_user_func( $value ); + } + + // Do our scheduled tasks + $scheduled_tasks = OC_Backgroundjobs_ScheduledTask::all(); + foreach( $scheduled_tasks as $task ){ + call_user_func( array( $task['klass'], $task['method'] ), $task['parameters'] ); + OC_Backgroundjobs_ScheduledTask::delete( $task['id'] ); + } + + return true; + } + + /** + * @brief does a single task + * @return boolean + * + * This method executes one task. It saves the last state and continues + * with the next step. This method should be used by webcron and ajax + * services. + */ + public static function doWebStep(){ + $laststep = OC_Appconfig::getValue( 'core', 'backgroundjobs_step', 'regular_tasks' ); + + if( $laststep == 'regular_tasks' ){ + // get last app + $lasttask = OC_Appconfig::getValue( 'core', 'backgroundjobs_task', '' ); + + // What's the next step? + $regular_tasks = OC_Backgroundjobs_RegularTask::all(); + ksort( $regular_tasks ); + $done = false; + + // search for next background job + foreach( $regular_tasks as $key => $value ){ + if( strcmp( $lasttask, $key ) > 0 ){ + OC_Appconfig::getValue( 'core', 'backgroundjobs_task', $key ); + $done = true; + call_user_func( $value ); + break; + } + } + + if( $done == false ){ + // Next time load scheduled tasks + OC_Appconfig::setValue( 'core', 'backgroundjobs_step', 'scheduled_tasks' ); + } + } + else{ + $tasks = OC_Backgroundjobs_ScheduledTask::all(); + if( length( $tasks )){ + $task = $tasks[0]; + // delete job before we execute it. This prevents endless loops + // of failing jobs. + OC_Backgroundjobs_ScheduledTask::delete($task['id']); + + // execute job + call_user_func( array( $task['klass'], $task['method'] ), $task['parameters'] ); + } + else{ + // Next time load scheduled tasks + OC_Appconfig::setValue( 'core', 'backgroundjobs_step', 'regular_tasks' ); + OC_Appconfig::setValue( 'core', 'backgroundjobs_task', '' ); + } + } + + return true; + } +} -- cgit v1.2.3 From 6025d2ebc33da1fe2e3cd97b6ad28989c3a60812 Mon Sep 17 00:00:00 2001 From: Jakob Sack Date: Wed, 8 Aug 2012 23:59:30 +0200 Subject: Rename Backgroundjobs to BackgroundJob --- lib/backgroundjobs/regulartask.php | 4 ++-- lib/backgroundjobs/scheduledtask.php | 3 +-- lib/backgroundjobs/worker.php | 14 +++++++------- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/backgroundjobs/regulartask.php b/lib/backgroundjobs/regulartask.php index 38c1255d766..5c416fcdb0e 100644 --- a/lib/backgroundjobs/regulartask.php +++ b/lib/backgroundjobs/regulartask.php @@ -23,7 +23,7 @@ /** * This class manages the regular tasks. */ -class OC_Backgroundjobs_RegularTask{ +class OC_BackgroundJob_RegularTask{ static private $registered = array(); /** @@ -32,7 +32,7 @@ class OC_Backgroundjobs_RegularTask{ * @param $method method name * @return true */ - static public function create( $klass, $method ){ + static public function register( $klass, $method ){ // Create the data structure self::$registered["$klass-$method"] = array( $klass, $method ); diff --git a/lib/backgroundjobs/scheduledtask.php b/lib/backgroundjobs/scheduledtask.php index 48a863c9068..5a2166175c7 100644 --- a/lib/backgroundjobs/scheduledtask.php +++ b/lib/backgroundjobs/scheduledtask.php @@ -1,5 +1,4 @@ $value ){ call_user_func( $value ); } // Do our scheduled tasks - $scheduled_tasks = OC_Backgroundjobs_ScheduledTask::all(); + $scheduled_tasks = OC_BackgroundJob_ScheduledTask::all(); foreach( $scheduled_tasks as $task ){ call_user_func( array( $task['klass'], $task['method'] ), $task['parameters'] ); - OC_Backgroundjobs_ScheduledTask::delete( $task['id'] ); + OC_BackgroundJob_ScheduledTask::delete( $task['id'] ); } return true; @@ -67,7 +67,7 @@ class OC_Backgroundjobs_Worker{ $lasttask = OC_Appconfig::getValue( 'core', 'backgroundjobs_task', '' ); // What's the next step? - $regular_tasks = OC_Backgroundjobs_RegularTask::all(); + $regular_tasks = OC_BackgroundJob_RegularTask::all(); ksort( $regular_tasks ); $done = false; @@ -87,12 +87,12 @@ class OC_Backgroundjobs_Worker{ } } else{ - $tasks = OC_Backgroundjobs_ScheduledTask::all(); + $tasks = OC_BackgroundJob_ScheduledTask::all(); if( length( $tasks )){ $task = $tasks[0]; // delete job before we execute it. This prevents endless loops // of failing jobs. - OC_Backgroundjobs_ScheduledTask::delete($task['id']); + OC_BackgroundJob_ScheduledTask::delete($task['id']); // execute job call_user_func( array( $task['klass'], $task['method'] ), $task['parameters'] ); -- cgit v1.2.3 From 443e19822460df2c831fc3c52bb181d2a7606e92 Mon Sep 17 00:00:00 2001 From: Jakob Sack Date: Thu, 9 Aug 2012 00:00:12 +0200 Subject: Renaming backgroundjobs on file system as well --- lib/backgroundjob/regulartask.php | 52 +++++++++++++++++ lib/backgroundjob/scheduledtask.php | 105 +++++++++++++++++++++++++++++++++ lib/backgroundjob/worker.php | 109 +++++++++++++++++++++++++++++++++++ lib/backgroundjobs/regulartask.php | 52 ----------------- lib/backgroundjobs/scheduledtask.php | 105 --------------------------------- lib/backgroundjobs/worker.php | 109 ----------------------------------- 6 files changed, 266 insertions(+), 266 deletions(-) create mode 100644 lib/backgroundjob/regulartask.php create mode 100644 lib/backgroundjob/scheduledtask.php create mode 100644 lib/backgroundjob/worker.php delete mode 100644 lib/backgroundjobs/regulartask.php delete mode 100644 lib/backgroundjobs/scheduledtask.php delete mode 100644 lib/backgroundjobs/worker.php diff --git a/lib/backgroundjob/regulartask.php b/lib/backgroundjob/regulartask.php new file mode 100644 index 00000000000..5c416fcdb0e --- /dev/null +++ b/lib/backgroundjob/regulartask.php @@ -0,0 +1,52 @@ +. +* +*/ + +/** + * This class manages the regular tasks. + */ +class OC_BackgroundJob_RegularTask{ + static private $registered = array(); + + /** + * @brief creates a regular task + * @param $klass class name + * @param $method method name + * @return true + */ + static public function register( $klass, $method ){ + // Create the data structure + self::$registered["$klass-$method"] = array( $klass, $method ); + + // No chance for failure ;-) + return true; + } + + /** + * @brief gets all regular tasks + * @return associative array + * + * key is string "$klass-$method", value is array( $klass, $method ) + */ + static public function all(){ + return self::$registered; + } +} diff --git a/lib/backgroundjob/scheduledtask.php b/lib/backgroundjob/scheduledtask.php new file mode 100644 index 00000000000..5a2166175c7 --- /dev/null +++ b/lib/backgroundjob/scheduledtask.php @@ -0,0 +1,105 @@ +. +* +*/ + +/** + * This class manages our scheduled tasks. + */ +class OC_BackgroundJob_ScheduledTask{ + /** + * @brief Gets one scheduled task + * @param $id ID of the task + * @return associative array + */ + public static function find( $id ){ + $stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*scheduledtasks WHERE id = ?' ); + $result = $stmt->execute(array($id)); + return $result->fetchRow(); + } + + /** + * @brief Gets all scheduled tasks + * @return array with associative arrays + */ + public static function all(){ + // Array for objects + $return = array(); + + // Get Data + $stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*scheduledtasks' ); + $result = $stmt->execute(array()); + while( $row = $result->fetchRow()){ + $return[] = $row; + } + + // Und weg damit + return $return; + } + + /** + * @brief Gets all scheduled tasks of a specific app + * @param $app app name + * @return array with associative arrays + */ + public static function whereAppIs( $app ){ + // Array for objects + $return = array(); + + // Get Data + $stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*scheduledtasks WHERE app = ?' ); + $result = $stmt->execute(array($app)); + while( $row = $result->fetchRow()){ + $return[] = $row; + } + + // Und weg damit + return $return; + } + + /** + * @brief schedules a task + * @param $app app name + * @param $klass class name + * @param $method method name + * @param $parameters all useful data as text + * @return id of task + */ + public static function add( $task, $klass, $method, $parameters ){ + $stmt = OC_DB::prepare( 'INSERT INTO *PREFIX*scheduledtasks (app, klass, method, parameters) VALUES(?,?,?,?)' ); + $result = $stmt->execute(array($app, $klass, $method, $parameters, time)); + + return OC_DB::insertid(); + } + + /** + * @brief deletes a scheduled task + * @param $id id of task + * @return true/false + * + * Deletes a report + */ + public static function delete( $id ){ + $stmt = OC_DB::prepare( 'DELETE FROM *PREFIX*scheduledtasks WHERE id = ?' ); + $result = $stmt->execute(array($id)); + + return true; + } +} diff --git a/lib/backgroundjob/worker.php b/lib/backgroundjob/worker.php new file mode 100644 index 00000000000..9709882978b --- /dev/null +++ b/lib/backgroundjob/worker.php @@ -0,0 +1,109 @@ +. +* +*/ + +/** + * This class does the dirty work. + * + * TODO: locking in doAllSteps + */ +class OC_BackgroundJob_Worker{ + /** + * @brief executes all tasks + * @return boolean + * + * This method executes all regular tasks and then all scheduled tasks. + * This method should be called by cli scripts that do not let the user + * wait. + */ + public static function doAllSteps(){ + // Do our regular work + $regular_tasks = OC_BackgroundJob_RegularTask::all(); + foreach( $regular_tasks as $key => $value ){ + call_user_func( $value ); + } + + // Do our scheduled tasks + $scheduled_tasks = OC_BackgroundJob_ScheduledTask::all(); + foreach( $scheduled_tasks as $task ){ + call_user_func( array( $task['klass'], $task['method'] ), $task['parameters'] ); + OC_BackgroundJob_ScheduledTask::delete( $task['id'] ); + } + + return true; + } + + /** + * @brief does a single task + * @return boolean + * + * This method executes one task. It saves the last state and continues + * with the next step. This method should be used by webcron and ajax + * services. + */ + public static function doWebStep(){ + $laststep = OC_Appconfig::getValue( 'core', 'backgroundjobs_step', 'regular_tasks' ); + + if( $laststep == 'regular_tasks' ){ + // get last app + $lasttask = OC_Appconfig::getValue( 'core', 'backgroundjobs_task', '' ); + + // What's the next step? + $regular_tasks = OC_BackgroundJob_RegularTask::all(); + ksort( $regular_tasks ); + $done = false; + + // search for next background job + foreach( $regular_tasks as $key => $value ){ + if( strcmp( $lasttask, $key ) > 0 ){ + OC_Appconfig::getValue( 'core', 'backgroundjobs_task', $key ); + $done = true; + call_user_func( $value ); + break; + } + } + + if( $done == false ){ + // Next time load scheduled tasks + OC_Appconfig::setValue( 'core', 'backgroundjobs_step', 'scheduled_tasks' ); + } + } + else{ + $tasks = OC_BackgroundJob_ScheduledTask::all(); + if( length( $tasks )){ + $task = $tasks[0]; + // delete job before we execute it. This prevents endless loops + // of failing jobs. + OC_BackgroundJob_ScheduledTask::delete($task['id']); + + // execute job + call_user_func( array( $task['klass'], $task['method'] ), $task['parameters'] ); + } + else{ + // Next time load scheduled tasks + OC_Appconfig::setValue( 'core', 'backgroundjobs_step', 'regular_tasks' ); + OC_Appconfig::setValue( 'core', 'backgroundjobs_task', '' ); + } + } + + return true; + } +} diff --git a/lib/backgroundjobs/regulartask.php b/lib/backgroundjobs/regulartask.php deleted file mode 100644 index 5c416fcdb0e..00000000000 --- a/lib/backgroundjobs/regulartask.php +++ /dev/null @@ -1,52 +0,0 @@ -. -* -*/ - -/** - * This class manages the regular tasks. - */ -class OC_BackgroundJob_RegularTask{ - static private $registered = array(); - - /** - * @brief creates a regular task - * @param $klass class name - * @param $method method name - * @return true - */ - static public function register( $klass, $method ){ - // Create the data structure - self::$registered["$klass-$method"] = array( $klass, $method ); - - // No chance for failure ;-) - return true; - } - - /** - * @brief gets all regular tasks - * @return associative array - * - * key is string "$klass-$method", value is array( $klass, $method ) - */ - static public function all(){ - return self::$registered; - } -} diff --git a/lib/backgroundjobs/scheduledtask.php b/lib/backgroundjobs/scheduledtask.php deleted file mode 100644 index 5a2166175c7..00000000000 --- a/lib/backgroundjobs/scheduledtask.php +++ /dev/null @@ -1,105 +0,0 @@ -. -* -*/ - -/** - * This class manages our scheduled tasks. - */ -class OC_BackgroundJob_ScheduledTask{ - /** - * @brief Gets one scheduled task - * @param $id ID of the task - * @return associative array - */ - public static function find( $id ){ - $stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*scheduledtasks WHERE id = ?' ); - $result = $stmt->execute(array($id)); - return $result->fetchRow(); - } - - /** - * @brief Gets all scheduled tasks - * @return array with associative arrays - */ - public static function all(){ - // Array for objects - $return = array(); - - // Get Data - $stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*scheduledtasks' ); - $result = $stmt->execute(array()); - while( $row = $result->fetchRow()){ - $return[] = $row; - } - - // Und weg damit - return $return; - } - - /** - * @brief Gets all scheduled tasks of a specific app - * @param $app app name - * @return array with associative arrays - */ - public static function whereAppIs( $app ){ - // Array for objects - $return = array(); - - // Get Data - $stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*scheduledtasks WHERE app = ?' ); - $result = $stmt->execute(array($app)); - while( $row = $result->fetchRow()){ - $return[] = $row; - } - - // Und weg damit - return $return; - } - - /** - * @brief schedules a task - * @param $app app name - * @param $klass class name - * @param $method method name - * @param $parameters all useful data as text - * @return id of task - */ - public static function add( $task, $klass, $method, $parameters ){ - $stmt = OC_DB::prepare( 'INSERT INTO *PREFIX*scheduledtasks (app, klass, method, parameters) VALUES(?,?,?,?)' ); - $result = $stmt->execute(array($app, $klass, $method, $parameters, time)); - - return OC_DB::insertid(); - } - - /** - * @brief deletes a scheduled task - * @param $id id of task - * @return true/false - * - * Deletes a report - */ - public static function delete( $id ){ - $stmt = OC_DB::prepare( 'DELETE FROM *PREFIX*scheduledtasks WHERE id = ?' ); - $result = $stmt->execute(array($id)); - - return true; - } -} diff --git a/lib/backgroundjobs/worker.php b/lib/backgroundjobs/worker.php deleted file mode 100644 index 9709882978b..00000000000 --- a/lib/backgroundjobs/worker.php +++ /dev/null @@ -1,109 +0,0 @@ -. -* -*/ - -/** - * This class does the dirty work. - * - * TODO: locking in doAllSteps - */ -class OC_BackgroundJob_Worker{ - /** - * @brief executes all tasks - * @return boolean - * - * This method executes all regular tasks and then all scheduled tasks. - * This method should be called by cli scripts that do not let the user - * wait. - */ - public static function doAllSteps(){ - // Do our regular work - $regular_tasks = OC_BackgroundJob_RegularTask::all(); - foreach( $regular_tasks as $key => $value ){ - call_user_func( $value ); - } - - // Do our scheduled tasks - $scheduled_tasks = OC_BackgroundJob_ScheduledTask::all(); - foreach( $scheduled_tasks as $task ){ - call_user_func( array( $task['klass'], $task['method'] ), $task['parameters'] ); - OC_BackgroundJob_ScheduledTask::delete( $task['id'] ); - } - - return true; - } - - /** - * @brief does a single task - * @return boolean - * - * This method executes one task. It saves the last state and continues - * with the next step. This method should be used by webcron and ajax - * services. - */ - public static function doWebStep(){ - $laststep = OC_Appconfig::getValue( 'core', 'backgroundjobs_step', 'regular_tasks' ); - - if( $laststep == 'regular_tasks' ){ - // get last app - $lasttask = OC_Appconfig::getValue( 'core', 'backgroundjobs_task', '' ); - - // What's the next step? - $regular_tasks = OC_BackgroundJob_RegularTask::all(); - ksort( $regular_tasks ); - $done = false; - - // search for next background job - foreach( $regular_tasks as $key => $value ){ - if( strcmp( $lasttask, $key ) > 0 ){ - OC_Appconfig::getValue( 'core', 'backgroundjobs_task', $key ); - $done = true; - call_user_func( $value ); - break; - } - } - - if( $done == false ){ - // Next time load scheduled tasks - OC_Appconfig::setValue( 'core', 'backgroundjobs_step', 'scheduled_tasks' ); - } - } - else{ - $tasks = OC_BackgroundJob_ScheduledTask::all(); - if( length( $tasks )){ - $task = $tasks[0]; - // delete job before we execute it. This prevents endless loops - // of failing jobs. - OC_BackgroundJob_ScheduledTask::delete($task['id']); - - // execute job - call_user_func( array( $task['klass'], $task['method'] ), $task['parameters'] ); - } - else{ - // Next time load scheduled tasks - OC_Appconfig::setValue( 'core', 'backgroundjobs_step', 'regular_tasks' ); - OC_Appconfig::setValue( 'core', 'backgroundjobs_task', '' ); - } - } - - return true; - } -} -- cgit v1.2.3 From 088b3ea0bcd60ddc9d6854321e4ca502f874c5f9 Mon Sep 17 00:00:00 2001 From: Jakob Sack Date: Thu, 9 Aug 2012 00:01:06 +0200 Subject: Add public interface to background jobs --- lib/public/backgroundjob.php | 116 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 lib/public/backgroundjob.php diff --git a/lib/public/backgroundjob.php b/lib/public/backgroundjob.php new file mode 100644 index 00000000000..ac863634540 --- /dev/null +++ b/lib/public/backgroundjob.php @@ -0,0 +1,116 @@ +. +* +*/ + +/** + * Public interface of ownCloud forbackground jobs. + */ + +// use OCP namespace for all classes that are considered public. +// This means that they should be used by apps instead of the internal ownCloud classes +namespace OCP; + +/** + * This class provides functions to manage backgroundjobs in ownCloud + * + * There are two kind of background jobs in ownCloud: regular tasks and + * scheduled tasks. + * + * Regular tasks have to be registered in appinfo.php and + * will run on a regular base. Fetching news could be a task that should run + * frequently. + * + * Scheduled tasks have to be registered each time you want to execute them. + * An example of the scheduled task would be the creation of the thumbnail. As + * soon as the user uploads a picture the gallery app registers the scheduled + * task "create thumbnail" and saves the path in the parameter. As soon as the + * task is done it will be deleted from the list. + */ +class Backgroundjob { + /** + * @brief creates a regular task + * @param $klass class name + * @param $method method name + * @return true + */ + public static function addRegularTask( $klass, $method ){ + return \OC_BackgroundJob_RegularTask::register( $klass, $method ); + } + + /** + * @brief gets all regular tasks + * @return associative array + * + * key is string "$klass-$method", value is array( $klass, $method ) + */ + static public function allRegularTasks(){ + return \OC_BackgroundJob_RegularTask::all(); + } + + /** + * @brief Gets one scheduled task + * @param $id ID of the task + * @return associative array + */ + public static function findScheduledTask( $id ){ + return \OC_BackgroundJob_ScheduledTask::find( $id ); + } + + /** + * @brief Gets all scheduled tasks + * @return array with associative arrays + */ + public static function allScheduledTasks(){ + return \OC_BackgroundJob_ScheduledTask::all(); + } + + /** + * @brief Gets all scheduled tasks of a specific app + * @param $app app name + * @return array with associative arrays + */ + public static function scheduledTaskWhereAppIs( $app ){ + return \OC_BackgroundJob_ScheduledTask::whereAppIs( $app ); + } + + /** + * @brief schedules a task + * @param $app app name + * @param $klass class name + * @param $method method name + * @param $parameters all useful data as text + * @return id of task + */ + public static function addScheduledTask( $task, $klass, $method, $parameters ){ + return \OC_BackgroundJob_ScheduledTask::add( $task, $klass, $method, $parameters ); + } + + /** + * @brief deletes a scheduled task + * @param $id id of task + * @return true/false + * + * Deletes a report + */ + public static function deleteScheduledTask( $id ){ + return \OC_BackgroundJob_ScheduledTask::delete( $id ); + } +} -- cgit v1.2.3 From 4107273a790ec53880b70b944faa9db5b142c15b Mon Sep 17 00:00:00 2001 From: Jakob Sack Date: Thu, 9 Aug 2012 00:46:50 +0200 Subject: fix license text --- lib/backgroundjob/regulartask.php | 2 +- lib/backgroundjob/scheduledtask.php | 2 +- lib/backgroundjob/worker.php | 16 ++++++++-------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/backgroundjob/regulartask.php b/lib/backgroundjob/regulartask.php index 5c416fcdb0e..53bd4eb5e9b 100644 --- a/lib/backgroundjob/regulartask.php +++ b/lib/backgroundjob/regulartask.php @@ -1,6 +1,6 @@ $value ){ if( strcmp( $lasttask, $key ) > 0 ){ - OC_Appconfig::getValue( 'core', 'backgroundjobs_task', $key ); + OC_Appconfig::getValue( 'core', 'backgroundjob_task', $key ); $done = true; call_user_func( $value ); break; @@ -83,7 +83,7 @@ class OC_BackgroundJob_Worker{ if( $done == false ){ // Next time load scheduled tasks - OC_Appconfig::setValue( 'core', 'backgroundjobs_step', 'scheduled_tasks' ); + OC_Appconfig::setValue( 'core', 'backgroundjob_step', 'scheduled_tasks' ); } } else{ @@ -99,8 +99,8 @@ class OC_BackgroundJob_Worker{ } else{ // Next time load scheduled tasks - OC_Appconfig::setValue( 'core', 'backgroundjobs_step', 'regular_tasks' ); - OC_Appconfig::setValue( 'core', 'backgroundjobs_task', '' ); + OC_Appconfig::setValue( 'core', 'backgroundjob_step', 'regular_tasks' ); + OC_Appconfig::setValue( 'core', 'backgroundjob_task', '' ); } } -- cgit v1.2.3 From 14cbb8724c65491daaacc4717be020f6340fb829 Mon Sep 17 00:00:00 2001 From: Jakob Sack Date: Thu, 9 Aug 2012 00:48:36 +0200 Subject: Add "cron.php" for background jobs. It is named cron.php because more people will recognize the purpose of the file then. --- cron.php | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 cron.php diff --git a/cron.php b/cron.php new file mode 100644 index 00000000000..fd46174f2bd --- /dev/null +++ b/cron.php @@ -0,0 +1,51 @@ +. +* +*/ + +$RUNTIME_NOSETUPFS = true; +require_once('lib/base.php'); + +$appmode = OC_Appconfig::getValue( 'core', 'backgroundjob_mode', 'web' ); +if( OC::$CLI ){ + if( $appmode == 'web' ){ + OC_Appconfig::setValue( 'core', 'backgroundjob_mode', 'cron' ); + } + + // check if backgroundjobs is still running + $pid = OC_Appconfig::getValue( 'core', 'backgroundjob_pid', false ); + if( $pid !== false ){ + // FIXME: check if $pid is still alive (*nix/mswin). if so then exit + } + // save pid + OC_Appconfig::setValue( 'core', 'backgroundjob_pid', getmypid()); + + // Work + OC_BackgroundJob_Worker::doAllSteps(); +} +else{ + if( $appmode == 'web' ){ + OC_JSON::error( array( 'data' => array( 'message' => 'Backgroundjobs are using system cron!'))); + exit(); + } + OC_BackgroundJob_Worker::doNextStep(); + OC_JSON::success(); +} +exit(); -- cgit v1.2.3 From 7fa896971ffaac5daa5efd7408f58c67ec56f493 Mon Sep 17 00:00:00 2001 From: Jakob Sack Date: Thu, 9 Aug 2012 00:58:54 +0200 Subject: JavaScript file for activating web cron --- core/js/backgroundjobs.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 core/js/backgroundjobs.js diff --git a/core/js/backgroundjobs.js b/core/js/backgroundjobs.js new file mode 100644 index 00000000000..4a558a66b4b --- /dev/null +++ b/core/js/backgroundjobs.js @@ -0,0 +1,25 @@ +/** +* ownCloud +* +* @author Jakob Sack +* @copyright 2012 Jakob Sack owncloud@jakobsack.de +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library 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 library. If not, see . +* +*/ + +// start worker once page has loaded +$(document).ready(function(){ + $.get( OC.webroot+'/cron.php' ); +}); -- cgit v1.2.3 From 13a0818fec4ac758fb050764fb33d90c74200cfe Mon Sep 17 00:00:00 2001 From: Jakob Sack Date: Thu, 9 Aug 2012 01:02:05 +0200 Subject: Be more precise regarding backgroundjobs mode --- cron.php | 6 +++--- lib/base.php | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/cron.php b/cron.php index fd46174f2bd..2bcaaff9fd9 100644 --- a/cron.php +++ b/cron.php @@ -23,9 +23,9 @@ $RUNTIME_NOSETUPFS = true; require_once('lib/base.php'); -$appmode = OC_Appconfig::getValue( 'core', 'backgroundjob_mode', 'web' ); +$appmode = OC_Appconfig::getValue( 'core', 'backgroundjob_mode', 'ajax' ); if( OC::$CLI ){ - if( $appmode == 'web' ){ + if( $appmode != 'cron' ){ OC_Appconfig::setValue( 'core', 'backgroundjob_mode', 'cron' ); } @@ -41,7 +41,7 @@ if( OC::$CLI ){ OC_BackgroundJob_Worker::doAllSteps(); } else{ - if( $appmode == 'web' ){ + if( $appmode == 'cron' ){ OC_JSON::error( array( 'data' => array( 'message' => 'Backgroundjobs are using system cron!'))); exit(); } diff --git a/lib/base.php b/lib/base.php index c3887dec2f8..090d05cdbae 100644 --- a/lib/base.php +++ b/lib/base.php @@ -227,11 +227,17 @@ class OC{ OC_Util::addScript( "jquery.infieldlabel.min" ); OC_Util::addScript( "jquery-tipsy" ); OC_Util::addScript( "oc-dialogs" ); + OC_Util::addScript( "backgroundjobs" ); OC_Util::addScript( "js" ); OC_Util::addScript( "eventsource" ); OC_Util::addScript( "config" ); //OC_Util::addScript( "multiselect" ); OC_Util::addScript('search','result'); + + if( OC_Appconfig::getValue( 'core', 'backgroundjob_mode', 'ajax' ) == 'ajax' ){ + OC_Util::addScript( 'backgroundjobs' ); + } + OC_Util::addStyle( "styles" ); OC_Util::addStyle( "multiselect" ); OC_Util::addStyle( "jquery-ui-1.8.16.custom" ); -- cgit v1.2.3 From 37ee88aa6d6470bdb0e500fc94fe75042453fbb7 Mon Sep 17 00:00:00 2001 From: Jakob Sack Date: Thu, 9 Aug 2012 01:29:15 +0200 Subject: Fixed bug in OC_BackgroundJob_Worker --- lib/backgroundjob/worker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/backgroundjob/worker.php b/lib/backgroundjob/worker.php index 23757529ad8..7514a16b696 100644 --- a/lib/backgroundjob/worker.php +++ b/lib/backgroundjob/worker.php @@ -88,7 +88,7 @@ class OC_BackgroundJob_Worker{ } else{ $tasks = OC_BackgroundJob_ScheduledTask::all(); - if( length( $tasks )){ + if( count( $tasks )){ $task = $tasks[0]; // delete job before we execute it. This prevents endless loops // of failing jobs. -- cgit v1.2.3 From 889f0a1c6df51c5b6495445809143940ad17c327 Mon Sep 17 00:00:00 2001 From: Jakob Sack Date: Thu, 9 Aug 2012 10:40:39 +0200 Subject: rename appconfig keys for backgroundjobs --- cron.php | 8 ++++---- lib/backgroundjob/worker.php | 12 ++++++------ lib/base.php | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/cron.php b/cron.php index 2bcaaff9fd9..9d7e396d61e 100644 --- a/cron.php +++ b/cron.php @@ -23,19 +23,19 @@ $RUNTIME_NOSETUPFS = true; require_once('lib/base.php'); -$appmode = OC_Appconfig::getValue( 'core', 'backgroundjob_mode', 'ajax' ); +$appmode = OC_Appconfig::getValue( 'core', 'backgroundjobs_mode', 'ajax' ); if( OC::$CLI ){ if( $appmode != 'cron' ){ - OC_Appconfig::setValue( 'core', 'backgroundjob_mode', 'cron' ); + OC_Appconfig::setValue( 'core', 'backgroundjobs_mode', 'cron' ); } // check if backgroundjobs is still running - $pid = OC_Appconfig::getValue( 'core', 'backgroundjob_pid', false ); + $pid = OC_Appconfig::getValue( 'core', 'backgroundjobs_pid', false ); if( $pid !== false ){ // FIXME: check if $pid is still alive (*nix/mswin). if so then exit } // save pid - OC_Appconfig::setValue( 'core', 'backgroundjob_pid', getmypid()); + OC_Appconfig::setValue( 'core', 'backgroundjobs_pid', getmypid()); // Work OC_BackgroundJob_Worker::doAllSteps(); diff --git a/lib/backgroundjob/worker.php b/lib/backgroundjob/worker.php index 7514a16b696..799fa5306c6 100644 --- a/lib/backgroundjob/worker.php +++ b/lib/backgroundjob/worker.php @@ -60,11 +60,11 @@ class OC_BackgroundJob_Worker{ * services. */ public static function doNextStep(){ - $laststep = OC_Appconfig::getValue( 'core', 'backgroundjob_step', 'regular_tasks' ); + $laststep = OC_Appconfig::getValue( 'core', 'backgroundjobs_step', 'regular_tasks' ); if( $laststep == 'regular_tasks' ){ // get last app - $lasttask = OC_Appconfig::getValue( 'core', 'backgroundjob_task', '' ); + $lasttask = OC_Appconfig::getValue( 'core', 'backgroundjobs_task', '' ); // What's the next step? $regular_tasks = OC_BackgroundJob_RegularTask::all(); @@ -74,7 +74,7 @@ class OC_BackgroundJob_Worker{ // search for next background job foreach( $regular_tasks as $key => $value ){ if( strcmp( $lasttask, $key ) > 0 ){ - OC_Appconfig::getValue( 'core', 'backgroundjob_task', $key ); + OC_Appconfig::getValue( 'core', 'backgroundjobs_task', $key ); $done = true; call_user_func( $value ); break; @@ -83,7 +83,7 @@ class OC_BackgroundJob_Worker{ if( $done == false ){ // Next time load scheduled tasks - OC_Appconfig::setValue( 'core', 'backgroundjob_step', 'scheduled_tasks' ); + OC_Appconfig::setValue( 'core', 'backgroundjobs_step', 'scheduled_tasks' ); } } else{ @@ -99,8 +99,8 @@ class OC_BackgroundJob_Worker{ } else{ // Next time load scheduled tasks - OC_Appconfig::setValue( 'core', 'backgroundjob_step', 'regular_tasks' ); - OC_Appconfig::setValue( 'core', 'backgroundjob_task', '' ); + OC_Appconfig::setValue( 'core', 'backgroundjobs_step', 'regular_tasks' ); + OC_Appconfig::setValue( 'core', 'backgroundjobs_task', '' ); } } diff --git a/lib/base.php b/lib/base.php index 090d05cdbae..ee80294dd92 100644 --- a/lib/base.php +++ b/lib/base.php @@ -234,7 +234,7 @@ class OC{ //OC_Util::addScript( "multiselect" ); OC_Util::addScript('search','result'); - if( OC_Appconfig::getValue( 'core', 'backgroundjob_mode', 'ajax' ) == 'ajax' ){ + if( OC_Appconfig::getValue( 'core', 'backgroundjobs_mode', 'ajax' ) == 'ajax' ){ OC_Util::addScript( 'backgroundjobs' ); } -- cgit v1.2.3 From 1ce2cd73ffd9c76912f8ba03809a5b347cec38a9 Mon Sep 17 00:00:00 2001 From: Jakob Sack Date: Thu, 9 Aug 2012 10:41:10 +0200 Subject: Add first version of backgroundjobs settings --- settings/ajax/setbackgroundjobsmode.php | 28 ++++++++++++++++++++++++++++ settings/js/admin.js | 8 +++++++- settings/templates/admin.php | 13 +++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 settings/ajax/setbackgroundjobsmode.php diff --git a/settings/ajax/setbackgroundjobsmode.php b/settings/ajax/setbackgroundjobsmode.php new file mode 100644 index 00000000000..454b3caa5bf --- /dev/null +++ b/settings/ajax/setbackgroundjobsmode.php @@ -0,0 +1,28 @@ +. +* +*/ + +OC_Util::checkAdminUser(); +OCP\JSON::callCheck(); + +OC_Appconfig::setValue( 'core', 'backgroundjob_mode', $_POST['mode'] ); + +echo 'true'; diff --git a/settings/js/admin.js b/settings/js/admin.js index 4f295ab6f5d..409594a4b94 100644 --- a/settings/js/admin.js +++ b/settings/js/admin.js @@ -3,5 +3,11 @@ $(document).ready(function(){ $.post(OC.filePath('settings','ajax','setloglevel.php'), { level: $(this).val() },function(){ OC.Log.reload(); } ); - }) + }); + + $('#backgroundjobs input').change(function(){ + if($(this).attr('checked')){ + $.post(OC.filePath('settings','ajax','setbackgroundjobsmode.php'), { mode: $(this).val() }); + } + }); }); \ No newline at end of file diff --git a/settings/templates/admin.php b/settings/templates/admin.php index 60b9732d7f4..318bfbbe196 100755 --- a/settings/templates/admin.php +++ b/settings/templates/admin.php @@ -24,6 +24,19 @@ if(!$_['htaccessworking']) { + +
+ t('Cron');?> + +
+ +
+ +
+ +
+
+
t('Log');?> Log level: + >
- + >
- + >
- + >
-- cgit v1.2.3 From 7c766cdfe05d02bd896d73d30defec76d6844570 Mon Sep 17 00:00:00 2001 From: Jakob Sack Date: Fri, 10 Aug 2012 01:42:30 +0200 Subject: Backgroundjobs: fix bugs in template --- settings/templates/admin.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/settings/templates/admin.php b/settings/templates/admin.php index af724f134bf..31a7a3dcc10 100755 --- a/settings/templates/admin.php +++ b/settings/templates/admin.php @@ -27,13 +27,13 @@ if(!$_['htaccessworking']) {
t('Cron');?> - > + >
- > + >
- > + >
- > + >
-- cgit v1.2.3 From 9ad31e5f81ad5ec899101a67451843dd8f080340 Mon Sep 17 00:00:00 2001 From: Jakob Sack Date: Fri, 10 Aug 2012 01:44:38 +0200 Subject: Backgroundjobs: use correct var name in template --- settings/templates/admin.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/settings/templates/admin.php b/settings/templates/admin.php index 31a7a3dcc10..93707394270 100755 --- a/settings/templates/admin.php +++ b/settings/templates/admin.php @@ -27,13 +27,13 @@ if(!$_['htaccessworking']) {
t('Cron');?> - > + >
- > + >
- > + >
- > + >
-- cgit v1.2.3