-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcron.php
59 lines (46 loc) · 1.93 KB
/
cron.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
<?php
// SurfEdge - Making Lives Smarter
// www.surfedge.lk - 2019.
// This is used to run few ISYD controllers in a minute basis
// You can use the cron functionality of your server and configure it using cron URL * * * *
// Function Request is used to send the GET request to the contorller (modify if needed)
// Set the Default timezone as Colombo to get accurate timestamps
date_default_timezone_set('Asia/Colombo');
$executionStartTime = microtime(true);
$date = date('m/d/Y H:i:s', time());
// Use the request function to call all the ISYD endpoints
// request( URL , NAME, TIME_TO_RUN_AT - optional );
request('https://sv2.ideamarthosting.dialog.lk/APP_NAME/controller.php', 'onthisday');
request('ANY_URL', 'quote_of_the_day');
request('https://sv2.ideamarthosting.dialog.lk/APP_NAME/controller.php', 'daily_news', '20:00');
request('https://sv2.ideamarthosting.dialog.lk/APP_NAME/controller.php', 'weekly_winners', '20:00','Sun'); // Runs only on sundays at 8PM
// Logging
$executionEndTime = microtime(true);
$seconds = round($executionEndTime - $executionStartTime, 3);
echo "CRON RAN AT $data took $seconds seconds";
flog('cron_access', $date . ' > ' . $seconds);
function request($url, $name, $run_at = null, $has_date = null)
{
// There might be instance where you want to run an endpoint in a specific point in time of the day
if($run_at && date('H:i', time()) != $run_at ){
return;
}
// Check if its the day of the week
if($has_date && date('D') != $has_date){
return;
}
$date = date('m/d/Y H:i:s', time());
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'ISYD Cron',
]);
$resp = curl_exec($curl);
flog($name . '_access', $date . ' > ' . $resp);
curl_close($curl);
}
function flog($file_name, $text)
{
file_put_contents($file_name . '.txt', $text . PHP_EOL, FILE_APPEND | LOCK_EX);
}