Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Jobs\backlogUpdate;

class Kernel extends ConsoleKernel
{
Expand All @@ -24,8 +25,7 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
$schedule->job(new backlogUpdate)->dailyAt('13:00');
}

/**
Expand Down
68 changes: 68 additions & 0 deletions app/Jobs/BacklogUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace App\Jobs;

use GuzzleHttp\Exception\RequestException;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use App\Models\Item;
use GuzzleHttp\Client;
use Sentry\Severity;
use Sentry\State\Scope;

class BacklogUpdate implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$webhook = config('services.discord.webhooks.updates');
$published = DB::table('items')->where('status', '=', Item::PUBLISHED)->count();
$pending = DB::table('items')->where('status', '=', Item::PENDING)->count();
$changes = DB::table('items')->where('status', '=', Item::CHANGES_REQUESTED)->count();
$draft = DB::table('items')->where('status', '=', Item::DRAFT)->count();

$msg = <<<EOD
## *Current Entries*
**$draft** drafts
**$pending** pending review
**$changes** post-review, changes requested
**$published** published


EOD;

$client = new Client();
$res = $client->request('POST', $webhook, ["json" => ["content" => $msg]]);

if ($res->getStatusCode() !== 200) {
$scope = new Scope();
$scope->setContext('response', [
'body' => $res->getBody()->getContents(),
'status_code' => $res->getStatusCode(),
'headers' => $res->getHeaders(),
]);

app('sentry')->reportMessage('Failed to send discord webhook', Severity::error(), $scope);
}
}
}
73 changes: 73 additions & 0 deletions bin/healthcheck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import requests
import os
import urllib3

from requests.adapters import HTTPAdapter
from http.server import BaseHTTPRequestHandler, HTTPServer

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

hostname = os.getenv("HEALTH_CHECK_HOST", 'lolibrary.org')

class HostHeaderSSLAdapter(HTTPAdapter):
"""
A HTTPS Adapter for Python Requests that sets the hostname for certificate
verification based on the Host header.

This allows requesting the IP address directly via HTTPS without getting
a "hostname doesn't match" exception.

Example usage:

>>> s.mount('https://', HostHeaderSSLAdapter())
>>> s.get("https://93.184.216.34", headers={"Host": "example.org"})

"""
def send(self, request, **kwargs):
# HTTP headers are case-insensitive (RFC 7230)
host_header = None
for header in request.headers:
if header.lower() == "host":
host_header = request.headers[header]
break

connection_pool_kwargs = self.poolmanager.connection_pool_kw

if host_header:
connection_pool_kwargs["assert_hostname"] = host_header
connection_pool_kwargs["server_hostname"] = host_header
elif "assert_hostname" in connection_pool_kwargs:
# an assert_hostname from a previous request may have been left
connection_pool_kwargs.pop("assert_hostname", None)
connection_pool_kwargs.pop("server_hostname", None)

return super(HostHeaderSSLAdapter, self).send(request, **kwargs)

class Server(BaseHTTPRequestHandler):
def do_GET(self):
try:
res = check()
if res.status_code != 200:
raise Exception(res.json())
self.send_response(200)
self.end_headers()
self.wfile.write(b'UP')
except:
self.send_response(503)
self.end_headers()
self.wfile.write(b'DOWN')

def check():
s = requests.Session()
s.mount('https://', HostHeaderSSLAdapter())
return s.get('https://localhost/', headers={'Host': hostname}, verify=False)

def main(server_class=HTTPServer, handler_class=Server, port=12443):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print("running checks on", port)
httpd.serve_forever()
httpd.server_close()

if __name__ == '__main__':
main()
8 changes: 8 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],

'discord' => [
'webhooks' => [
'updates' => env("DISCORD_UPDATE_WEBHOOK"),
'publish' => env("DISCORD_PUBLISH_WEBHOOK"),
'draft' => env("DISCORD_DRAFT_WEBHOOK"),
],
],

];
Loading