forked from Automattic/vip-go-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
other-web-services.php
234 lines (197 loc) · 3.51 KB
/
other-web-services.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/*
* Queue up a message for IRC API,
* or alternatively empty the queue and
* return its contents.
*/
function vipgoci_irc_api_alert_queue(
$message = null,
$dump = false
) {
static $msg_queue = array();
if ( true === $dump ) {
$msg_queue_tmp = $msg_queue;
$msg_queue = array();
return $msg_queue_tmp;
}
$msg_queue[] = $message;
}
/**
* Empty IRC message queue and send off
* to the IRC API.
*
* @codeCoverageIgnore
*/
function vipgoci_irc_api_alerts_send(
$irc_api_url,
$irc_api_token,
$botname,
$channel
) {
$msg_queue = vipgoci_irc_api_alert_queue(
null, true
);
vipgoci_log(
'Sending messages to IRC API',
array(
'msg_queue' => $msg_queue,
)
);
foreach( $msg_queue as $message ) {
$irc_api_postfields = array(
'message' => $message,
'botname' => $botname,
'channel' => $channel,
);
$ch = curl_init();
curl_setopt(
$ch, CURLOPT_URL, $irc_api_url
);
curl_setopt(
$ch, CURLOPT_RETURNTRANSFER, 1
);
curl_setopt(
$ch, CURLOPT_CONNECTTIMEOUT, 5
);
curl_setopt(
$ch, CURLOPT_USERAGENT, VIPGOCI_CLIENT_ID
);
curl_setopt(
$ch, CURLOPT_POST, 1
);
curl_setopt(
$ch,
CURLOPT_POSTFIELDS,
json_encode( $irc_api_postfields )
);
curl_setopt(
$ch,
CURLOPT_HEADERFUNCTION,
'vipgoci_curl_headers'
);
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array( 'Authorization: Bearer ' . $irc_api_token )
);
vipgoci_curl_set_security_options(
$ch
);
/*
* Execute query, keep record of how long time it
* took, and keep count of how many requests we do.
*/
vipgoci_runtime_measure( VIPGOCI_RUNTIME_START, 'irc_api_post' );
vipgoci_counter_report(
VIPGOCI_COUNTERS_DO,
'irc_api_request_post',
1
);
$resp_data = curl_exec( $ch );
vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'irc_api_post' );
$resp_headers = vipgoci_curl_headers(
null,
null
);
curl_close( $ch );
/*
* Enforce a small wait between requests.
*/
time_nanosleep( 0, 500000000 );
}
}
/**
* Send statistics to pixel API so
* we can keep track of actions we
* take during runtime.
*
* @codeCoverageIgnore
*/
function vipgoci_send_stats_to_pixel_api(
$pixel_api_url,
$stat_names_to_report,
$statistics
) {
vipgoci_log(
'Sending statistics to pixel API service',
array(
'stat_names_to_report' =>
$stat_names_to_report
)
);
$stat_names_to_groups = array(
);
foreach(
array_keys( $stat_names_to_report ) as
$statistic_group
) {
foreach(
$stat_names_to_report[
$statistic_group
] as $stat_name
) {
$stat_names_to_groups[
$stat_name
] = $statistic_group;
}
}
foreach(
$statistics as
$stat_name => $stat_value
) {
/*
* We are to report only certain
* values, so skip those who we should
* not report on.
*/
if ( false === array_key_exists(
$stat_name,
$stat_names_to_groups
) ) {
/*
* Not found, so nothing to report, skip.
*/
continue;
}
/*
* Compose URL.
*/
$url =
$pixel_api_url .
'?' .
'v=wpcom-no-pv' .
'&' .
'x_' . rawurlencode(
$stat_names_to_groups[
$stat_name
]
) .
'/' .
rawurlencode(
$stat_name
) . '=' .
rawurlencode(
$stat_value
);
/*
* Call service, do nothing with output.
* Specify a short timeout.
*/
$ctx = stream_context_create(
array(
'http' => array(
'timeout' => 5
)
)
);
file_get_contents( $url, 0, $ctx );
/*
* Sleep a short while between
* requests.
*/
time_nanosleep(
0,
500000000
);
}
}