Skip to content

Commit 72312ab

Browse files
Handle and cache meetup API failures
1 parent 168bd1f commit 72312ab

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

php-classes/RemoteSystems/Meetup.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
namespace RemoteSystems;
44

55
use Cache;
6+
use Exception;
67

78
class Meetup
89
{
910
public static $groupUrl;
1011
public static $signedEventsUrl;
11-
public static $defaultCacheTime = 1800;
12+
public static $defaultCacheTime = 600;
13+
public static $failureCacheTime = 60;
1214
public static $eventsFilter;
1315

1416
public static function getEventUrl($meetupID)
@@ -31,9 +33,18 @@ public static function getEvents($ttl = null)
3133
}
3234

3335
$cacheKey = 'meetup/events';
36+
$data = Cache::fetch($cacheKey);
3437

35-
if (!$data = Cache::fetch($cacheKey)) {
36-
$data = json_decode(file_get_contents(static::$signedEventsUrl), true);
38+
if ($data === null) {
39+
// cached failure
40+
throw new Exception('Meetup API unavailable');
41+
} elseif ($data === false) {
42+
$data = @json_decode(@file_get_contents(static::$signedEventsUrl), true);
43+
44+
if (!$data) {
45+
Cache::store($cacheKey, null, static::$failureCacheTime);
46+
throw new Exception('Meetup API unavailable');
47+
}
3748

3849
if (static::$eventsFilter) {
3950
if (is_string(static::$eventsFilter)) {

site-root/home.php

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,33 @@
66

77

88
// meetups
9-
$meetups = RemoteSystems\Meetup::getEvents();
10-
$nextMeetup = array_shift($meetups);
9+
try {
10+
$meetups = RemoteSystems\Meetup::getEvents();
1111

12-
// detect if meetup is happening right now
13-
if($nextMeetup && $nextMeetup['time'] < $now) {
14-
$currentMeetup = $nextMeetup;
1512
$nextMeetup = array_shift($meetups);
13+
14+
// detect if meetup is happening right now
15+
if($nextMeetup && $nextMeetup['time'] < $now) {
16+
$currentMeetup = $nextMeetup;
17+
$nextMeetup = array_shift($meetups);
18+
}
19+
20+
// TODO: delete this!
21+
elseif(!empty($_GET['force_current'])) {
22+
$currentMeetup = $nextMeetup;
23+
}
24+
25+
if($currentMeetup) {
26+
$currentMeetup['checkins'] = Laddr\MemberCheckin::getAllForMeetupByProject($currentMeetup['id']);
27+
}
28+
29+
$pageData['currentMeetup'] = $currentMeetup;
30+
$pageData['nextMeetup'] = $nextMeetup;
31+
$pageData['futureMeetups'] = $meetups;
32+
} catch (Exception $e) {
33+
// just omit meetup data
1634
}
1735

18-
// TODO: delete this!
19-
elseif(!empty($_GET['force_current'])) {
20-
$currentMeetup = $nextMeetup;
21-
}
22-
23-
if($currentMeetup) {
24-
$currentMeetup['checkins'] = Laddr\MemberCheckin::getAllForMeetupByProject($currentMeetup['id']);
25-
}
26-
27-
$pageData['currentMeetup'] = $currentMeetup;
28-
$pageData['nextMeetup'] = $nextMeetup;
29-
$pageData['futureMeetups'] = $meetups;
30-
3136

3237
// projects
3338
$pageData['projectsTotal'] = Laddr\Project::getCount();

0 commit comments

Comments
 (0)