CMS  Version 3.9
ProcessManager Class Reference

Provides a central management class for event handlers and common functionality for the process component. More...

Public Member Functions

 ProcessManager ($id="")
 
 run ($action, $params=array())
 Spawns the specified action as a background process under the current user's credentials. More...
 
 put ($key, $value)
 
 get ($key)
 
 setProgress ($status, $message, $percentage)
 

Static Public Member Functions

static setDefaults ()
 
static upgradeComponent ($version)
 

Public Attributes

 $id
 
 $cache
 

Detailed Description

Provides a central management class for event handlers and common functionality for the process component.

Definition at line 45 of file process_manager.inc.

Member Function Documentation

◆ get()

ProcessManager::get (   $key)

Definition at line 114 of file process_manager.inc.

115  {
116  return $this->cache->get("{$this->id}_{$key}");
117  }

◆ ProcessManager()

ProcessManager::ProcessManager (   $id = "")

Definition at line 50 of file process_manager.inc.

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  }
global $config
Definition: import.inc:4

◆ put()

ProcessManager::put (   $key,
  $value 
)

Definition at line 109 of file process_manager.inc.

110  {
111  $this->cache->put("{$this->id}_{$key}", $value);
112  }

◆ run()

ProcessManager::run (   $action,
  $params = array() 
)

Spawns the specified action as a background process under the current user's credentials.

Parameters
string$actionthe action handler to use to handle the request
array$params(optional) array of parameters to pass to the spawned process
Returns
string unique ID for the spawned process

Definition at line 76 of file process_manager.inc.

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  }
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
put($key, $value)
static getValue($component, $name)
Retrieve the value of the specified Setting.
Definition: settings.inc:104
global $user
if(php_sapi_name() !="cli") $home
Definition: run.php:39
$action
Definition: run.php:41

◆ setDefaults()

static ProcessManager::setDefaults ( )
static

Definition at line 128 of file process_manager.inc.

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  }
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

◆ setProgress()

ProcessManager::setProgress (   $status,
  $message,
  $percentage 
)

Definition at line 119 of file process_manager.inc.

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  }
$message
Definition: mail_to.inc:49

◆ upgradeComponent()

static ProcessManager::upgradeComponent (   $version)
static

Definition at line 134 of file process_manager.inc.

135  {
136  $mgr = new ProcessUpgradeManager();
137  $mgr->upgrade($version);
138  }

Member Data Documentation

◆ $cache

ProcessManager::$cache

Definition at line 48 of file process_manager.inc.

◆ $id

ProcessManager::$id

Definition at line 47 of file process_manager.inc.


The documentation for this class was generated from the following file: