This repository was 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
Expand file tree
/
Copy pathworker.php
More file actions
143 lines (114 loc) · 3.44 KB
/
Copy pathworker.php
File metadata and controls
143 lines (114 loc) · 3.44 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
// Using gearman 0.28
$worker= new GearmanWorker();
$worker->addServer('127.0.0.1');
$worker->addFunction("get_weather", "get_weather");
$worker->addFunction("kill_job", "kill_job");
console("Started worker script.");
// This is the "worker" loop, endless (unless there is an error...)
// How would we detect this in a real-world situation? You could daemonize this
// script but that doesn't seem ideal solution.
while ($worker->work())
{
console("--------------------------");
console("Waiting for new job");
if ($worker->returnCode() != GEARMAN_SUCCESS)
{
console("It brokes: " . $worker->returnCode());
break;
}
}
/**
* Log something to the console
* @param string $text
*/
function console($text)
{
echo "[" . getmypid() . "] " . date("Y-m-d H:i:s") . " $text\n";
}
/**
* Function to kill another job
*/
function kill_job($job)
{
console("New job: " . $job->handle() . " (" . __FUNCTION__ . ")");
$data = json_decode($job->workload());
// kill job defined in $data
$pidfile = sys_get_temp_dir() . "/gm_pid_" . md5($data->job_handle);
$pid = file_get_contents($pidfile);
if($pid)
{
console("Killing handle {$data->job_handle} (pid={$pid})");
$norestart = sys_get_temp_dir() . "/gm_norestart_" . md5($data->job_handle);
touch($norestart);
$cmd = "kill -9 {$pid}";
$errno = 0;
$output = array();
exec($cmd . " 2>&1", $output, $errno);
if($errno == 0)
{
console("Killed.");
}
else
{
consoled("Failed to kill pid");
}
}
else
{
console("pidfile for {$data->job_handle} not found");
}
console("Done");
return true;
}
/**
* Get weather information
* @param GearmanJob $job
*/
function get_weather($job)
{
console("New job: " . $job->handle() . " (" . __FUNCTION__ . ")");
$norestart = sys_get_temp_dir() . "/gm_norestart_" . md5($job->handle());
if(file_exists($norestart))
{
console("norestart exists, returning");
return;
}
$pidfile = sys_get_temp_dir() . "/gm_pid_" . md5($job->handle());
file_put_contents($pidfile, getmypid());
$data = json_decode($job->workload());
// Extract variables from provided data, with defaults
$location = $data->location ?: "portsmouth, uk";
$unit = $data->unit ?: "c";
// Build a YQL query
console("Building YQL query");
$yql_query = "use 'http://github.com/yql/yql-tables/raw/master/weather/weather.bylocation.xml' as we;";
$yql_query .= "select * from we where location=\"{$location}\" and unit='{$unit}'";
$yql_query_url = "https://query.yahooapis.com/v1/public/yql?q=" . urlencode($yql_query) . "&format=json";
// Make call with cURL
console("Executing YQL query");
$ch = curl_init($yql_query_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
// Tidy up the returned JSON
console("Extracting relevant data");
$obj = json_decode($json);
$new_json = json_encode($obj->query->results->weather->rss->channel->item);
// Sleep for fun...
console("Falling asleep here");
$sleep_for = 10;
for($i = 1; $i <= $sleep_for; $i++)
{
console("Sending Status $i/$sleep_for");
$job->sendStatus($i,$sleep_for);
sleep(1);
}
// Write the data to /tmp for client-nonblocking background worker
// In a realworld we'd write this data to somewhere else, maybe a database
// or something, as the client might not have access to local filesystem!
$datafile = sys_get_temp_dir() . "/gm_data_" . md5($job->handle());
file_put_contents($datafile, $new_json);
console("Done, returning weather");
unlink($pidfile);
return $new_json;
}