Ratings | | Unique User Downloads | | Download Rankings |
73% | | Total: 761 | | All time: 4,436 This week: 73 |
|
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
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 |
| |
|
|
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
|
Applications that use this package |
|
No pages of applications that use this class were specified.
If you know an application of this package, send a message to the author to add a link here.