Last active
December 20, 2015 16:58
-
-
Save prodeveloper/6165061 to your computer and use it in GitHub Desktop.
Controller for long running process blog.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Chencha Jacob Gisore | |
* @link www.chenchatech.com | |
*/ | |
require_once 'queue_manager.php'; | |
require_once 'helpers.php'; | |
$controller = new Controller(); | |
$request = isset($_GET['request']) ? $_GET['request'] : false; | |
$progress = isset($_GET['progress']) ? $_GET['progress'] : FALSE; | |
//Request not specified return bad request | |
if (!$request) { | |
$response = "No request provided"; | |
return_value($response, 400); | |
die(); | |
} | |
//Act if user requested progress information | |
if ($progress) { | |
$curr_progress = $controller -> check_progress($request); | |
//Return resouce if we are finished processing | |
if($curr_progress=='Complete'){ | |
$curr_progress=$controller->get_resource($request); | |
} | |
return_value($curr_progress, 200); | |
} | |
//Add job to queue | |
else { | |
$controller -> queue($request); | |
$response = "Request received please wait while we process"; | |
return_value($response, 201); | |
} | |
/** | |
* | |
*/ | |
class Controller extends Queue_manager{ | |
function __construct() { | |
} | |
/** | |
* Check progress of request | |
* @param {String} $request | |
*/ | |
function check_progress($request) { | |
return self::progress($request); | |
} | |
function get_resource($request){ | |
return self::get_resource($request); | |
} | |
/** | |
* Add request to queue | |
* @param {String} $request | |
*/ | |
function queue($request) { | |
self::add($request); | |
return TRUE; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* @author Chencha Jacob Gisore | |
* @link www.chenchatech.com | |
* | |
* uses curl to get html conten | |
* @param {String} url The url to be fetched | |
*/ | |
function curl_get_contents($url, $post = false, $touch = false) { | |
// Initiate the curl session | |
$ch = curl_init(); | |
// Set the URL | |
curl_setopt($ch, CURLOPT_URL, $url); | |
// Removes the headers from the output | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30")); | |
if ($touch) { | |
//Limit time to connection to at most 200 secs | |
curl_setopt($ch, CURLOPT_TIMEOUT, 1); | |
} else {//Limit time to connection to at most 200 secs | |
curl_setopt($ch, CURLOPT_TIMEOUT, 200); | |
} | |
//curl_setopt($ch, CURLOPT_REFERER, 'http://nlp.stanford.edu:8080/'); | |
//Follow redirects | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | |
//If we have post data then include it here | |
if ($post) { | |
//Set HTTP POST Request. | |
curl_setopt($ch, CURLOPT_POST, TRUE); | |
//Set data to POST in HTTP "POST" Operation. | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); | |
} | |
// Return the output instead of displaying it directly | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
// Execute the curl session | |
$output = curl_exec($ch); | |
// Close the curl session | |
curl_close($ch); | |
// Return the output as a variable | |
return $output; | |
} | |
/** | |
* Returns value to the user | |
*/ | |
function return_value($response, $status = 200) { | |
http_response_code(200); | |
print $response; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
/** | |
* @author Chencha Jacob Gisore | |
* @link www.chenchatech.com | |
*/ | |
/** | |
*This will handle the data model | |
*/ | |
class Model { | |
function __construct() { | |
} | |
static function save_progress($data) { | |
file_put_contents('progress.txt', $data . '%'); | |
return TRUE; | |
} | |
static function retreive_progress(){ | |
return file_get_contents('progress.txt'); | |
} | |
static function save_data($content) { | |
file_put_contents('store.txt', $content); | |
file_put_contents('progress.txt', 'Complete'); | |
} | |
static function retrieve_data() { | |
return file_get_contents('store.txt'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'helpers.php'; | |
require_once 'model.php'; | |
/** | |
* @author Chencha Jacob Gisore | |
* @link www.chenchatech.com | |
*/ | |
class Queue_manager { | |
//Write the path to your worker here | |
static $path_to_worker='http://localhost/blog/long_running/worker.php'; | |
function __construct() { | |
} | |
static function add($request){ | |
//Send any parameters that the worker needs. | |
$request=http_build_query(compact('request'),'&'); | |
$url=self::$path_to_worker . "?$request"; | |
curl_get_contents($url,false,true); | |
} | |
static function progress($request){ | |
$data=Model::retreive_progress(); | |
return $data; | |
} | |
static function get_resource($request){ | |
return Model::retrieve_data(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment