From b12d672c5c1983da2f6af7ae9cac189e58455cd7 Mon Sep 17 00:00:00 2001
From: Ralf Becker
Date: Sun, 27 Apr 2003 23:19:49 +0000
Subject: [PATCH] importing asyncservices from .16
---
phpgwapi/cron/asyncservices.php | 42 ++
phpgwapi/inc/class.asyncservice.inc.php | 622 ++++++++++++++++++++++++
2 files changed, 664 insertions(+)
create mode 100644 phpgwapi/cron/asyncservices.php
create mode 100644 phpgwapi/inc/class.asyncservice.inc.php
diff --git a/phpgwapi/cron/asyncservices.php b/phpgwapi/cron/asyncservices.php
new file mode 100644
index 0000000000..95d5d7e4e0
--- /dev/null
+++ b/phpgwapi/cron/asyncservices.php
@@ -0,0 +1,42 @@
+#!/usr/bin/php -q
+ *
+ * Class for creating cron-job like timed calls of phpGroupWare methods *
+ * -------------------------------------------------------------------------*
+ * This library is part of the phpGroupWare API *
+ * http://www.phpgroupware.org/ *
+ * ------------------------------------------------------------------------ *
+ * This library is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as published by *
+ * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details. *
+ * You should have received a copy of the GNU Lesser General Public License *
+ * along with this library; if not, write to the Free Software Foundation, *
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
+ \**************************************************************************/
+
+ /* $Id$ */
+
+ $path_to_phpgroupware = '../..'; // need to be adapted if this script is moved somewhere else
+ $GLOBALS['domain'] = 'default';
+
+ $GLOBALS['phpgw_info']['flags'] = array(
+ 'currentapp' => 'login'
+ );
+ include($path_to_phpgroupware.'/header.inc.php');
+
+ $num = ExecMethod('phpgwapi.asyncservice.check_run');
+
+ // if the following comment got removed, you will get an email from cron for every check performed
+ //echo date('Y/m/d H:i:s ').($num ? "$num job(s) executed" : 'Nothing to execute')."\n";
+
+ $GLOBALS['phpgw']->common->phpgw_exit();
+
+
+
diff --git a/phpgwapi/inc/class.asyncservice.inc.php b/phpgwapi/inc/class.asyncservice.inc.php
new file mode 100644
index 0000000000..b00c48726e
--- /dev/null
+++ b/phpgwapi/inc/class.asyncservice.inc.php
@@ -0,0 +1,622 @@
+ *
+ * Class for creating cron-job like timed calls of phpGroupWare methods *
+ * -------------------------------------------------------------------------*
+ * This library is part of the phpGroupWare API *
+ * http://www.phpgroupware.org/ *
+ * ------------------------------------------------------------------------ *
+ * This library is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as published by *
+ * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details. *
+ * You should have received a copy of the GNU Lesser General Public License *
+ * along with this library; if not, write to the Free Software Foundation, *
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
+ \**************************************************************************/
+
+ /* $Id$ */
+
+ class asyncservice
+ {
+ var $public_functions = array(
+ 'set_timer' => True,
+ 'check_run' => True,
+ 'cancel_timer' => True,
+ 'read' => True,
+ 'install' => True,
+ 'installed' => True,
+ 'test' => True
+ );
+ var $php = '';
+ var $crontab = '';
+ var $db;
+ var $db_table = 'phpgw_async';
+
+ function asyncservice()
+ {
+ $this->db = $GLOBALS['phpgw']->db;
+ }
+
+ /*!
+ @function set_timer
+ @abstract calculates the next run of the timer and puts that with the rest of the data in the db
+ @syntax set_timer($times,$id,$method,$data)
+ @param $times unix timestamp or array('min','hour','dow','day','month','year') with execution time. \
+ Repeated events are possible to shedule by setting the array only partly, eg. \
+ array('day' => 1) for first day in each month 0am or array('min' => '* /5', 'hour' => '9-17') \
+ for every 5mins in the time from 9am to 5pm.
+ @param $id unique id to cancel the request later, if necessary. Should be in a form like \
+ eg. 'X' where id is the internal id of app and X might indicate the action.
+ @param $method Method to be called via ExecMethod($method,$data). $method has the form \
+ '..'.
+ @param $data This data is passed back when the method is called. It might simply be an \
+ integer id, but it can also be a complete array.
+ @Returns False if $id already exists, else True
+ */
+ function set_timer($times,$id,$method,$data)
+ {
+ if (empty($id) || empty($method) || $this->read($id) ||
+ !($next = $this->next_run($times)))
+ {
+ return False;
+ }
+ $job = array(
+ 'id' => $id,
+ 'next' => $next,
+ 'times' => $times,
+ 'method' => $method,
+ 'data' => $data
+ );
+ $this->write($job);
+
+ return True;
+ }
+
+ /*!
+ @function next_run
+ @abstract calculates the next execution time for $time
+ @syntax next_run($time)
+ @param $times unix timestamp or array('year'=>$year,'month'=>$month,'dow'=>$dow,'day'=>$day,'hour'=>$hour,'min'=>$min) \
+ with execution time. Repeated execution is possible to shedule by setting the array only partly, \
+ eg. array('day' => 1) for first day in each month 0am or array('min' => '/5', 'hour' => '9-17') \
+ for every 5mins in the time from 9am to 5pm. All not set units before the smallest one set, \
+ are taken into account as every possible value, all after as the smallest possible value.
+ @returns a unix timestamp of the next execution time or False if no more executions
+ */
+ function next_run($times,$debug=False)
+ {
+ $now = time();
+
+ // $times is unix timestamp => if it's not expired return it, else False
+ //
+ if (!is_array($times))
+ {
+ $next = intval($times);
+
+ return $next > $now ? $next : False;
+ }
+ // If an array is given, we have to enumerate the possible times first
+ //
+ $units = array(
+ 'year' => 'Y',
+ 'month' => 'm',
+ 'day' => 'd',
+ 'dow' => 'w',
+ 'hour' => 'H',
+ 'min' => 'i'
+ );
+ $max_unit = array(
+ 'min' => 59,
+ 'hour' => 23,
+ 'dow' => 6,
+ 'day' => 31,
+ 'month' => 12,
+ 'year' => date('Y')+10 // else */[0-9] would never stop returning numbers
+ );
+ $min_unit = array(
+ 'min' => 0,
+ 'hour' => 0,
+ 'dow' => 0,
+ 'day' => 1,
+ 'month' => 1,
+ 'year' => date('Y')
+ );
+
+ // get the number of the first and last pattern set in $times,
+ // as empty patterns get enumerated before the the last pattern and
+ // get set to the minimum after
+ //
+ $n = $first_set = $last_set = 0;
+ foreach($units as $u => $date_pattern)
+ {
+ ++$n;
+ if (isset($times[$u]))
+ {
+ $last_set = $n;
+
+ if (!$first_set)
+ {
+ $first_set = $n;
+ }
+ }
+ }
+
+ // now we go through all units and enumerate all patterns and not set patterns
+ // (as descript above), enumerations are arrays with unit-values as keys
+ //
+ $n = 0;
+ foreach($units as $u => $date_pattern)
+ {
+ ++$n;
+ //echo "