PHP Classes

Asynchronous Long Server Tasks: Start background tasks and query progress status

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 73%Total: 761 All time: 4,436 This week: 73Up
Version License PHP version Categories
long-server-tasks 1.0.6MIT/X Consortium ...5.3HTTP, PHP 5, Language
Description 

Author

This package can start background tasks and query progress status.

There is a class that can start a background task by sending a HTTP request to a URL of a script that do the actual work on the background.

Another class can query the task progress status by polling on a given status file.

The status responses can be encoded in JSON to make it easy for processing from browser side AJAX based applications.

Innovation Award
PHP Programming Innovation award winner
September 2015
Winner


Prize: One subscription to the PDF edition of the PHP Architect magazine
Sometimes applications need to perform tasks that time a long time to complete, like for instance sending a newsletter to many subscribers.

If you send a request from the browser to execute such long server tasks, the browser will stall waiting for the job to finish.

This package provides an alternative solution that frees the browser once the long server task is started.

It also provides means to poll the server to learn about the status of progress of the long task that is still running.

Manuel Lemos
Picture of Joseluis Laso
  Performance   Level  
Name: Joseluis Laso <contact>
Classes: 16 packages by
Country: Spain Spain
Age: 56
All time rank: 92219 in Spain Spain
Week rank: 216 Up8 in Spain Spain Up
Innovation award
Innovation award
Nominee: 6x

Winner: 2x

Example

<html>

<head>

    <style>
        body{
            height: 100%;
            width: 100%;
            background: lightcyan repeat;
            margin: 0;
            padding: 0;
        }
        #container{
            position: relative;
            width: 100%;
            height: 50%;
        }
        #content{
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }
        #status{
            position: fixed;
            overflow: auto;
            height: 50%;
            bottom: 0;
            width: 100%;
            border: 1px solid black;
            padding: 0;
            margin: 0;
        }
    </style>
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
</head>

<body>

<div id="container">
    <div id="content">
        <input id="start-task" type="button" value="Start task" task="1">
    </div>
</div>

<div id="status">

</div>

</body>

<script>

    function logMessage(message){
        if ($("#status").html().length > 1000) {
            $("#status").html(message+"<br/>");
        }else{
            $("#status").append(message+"<br/>");
        }

    }

    function pollingStatus()
    {
        $.ajax({
            url: '/status-task.php',
            data: { id: id },
            type: 'POST',
            success: function(data){
                if (data.result){
                    logMessage("task "+data.id+" "+data.status);
                    if (data.status == "done"){
                        clearInterval(interval);
                        alert("The process is done, you can refresh the win")
                    }
                }else{
                    logMessage(data.reason);
                }
            },
            error: function(){
                alert("something wrong was happened");
            }
        });
    }

    var interval = 0;
    var id = 0;

    $(function(){
        logMessage("Welcome to asynchronous server side task demo");

        $("#start-task").click(function(e){
            e.preventDefault();

            id = $("#start-task").attr("task");
            $.ajax({
                url: '/start-task.php',
                data: {
                    id: id,
                    _task: "task"
                },
                type: 'POST',
                success: function(data){
                    //logMessage(data);
                    if (data.result){
                        logMessage("task "+data.id+" added to the system, will be executed soon!");
                        interval = setInterval(pollingStatus, 1000);
                    }else{
                        logMessage(data.reason);
                    }
                },
                error: function(){
                    alert("something wrong was happened");
                }
            });

            return false;
        })
    })
</script>

</html>


Details

asynchronous-server-tasks

A wrapper to implement asynchronous server taks in your PHP project

remember to run first `composer dumpautoload` in order to generate the appropriate autoload files

To run the example:

create a virtual a host with "your-local-name" in your development environment:

now, go to your browser and write http://your-local-name and you'll see a simple page with a button to start a new task and an area to see messages.

A little complex example

This example serves as a base to document the second part of the article on PHP Classes

Go in your browser to http://your-local-name/file-list.php


  Files folder image Files (24)  
File Role Description
Files folder imagepublic (9 files, 2 directories)
Files folder imagesrc (4 files)
Files folder imagetests (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (24)  /  public  
File Role Description
Files folder imageimg (4 files)
Files folder imageserver (3 files)
  Accessible without login Plain text file copy-multi.php Example new example third part
  Accessible without login Plain text file copy.php Example Example script
  Accessible without login Plain text file file-list-multi.php Example new example third part
  Accessible without login Plain text file file-list.php Example Example script
  Accessible without login Plain text file index.php Example Example script
  Accessible without login Plain text file start-task.php Example Example script
  Accessible without login Plain text file status-copy-multi.php Example new example third part
  Accessible without login Plain text file status-copy.php Example Example script
  Accessible without login Plain text file status-task.php Example Example script

  Files folder image Files (24)  /  public  /  img  
File Role Description
  Accessible without login Image file alert.png Icon Icon image
  Accessible without login Image file checkmark.png Icon Icon image
  Accessible without login Image file loader.gif Icon Icon image
  Accessible without login Image file progress.gif Icon Icon image

  Files folder image Files (24)  /  public  /  server  
File Role Description
  Accessible without login Plain text file copy-file-multi.php Example new example third part
  Accessible without login Plain text file copy-file.php Example Example script
  Accessible without login Plain text file task.php Example Example script

  Files folder image Files (24)  /  src  
File Role Description
  Accessible without login Plain text file CommonAbstract.php Class Class source
  Accessible without login Plain text file Json.php Class Class source
  Accessible without login Plain text file Starter.php Class Class source
  Accessible without login Plain text file Status.php Class Class source

  Files folder image Files (24)  /  tests  
File Role Description
  Accessible without login Plain text file status-list.php Example Example script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:761
This week:0
All time:4,436
This week:73Up
 User Ratings  
 
 All time
Utility:100%StarStarStarStarStarStar
Consistency:91%StarStarStarStarStar
Documentation:66%StarStarStarStar
Examples:75%StarStarStarStar
Tests:-
Videos:-
Overall:73%StarStarStarStar
Rank:127