Skip to content

Commit

Permalink
Add last HTTP code received for the upstream contestSource
Browse files Browse the repository at this point in the history
Exposes when externalContestSource (URI) returns a non HTTP 200.

To trigger this: create a contest with http://URL// (the double // is
important)
  • Loading branch information
vmcj committed Oct 7, 2023
1 parent 82a1cd0 commit f6d2960
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
34 changes: 34 additions & 0 deletions webapp/migrations/Version20231006185028.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20231006185028 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add the last received HTTP code for the external contest source.';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE external_contest_source ADD last_httpcode SMALLINT UNSIGNED DEFAULT NULL COMMENT \'Last HTTP code received by event feed reader\'');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE external_contest_source DROP last_httpcode');
}

public function isTransactional(): bool
{
return false;
}
}
18 changes: 18 additions & 0 deletions webapp/src/Entity/ExternalContestSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ class ExternalContestSource
)]
private ?float $lastPollTime = null;

#[ORM\Column(
type: 'smallint',
nullable: true,
options: ['comment' => 'Last HTTP code received by event feed reader', 'unsigned' => true]
)]
public ?int $lastHTTPCode = null;

#[ORM\ManyToOne(inversedBy: 'externalContestSources')]
#[ORM\JoinColumn(name: 'cid', referencedColumnName: 'cid', onDelete: 'CASCADE')]
private Contest $contest;
Expand Down Expand Up @@ -176,6 +183,17 @@ public function getShortDescription(): string
return $this->getSource();
}

public function setLastHTTPCode(?int $lastHTTPCode): ExternalContestSource
{
$this->lastHTTPCode = $lastHTTPCode;
return $this;
}

public function getLastHTTPCode(): ?int
{
return $this->lastHTTPCode;
}

#[Assert\Callback]
public function validate(ExecutionContextInterface $context): void
{
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/Service/DOMJudgeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ public function getUpdates(): array
->select('ecs.extsourceid', 'ecs.lastPollTime')
->from(ExternalContestSource::class, 'ecs')
->andWhere('ecs.contest = :contest')
->andWhere('ecs.lastPollTime < :i OR ecs.lastPollTime is NULL')
->andWhere('ecs.lastPollTime < :i OR ecs.lastPollTime is NULL OR ecs.lastHTTPCode != 200')
->setParameter('contest', $contest)
->setParameter('i', time() - $this->config->get('external_contest_source_critical'))
->getQuery()->getOneOrNullResult();
Expand Down
7 changes: 5 additions & 2 deletions webapp/src/Service/ExternalContestSourceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,14 @@ protected function importFromCcsApi(array $eventsToSkip, ?callable $progressRepo
$fullUrl .= '&since_token=' . $this->getLastReadEventId();
}
$response = $this->httpClient->request('GET', $fullUrl, ['buffer' => false]);
if ($response->getStatusCode() !== 200) {
$statusCode = $response->getStatusCode();
$this->source->setLastHTTPCode($statusCode);
$this->em->flush();
if ($statusCode !== 200) {
$this->logger->warning(
'Received non-200 response code %d, waiting for five seconds ' .
'and trying again. Press ^C to quit.',
[$response->getStatusCode()]
[$statusCode]
);
sleep(5);
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
<td>
{% if not externalContestSource.lastPollTime %}
Event feed reader never checked in.
{% elseif externalContestSource.lastHTTPCode is not same as(200) %}
Received {{ externalContestSource.lastHTTPCode }} HTTP code.
{% else %}
{{ status }}, last checked in {{ externalContestSource.lastPollTime | printtimediff }}s ago.
{% endif %}
Expand Down

0 comments on commit f6d2960

Please sign in to comment.