This repository has been archived by the owner on Mar 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-example.php
105 lines (99 loc) · 2.58 KB
/
web-example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
require_once("shared.php");
if(isset($_REQUEST["job"]))
{
$client = new GearmanClient();
$client->addServer("127.0.0.1");
switch($_REQUEST["job"])
{
case "start":
// Start a new background worker
$data = new stdClass();
$data->location = "portsmouth, uk";
$data->unit = "c";
$job_handle = $client->doBackground("get_weather", json_encode($data));
if($client->returnCode() != GEARMAN_SUCCESS)
{
echo json_encode(array("status" => "failed"));
}
else
{
echo json_encode(array("status" => "success", "job_handle" => $job_handle));
}
break;
case "status":
// Check the status of a background worker
$job_handle = $_REQUEST['job_handle'];
$status = $client->jobStatus($job_handle);
if($status[0] && !$status[1])
{
echo json_encode(array("status" => "queued"));
}
else if($status[0] && $status[1])
{
if($status[3] > 0)
{
$percent = ceil(($status[2] / $status[3]) * 100);
}
else
{
$percent = 0;
}
echo json_encode(array("status" => "running", "percent" => $percent));
}
else
{
echo json_encode(array("status" => "complete"));
}
break;
case "data":
// Retrieve the data
$job_handle = $_REQUEST['job_handle'];
$file = sys_get_temp_dir() . "/gm_data_" . md5($job_handle);
if(file_exists($file))
{
$json = file_get_contents($file);
$weather_text = format_yql_weather(json_decode($json));
}
else
{
$weather_text = '';
}
echo json_encode(array("data" => $weather_text));
break;
case "stop":
// Kill a running background worker!
$job_handle = $_REQUEST['job_handle'];
$data = new stdClass();
$data->job_handle = $job_handle;
$kill_job_handle = $client->doBackground("kill_job", json_encode($data));
if($client->returnCode() != GEARMAN_SUCCESS)
{
echo json_encode(array("status" => "failed"));
}
else
{
echo json_encode(array("status" => "success"));
}
break;
}
die();
}
?>
<html>
<head>
<title>Gearman Web Example</title>
<script src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js" type="text/javascript"></script>
<script src="web-example.js" type="text/javascript"></script>
<link rel="stylesheet" href="web-example.css" type="text/css" media="screen" />
</head>
<body>
<h1>Gearman Web Example</h1>
<ul>
<li><a href="javascript:;" id="link_start">Start Request</a></li>
<li><a href="javascript:;" id="link_stop">Stop Request</a></li>
<li><strong>Current status:</strong> <span id="current_status">Idle</span></li>
</ul>
<textarea id="output"></textarea>
</body>
</html>