CMS  Version 3.9
process_manager.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9  Copyright (c) 2010 Sonjara, Inc
10 
11  Permission is hereby granted, free of charge, to any person
12  obtaining a copy of this software and associated documentation
13  files (the "Software"), to deal in the Software without
14  restriction, including without limitation the rights to use,
15  copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the
17  Software is furnished to do so, subject to the following
18  conditions:
19 
20  The above copyright notice and this permission notice shall be
21  included in all copies or substantial portions of the Software.
22 
23  Except as contained in this notice, the name(s) of the above
24  copyright holders shall not be used in advertising or otherwise
25  to promote the sale, use or other dealings in this Software
26  without prior written authorization.
27 
28  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35  OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 
46 {
47  var $id;
48  var $cache;
49 
50  function ProcessManager($id = "")
51  {
52  global $config;
53 
54  $this->id = $id;
55 
56  if (!$config["file_backed_cache_directory"])
57  {
58  $cacheDir = $config["uploadbase"] . DIRECTORY_SEPARATOR . "cache";
59  if (!is_dir($cacheDir))
60  {
61  mkdir($cacheDir);
62  }
63 
64  $config["file_backed_cache_directory"] = $cacheDir;
65 
66  $this->cache = new FileBackedCache();
67  }
68  }
69 
76  function run($action, $params = array())
77  {
78  global $config;
79  global $user;
80 
81  $isWindows = Fakoli::detectWindowsOS();
82  $this->id = sha1(now() . $action);
83  foreach($params as $key => $value)
84  {
85  $this->put($key, $value);
86  }
87 
88  $launch = $isWindows ? "start " : "";
89  $bg = $isWindows ? "" : " &";
90  $run = Settings::getValue("process", "PHP_executable")." -d include_path='".
91  get_include_path()."' ".
92  sanitizePath(Fakoli::getComponent("process")->component_path) . DIRECTORY_SEPARATOR . "run.php";
93  $home = $config['homedir'];
94 
95  $userPK = $user ? $user->get($user->primary_key) : "";
96 
97  $cmd = "$launch$run ".escapeshellarg($home)." ".escapeshellarg($this->id)." ".escapeshellarg($action)." ".$userPK." ".$bg;
98 
99  $this->put("status", "Starting");
100  $this->put("error", "");
101 
102  trace($cmd, 3);
103 
104  pclose(popen($cmd, "r"));
105 
106  return $this->id;
107  }
108 
109  function put($key, $value)
110  {
111  $this->cache->put("{$this->id}_{$key}", $value);
112  }
113 
114  function get($key)
115  {
116  return $this->cache->get("{$this->id}_{$key}");
117  }
118 
119  function setProgress($status, $message, $percentage)
120  {
121  $this->put("status", $status);
122  $this->put("message", $message);
123  $this->put("percentage", number_format($percentage, 0));
124 
125  trace("{$this->id} $status: $message ($percentage%)", 3);
126  }
127 
128  static function setDefaults()
129  {
130  Settings::setDefaultValue("process", "PHP_executable", "/usr/bin/php", String, "This is the path to the PHP command line executable");
131  Settings::setDefaultValue("process", "background_process_log_file", "", String, "Optionally specify a separate log file for background processes");
132  }
133 
134  static function upgradeComponent($version)
135  {
136  $mgr = new ProcessUpgradeManager();
137  $mgr->upgrade($version);
138  }
139 }
140 ?>
static detectWindowsOS()
Returns true if running on Windows, false otherwise.
Definition: core.inc:1455
static getComponent($name)
Return the component record by name.
Definition: core.inc:235
Provides a central management class for event handlers and common functionality for the process compo...
setProgress($status, $message, $percentage)
ProcessManager($id="")
static upgradeComponent($version)
put($key, $value)
run($action, $params=array())
Spawns the specified action as a background process under the current user's credentials.
static getValue($component, $name)
Retrieve the value of the specified Setting.
Definition: settings.inc:104
static setDefaultValue($component, $name, $value, $field_type="String", $annotation="", $category="", $options="", $weight=0)
Sets the default value of the given component setting.
Definition: settings.inc:174
global $user
global $config
Definition: import.inc:4
$message
Definition: mail_to.inc:49
if(php_sapi_name() !="cli") $home
Definition: run.php:39
$action
Definition: run.php:41