Skip to content

Instantly share code, notes, and snippets.

@lukebeer
Created January 27, 2016 15:43
Show Gist options
  • Save lukebeer/af7d3383f7147ab52e24 to your computer and use it in GitHub Desktop.
Save lukebeer/af7d3383f7147ab52e24 to your computer and use it in GitHub Desktop.
Example code from a webapp adding serialised OCI-P messages into a Redis list by POST then processed by 1 or more workers in separate processes to decouple the processor - old code, depends on v1 of broadworks-ocip
<?php
require_once 'common.php';
require_once BASE_PATH . 'inc/Broadworks_OCI-P/common.php';
require_once BASE_PATH . 'inc/Broadworks_OCI-P/core/OCISession.php';
ini_set('display_errors',1);
require 'Predis/Autoloader.php';
Predis\Autoloader::register();
if (isset($_POST['file'])) {
if (preg_match('/^migrate_/', $_POST['file'])) {
$file = JOBFILE_BASE_PATH . strtolower($_SESSION['platform']) . '/migrate/' . $_POST['file'];
}
if (preg_match('/^backup_/', $_POST['file'])) {
$file = JOBFILE_BASE_PATH . strtolower($_SESSION['platform']) . '/backup/' . $_POST['file'];
}
if (file_exists($file)) {
header('Content-type: application/json');
$redis = new Predis\Client();
$session = serialize($_SESSION['session']);
$id = uniqid();
$client = CoreFactory::getOCIClient($_SESSION['webservice'], false);
$client->setSession(unserialize($session));
$client->send(OCISchemaLogin::VerifySessionIsValidRequest());
if ($client->getResponse()) {
if ($redis->set("dmsjob:$id", $file)) {
$redis->set("dmssession:$id", $session);
$redis->set("dmsprogress:$id", 0);
exec("/usr/bin/php5-cli ~/public_html/dms/php/worker.php > /dev/null 2>&1 &");
echo json_encode(['success' => "Sent {$_POST['file']} to worker thread with ID: $id", 'jobid' => $id]);
} else {
echo json_encode(['error' => "Something went wrong adding the job..."]);
}
} else {
echo json_encode(['error' => "Invalid session, please re-login."]);
}
} else {
header('HTTP/1.1 500 Internal Server Error');
}
}
<?php
header('Content-type: application/json');
require_once 'common.php';
require_once BASE_PATH . 'inc/Broadworks_OCI-P/utils/OCIJobControl.php';
require_once BASE_PATH . 'inc/Broadworks_OCI-P/core/OCISession.php';
require 'Predis/Autoloader.php';
Predis\Autoloader::register();
$redis = new Predis\Client();
foreach ($redis->keys('dmsjob:*') as $item) {
$id = explode(':', $item)[1];
$file = $redis->get("dmsjob:$id");
$session = unserialize($redis->get("dmssession:$id"));
$control = new OCIPJobControl(null, null, null, $session);
if ($control) {
$total = $control->addJobsCSV($file);
while ($job = $control->getNext()) {
$joblog = print_r($job, true);
$execlog = ($log = $control->execute($job))
? print_r($log, true)
: $errorControl->getLastError();
$redis->rpush("dmslog:$id", $joblog);
$redis->rpush("dmslog:$id", $execlog);
$percent = round($control->getCurrent() / $total * 100);
$redis->set("dmsprogress:$id", $percent);
}
$redis->del("dmsjob:$id");
$redis->del("dmssession:$id");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment