PHP Classes

File: demo/demo-backend.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in Bookmark in
  Classes of Alexander Selifonov  >  PHP Long Running Operations  >  demo/demo-backend.php  >  Download  
File: demo/demo-backend.php
Role: Example script
Content type: text/plain
Description: Demo Backend (procedural)
Class: PHP Long Running Operations
Execute long operations split in small tasks
Author: By
Last change:
Date: 2 years ago
Size: 2,000 bytes


Class file image Download
* @name demo-backend.php - backend module for the demo.php
* Demonstration for class.longops.php
* @author Alexander Selifonov
* @license MIT



$params = array_merge($_GET,$_POST);

$params['longops_action'])) {
# 'maxtime': we give 2 seconds for one working session
$longop = new LongOps('myLongOperation', array('processid'=>'LONGOPT_DEMO', 'maxtime'=>2));
else die(
'Empty backend call (no longops_action passed) !');

myLongOperation($longoptObj, $opt=array()) {

$longoptObj->isAborted()) {

# Here make your "Before Abort" cleanup (close/delete unfinished files, drop temp tables in DB etc.)
        # After that you can call abortProcess();
$opt['message'] = 'Stopped by user at '.date('H:i:s');


$opt['lastItem'])) {
$opt['lastItem'] = 0;
$opt['itemCount'] = 50;
# Starting operation: Peform initial tasks (create output file, count items to work with etc.
    else {
# We're going to resume from paused job:
        # Re-open output file(s) and fseek to the end of it, restore other working parameters.

# Main working loop
while($opt['lastItem'] < $opt['itemCount']) {
# our long action works here:

$opt['lastItem'] += 1;
usleep(500000); # 0.5 sec delay simulates loong job...

if($longoptObj->isTimedOut($opt)) {
# here You have to close output file, to reopen it in next work-session for appending - fopen(fname,'a')
$opt['message'] = 'Processed:'.$opt['lastItem'].' of '.$opt['itemCount'] . ' items';
$longoptObj->pauseProcess($opt); # will exit($response);

# Here is the place for "final" cleanups, when the "long job" successfully finished.

$opt['message'] = 'Success, handled items: '.$opt['lastItem'];