Ejemplo n.º 1
0
 /**
  * Fires off a controller web request in the background. This is used for triggering long running actions that don't require the user waiting.
  *
  * @param string controllerPath The full url to execute.
  * @param array $post Optional post data to accompany the request
  * @static
  */
 public static function TriggerBackgroundTask($url, $post = null)
 {
     if ($url[0] != '/') {
         $url = "/{$url}";
     }
     $key = uniqid();
     $keypath = Path::Root("/cache/{$key}.srvskey");
     touch($keypath);
     if (!file_exists($keypath)) {
         throw new Exception("Background process could not be triggered, access key could not be written");
     }
     if (is_array($post)) {
         $post['SRVSKEY'] = $key;
     } else {
         $post = array('SRVSKEY' => $key);
     }
     //initiate the transaction.
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, WebPath::Absolute('/srvs' . $url));
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_TIMEOUT, 1);
     //end the request immediately (one second), execution will continue.
     curl_setopt($ch, CURLOPT_FAILONERROR, true);
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
     if (curl_exec($ch) === false && file_exists($keypath)) {
         unlink($keypath);
         throw new Exception("Background process could not be triggered: " . curl_error($ch));
     }
     curl_close($ch);
 }