Skip to content

Commit

Permalink
Fix deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-ritti committed Apr 15, 2022
1 parent 438cb3f commit 746ab79
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ CORS_ALLOW_ORIGIN='^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$'
TRUSTED_PROXIES=127.0.0.1,REMOTE_ADDR

BUCKET_MODE="path"
BUCKET_REQUEST_LIMIT=50
# Duration in hours
BUCKET_EXPIRE_AFTER=48
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ RUN rm -rf node_modules

ENV APP_ENV "prod"
ENV BUCKET_MODE "path"
ENV BUCKET_REQUEST_LIMIT 50
ENV BUCKET_EXPIRE_AFTER 48
ENV CORS_ALLOW_ORIGIN "^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$"
ENV TRUSTED_PROXIES "127.0.0.1,REMOTE_ADDR"

Expand Down
2 changes: 1 addition & 1 deletion config/packages/webpack_encore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ webpack_encore:
crossorigin: 'anonymous'

# Preload all rendered script and link tags automatically via the HTTP/2 Link header
preload: true
preload: false

# Throw an exception if the entrypoints.json file is missing or an entry is missing from the data
# strict_mode: false
Expand Down
10 changes: 7 additions & 3 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:
buckets_dirs: '%kernel.project_dir%/var/bins'
bucket_mode: '%env(default:default_bucket_mode:BUCKET_MODE)%'
default_bucket_mode: 'path'
buckets_request_limit: '%env(default:default_buckets_request_limit:BUCKET_REQUEST_LIMIT)%'
default_buckets_request_limit: 50
buckets_expire_after: '%env(default:default_buckets_expire_after:BUCKET_EXPIRE_AFTER)%'
default_buckets_expire_after: 48
buckets_mode: '%env(default:default_buckets_mode:BUCKET_MODE)%'
default_buckets_mode: 'path'

services:
# default configuration for services in *this* file
Expand Down Expand Up @@ -49,7 +53,7 @@ services:
App\EventListener\CurrentBinEventSubscriber:
arguments:
- '@bin_manager'
- '%bucket_mode%'
- '%buckets_mode%'
tags:
- { name: kernel.event_subscriber }

Expand Down
4 changes: 2 additions & 2 deletions deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
task('npm:build', static function (): void {
runLocally('npm run build');
upload('public/build/', '{{release_path}}/public/build/');
run('cd {{release_or_current_path}} && {{bin/console}} cache:clear {{console_options}}');
});

task('database:fixture', static function (): void {
runLocally('npm run build');
run('cd {{release_or_current_path}} && {{bin/console}} doctrine:fixtures:load --purge-with-truncate {{console_options}}');
run('cd {{release_or_current_path}} && {{bin/console}} doctrine:fixtures:load {{console_options}}');
});

before('deploy:symlink', 'npm:build');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"release": "release-it"
},
"lint-staged": {
"*.php": [
"{src,tests}/**/*.php": [
"vendor/bin/php-cs-fixer fix --diff --config .php-cs-fixer.dist.php --no-ansi",
"vendor/bin/phpstan analyse --no-ansi --no-progress"
],
Expand Down
12 changes: 6 additions & 6 deletions src/Manager/BinManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

class BinManager
{
public const LIMIT = 50;
public const EXPIRATION = 48;
private ?Bin $currentBin = null;
private BinConnectionWrapper $binConnectionWrapper;

Expand Down Expand Up @@ -49,7 +47,7 @@ public function getBinUrls(): array
$host = $request->getHttpHost();
$subdomain = sprintf('%s://%s.%s', $scheme, (string) $this->getCurrentBin(), $host);
$path = sprintf('%s://%s/b/%s', $scheme, $host, (string) $this->getCurrentBin());
$bucketMode = $this->parameterBag->get('bucket_mode');
$bucketMode = $this->parameterBag->get('buckets_mode');

if (filter_var($request->getHost(), FILTER_VALIDATE_IP) && 'both' === $bucketMode) {
$bucketMode = 'path';
Expand All @@ -64,7 +62,7 @@ public function getBinUrls(): array

public function getMax(): int
{
return self::LIMIT;
return (int) $this->parameterBag->get('buckets_request_limit');
}

public function getCurrentBin(): ?Bin
Expand Down Expand Up @@ -125,19 +123,21 @@ public function getRequests(): ArrayCollection
public function isExpired(): bool
{
$qb = $this->binConnectionWrapper->createQueryBuilder();
$expiration = (int) $this->parameterBag->get('buckets_expire_after');

$hasExpiredRequest = (bool) $qb->select('id')
->from('requests')
->orderBy('date', 'ASC')
->where('date <= :date')
->setParameter('date', new DateTime(sprintf('-%s hour', self::EXPIRATION)), Types::DATETIME_MUTABLE)
->setParameter('date', new DateTime(sprintf('-%s hour', $expiration)), Types::DATETIME_MUTABLE)
->setMaxResults(1)
->fetchOne();

$hasExpiredLock = false;
$binFile = sprintf('%s/%s.db', $this->parameterBag->get('buckets_dirs'), $this->currentBin->getId());
if (file_exists($binFile.'.lock')) {
$fileStat = stat($binFile.'.lock');
$hasExpiredLock = (new DateTime('@'.$fileStat['mtime'])) <= (new DateTime(sprintf('-%s hour', self::EXPIRATION)));
$hasExpiredLock = (new DateTime('@'.$fileStat['mtime'])) <= (new DateTime(sprintf('-%s hour', $expiration)));
}

return $hasExpiredRequest || $hasExpiredLock;
Expand Down

0 comments on commit 746ab79

Please sign in to comment.