mirror of
https://github.com/standardebooks/web.git
synced 2025-07-06 23:00:28 -04:00
Pull out apcu code into a function to improve error handling
This commit is contained in:
parent
0bf50873ac
commit
beda605f32
4 changed files with 43 additions and 60 deletions
|
@ -1,7 +1,6 @@
|
||||||
<?
|
<?
|
||||||
// Auto-included by Composer in composer.json to satisfy PHPStan
|
// Auto-included by Composer in composer.json to satisfy PHPStan
|
||||||
use function Safe\define;
|
use function Safe\define;
|
||||||
use function Safe\file_get_contents;
|
|
||||||
use function Safe\gmdate;
|
use function Safe\gmdate;
|
||||||
use function Safe\strtotime;
|
use function Safe\strtotime;
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,10 @@
|
||||||
use PHPMailer\PHPMailer\PHPMailer;
|
use PHPMailer\PHPMailer\PHPMailer;
|
||||||
use PHPMailer\PHPMailer\Exception;
|
use PHPMailer\PHPMailer\Exception;
|
||||||
|
|
||||||
|
use function Safe\define;
|
||||||
|
use function Safe\file_get_contents;
|
||||||
|
|
||||||
define('EMAIL_SMTP_USERNAME', trim(file_get_contents(POSTMARK_SECRET_FILE_PATH)) ?: '');
|
define('EMAIL_SMTP_USERNAME', trim(file_get_contents(POSTMARK_SECRET_FILE_PATH)) ?: '');
|
||||||
const EMAIL_SMTP_PASSWORD = EMAIL_SMTP_USERNAME;
|
|
||||||
|
|
||||||
class Email{
|
class Email{
|
||||||
public $To = '';
|
public $To = '';
|
||||||
|
@ -54,7 +56,7 @@ class Email{
|
||||||
$phpMailer->Port = 587;
|
$phpMailer->Port = 587;
|
||||||
$phpMailer->Host = EMAIL_SMTP_HOST;
|
$phpMailer->Host = EMAIL_SMTP_HOST;
|
||||||
$phpMailer->Username = EMAIL_SMTP_USERNAME;
|
$phpMailer->Username = EMAIL_SMTP_USERNAME;
|
||||||
$phpMailer->Password = EMAIL_SMTP_PASSWORD;
|
$phpMailer->Password = EMAIL_SMTP_USERNAME;
|
||||||
|
|
||||||
if($this->PostmarkStream !== null){
|
if($this->PostmarkStream !== null){
|
||||||
$phpMailer->addCustomHeader('X-PM-Message-Stream', $this->PostmarkStream);
|
$phpMailer->addCustomHeader('X-PM-Message-Stream', $this->PostmarkStream);
|
||||||
|
|
|
@ -101,64 +101,22 @@ class Library{
|
||||||
* @return array<Ebook>
|
* @return array<Ebook>
|
||||||
*/
|
*/
|
||||||
public static function GetEbooks(): array{
|
public static function GetEbooks(): array{
|
||||||
$ebooks = [];
|
|
||||||
|
|
||||||
// Get all ebooks, unsorted.
|
// Get all ebooks, unsorted.
|
||||||
try{
|
return self::GetFromApcu('ebooks');
|
||||||
$ebooks = apcu_fetch('ebooks');
|
|
||||||
}
|
|
||||||
catch(Safe\Exceptions\ApcuException $ex){
|
|
||||||
Library::RebuildCache();
|
|
||||||
try{
|
|
||||||
$ebooks = apcu_fetch('ebooks');
|
|
||||||
}
|
|
||||||
catch(Safe\Exceptions\ApcuException $ex){
|
|
||||||
// We can get here if the cache is currently rebuilding from a different process.
|
|
||||||
// Nothing we can do but wait, so wait 20 seconds before retrying
|
|
||||||
sleep(20);
|
|
||||||
|
|
||||||
try{
|
|
||||||
$ebooks = apcu_fetch('ebooks');
|
|
||||||
}
|
|
||||||
catch(Safe\Exceptions\ApcuException $ex){
|
|
||||||
// Cache STILL rebuilding... give up silently for now
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $ebooks;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array<Ebook>
|
* @return array<Ebook>
|
||||||
*/
|
*/
|
||||||
public static function GetEbooksByAuthor(string $wwwFilesystemPath): array{
|
public static function GetEbooksByAuthor(string $wwwFilesystemPath): array{
|
||||||
// Do we have the author's ebooks cached?
|
return self::GetFromApcu('author-' . $wwwFilesystemPath);
|
||||||
$ebooks = [];
|
|
||||||
|
|
||||||
try{
|
|
||||||
$ebooks = apcu_fetch('author-' . $wwwFilesystemPath);
|
|
||||||
}
|
|
||||||
catch(Safe\Exceptions\ApcuException $ex){
|
|
||||||
}
|
|
||||||
|
|
||||||
return $ebooks;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array<Ebook>
|
* @return array<Ebook>
|
||||||
*/
|
*/
|
||||||
public static function GetEbooksByTag(string $tag): array{
|
public static function GetEbooksByTag(string $tag): array{
|
||||||
// Do we have the tag's ebooks cached?
|
return self::GetFromApcu('tag-' . $tag);
|
||||||
$ebooks = [];
|
|
||||||
|
|
||||||
try{
|
|
||||||
$ebooks = apcu_fetch('tag-' . $tag);
|
|
||||||
}
|
|
||||||
catch(Safe\Exceptions\ApcuException $ex){
|
|
||||||
}
|
|
||||||
|
|
||||||
return $ebooks;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -166,23 +124,45 @@ class Library{
|
||||||
*/
|
*/
|
||||||
public static function GetEbooksByCollection(string $collection): array{
|
public static function GetEbooksByCollection(string $collection): array{
|
||||||
// Do we have the tag's ebooks cached?
|
// Do we have the tag's ebooks cached?
|
||||||
$ebooks = [];
|
return self::GetFromApcu('collection-' . $collection);
|
||||||
|
|
||||||
try{
|
|
||||||
$ebooks = apcu_fetch('collection-' . $collection);
|
|
||||||
}
|
|
||||||
catch(Safe\Exceptions\ApcuException $ex){
|
|
||||||
}
|
|
||||||
|
|
||||||
return $ebooks;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array<Tag>
|
* @return array<Tag>
|
||||||
*/
|
*/
|
||||||
public static function GetTags(): array{
|
public static function GetTags(): array{
|
||||||
return apcu_fetch('tags');
|
return self::GetFromApcu('tags');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<mixed>
|
||||||
|
*/
|
||||||
|
private static function GetFromApcu(string $variable): array{
|
||||||
|
$results = [];
|
||||||
|
|
||||||
|
try{
|
||||||
|
$results = apcu_fetch($variable);
|
||||||
|
}
|
||||||
|
catch(Safe\Exceptions\ApcuException $ex){
|
||||||
|
Library::RebuildCache();
|
||||||
|
try{
|
||||||
|
$results = apcu_fetch($variable);
|
||||||
|
}
|
||||||
|
catch(Safe\Exceptions\ApcuException $ex){
|
||||||
|
// We can get here if the cache is currently rebuilding from a different process.
|
||||||
|
// Nothing we can do but wait, so wait 20 seconds before retrying
|
||||||
|
sleep(20);
|
||||||
|
|
||||||
|
try{
|
||||||
|
$results = apcu_fetch($variable);
|
||||||
|
}
|
||||||
|
catch(Safe\Exceptions\ApcuException $ex){
|
||||||
|
// Cache STILL rebuilding... give up silently for now
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -12,6 +12,8 @@ use function Safe\substr;
|
||||||
$requestId = substr(sha1(time() . rand()), 0, 8);
|
$requestId = substr(sha1(time() . rand()), 0, 8);
|
||||||
|
|
||||||
try{
|
try{
|
||||||
|
$smtpUsername = trim(file_get_contents(POSTMARK_SECRET_FILE_PATH)) ?: '';
|
||||||
|
|
||||||
Logger::WritePostmarkWebhookLogEntry($requestId, 'Received Postmark webhook.');
|
Logger::WritePostmarkWebhookLogEntry($requestId, 'Received Postmark webhook.');
|
||||||
|
|
||||||
if($_SERVER['REQUEST_METHOD'] != 'POST'){
|
if($_SERVER['REQUEST_METHOD'] != 'POST'){
|
||||||
|
@ -50,7 +52,7 @@ try{
|
||||||
$handle = curl_init();
|
$handle = curl_init();
|
||||||
curl_setopt($handle, CURLOPT_URL, 'https://api.postmarkapp.com/message-streams/' . $post->MessageStream . '/suppressions/delete');
|
curl_setopt($handle, CURLOPT_URL, 'https://api.postmarkapp.com/message-streams/' . $post->MessageStream . '/suppressions/delete');
|
||||||
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
|
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
|
||||||
curl_setopt($handle, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', 'X-Postmark-Server-Token: ' . EMAIL_SMTP_USERNAME]);
|
curl_setopt($handle, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', 'X-Postmark-Server-Token: ' . $smtpUsername]);
|
||||||
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST");
|
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST");
|
||||||
curl_setopt($handle, CURLOPT_POSTFIELDS, '{"Suppressions": [{"EmailAddress": "' . $email . '"}]}');
|
curl_setopt($handle, CURLOPT_POSTFIELDS, '{"Suppressions": [{"EmailAddress": "' . $email . '"}]}');
|
||||||
curl_exec($handle);
|
curl_exec($handle);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue