Skip to content

Commit

Permalink
Merge pull request #106 from canyongbs/feature/AIDAPP-98
Browse files Browse the repository at this point in the history
[AIDAPP-98]: Improve our Queue Strategy
  • Loading branch information
Orrison authored Jun 4, 2024
2 parents 770846f + 77e2025 commit b3b1d44
Show file tree
Hide file tree
Showing 18 changed files with 143 additions and 20 deletions.
9 changes: 8 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ AWS_S3_URL=http://localhost:9000/local
AWS_SQS_ACCESS_KEY_ID=
AWS_SQS_SECRET_ACCESS_KEY=
SQS_PREFIX=
SQS_QUEUE=
SQS_QUEUE=default
LANDLORD_SQS_QUEUE=landlord
OUTBOUND_COMMUNICATION_QUEUE=outbound-communication
AUDIT_QUEUE_QUEUE=audit
IMPORT_EXPORT_QUEUE=import-export
SQS_SUFFIX=
AWS_SQS_DEFAULT_REGION=us-east-1

Expand Down Expand Up @@ -116,6 +119,10 @@ DEV_SUPER_ADMIN_EMAIL=
DEV_USER_EMAILS=
DEV_CONTACT_EMAILS=

# Will specify the amount of queue worker processes to keep running when the container starts
# Requires image rebuild if changed, will default to 3 if not set
#TOTAL_QUEUE_WORKERS=3

### END DEV SETTINGS ###

# TODO: Determine from Product whether or not this would be per tenant
Expand Down
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ RUN echo "source $NVM_DIR/nvm.sh \
COPY ./docker/s6-overlay/scripts/ /etc/s6-overlay/scripts/
COPY docker/s6-overlay/s6-rc.d/ /etc/s6-overlay/s6-rc.d/
COPY ./docker/s6-overlay/user/ /etc/s6-overlay/s6-rc.d/user/contents.d/
COPY ./docker/s6-overlay/templates/ /tmp/s6-overlay-templates

ARG TOTAL_QUEUE_WORKERS=3

COPY ./docker/generate-queues.sh /generate-queues.sh
RUN chmod +x /generate-queues.sh
RUN /generate-queues.sh
RUN rm /generate-queues.sh

COPY ./docker/nginx/nginx.conf /etc/nginx/nginx.conf
COPY ./docker/nginx/site-opts.d /etc/nginx/site-opts.d
Expand Down
2 changes: 1 addition & 1 deletion app-modules/audit/config/audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@
'queue' => [
'enable' => true,
'connection' => env('AUDIT_QUEUE_CONNECTION', 'sync'),
'queue' => env('SQS_QUEUE', 'default'),
'queue' => env('AUDIT_QUEUE_QUEUE', env('SQS_QUEUE', 'default')),
'delay' => 0,
],

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ public function via(object $notifiable): array
->toArray();
}

public function viaQueues(): array
{
return [
DatabaseChannel::class => config('queue.outbound_communication_queue'),
EmailChannel::class => config('queue.outbound_communication_queue'),
SmsChannel::class => config('queue.outbound_communication_queue'),
];
}

public function beforeSend(object $notifiable, string $channel): OutboundDeliverable|false
{
$deliverable = resolve(MakeOutboundDeliverable::class)->handle($this, $notifiable, $channel);
Expand Down
1 change: 0 additions & 1 deletion app-modules/timeline/src/Listeners/AddRecordToTimeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class AddRecordToTimeline
{
public function handle(TimelineableRecordCreated $event): void
{
/** @var Model $entity */
$entity = $event->entity;

cache()->forget("timeline.synced.{$entity->getMorphClass()}.{$entity->getKey()}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@
namespace AidingApp\Timeline\Listeners;

use AidingApp\Timeline\Models\Timeline;
use Illuminate\Database\Eloquent\Model;
use AidingApp\Timeline\Events\TimelineableRecordDeleted;

class RemoveRecordFromTimeline
{
public function handle(TimelineableRecordDeleted $event): void
{
/** @var Model $entity */
$entity = $event->entity;

cache()->forget("timeline.synced.{$entity->getMorphClass()}.{$entity->getKey()}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,22 @@
</COPYRIGHT>
*/

namespace App\Jobs\Middleware;
namespace App\Overrides\Filament\Actions\Imports\Jobs;

class SkipIfNotLocal
use Carbon\CarbonInterface;
use Filament\Actions\Imports\Jobs\ImportCsv;

class ImportCsvOverride extends ImportCsv
{
public function handle($job, $next): void
public int $tries = 2;

public function retryUntil(): ?CarbonInterface
{
if (! app()->environment('local', 'testing')) {
return;
}
return null;
}

$next($job);
public function getJobQueue(): ?string
{
return config('queue.import_export_queue');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
<COPYRIGHT>
Copyright © 2016-2024, Canyon GBS LLC. All rights reserved.
Aiding App™ is licensed under the Elastic License 2.0. For more details,
see <https://github.com/canyongbs/aidingapp/blob/main/LICENSE.>
Notice:
- You may not provide the software to third parties as a hosted or managed
service, where the service provides users with access to any substantial set of
the features or functionality of the software.
- You may not move, change, disable, or circumvent the license key functionality
in the software, and you may not remove or obscure any functionality in the
software that is protected by the license key.
- You may not alter, remove, or obscure any licensing, copyright, or other notices
of the licensor in the software. Any use of the licensor’s trademarks is subject
to applicable law.
- Canyon GBS LLC respects the intellectual property rights of others and expects the
same in return. Canyon GBS™ and Aiding App™ are registered trademarks of
Canyon GBS LLC, and we are committed to enforcing and protecting our trademarks
vigorously.
- The software solution, including services, infrastructure, and code, is offered as a
Software as a Service (SaaS) by Canyon GBS LLC.
- Use of this software implies agreement to the license terms and conditions as stated
in the Elastic License 2.0.
For more information or inquiries please visit our website at
<https://www.canyongbs.com> or contact us via email at legal@canyongbs.com.
</COPYRIGHT>
*/

namespace App\Overrides\Filament\Actions\Imports\Jobs;

use Carbon\CarbonInterface;
use Filament\Actions\Exports\Jobs\PrepareCsvExport;

class PrepareCsvExportOverride extends PrepareCsvExport
{
public int $tries = 2;

public function retryUntil(): ?CarbonInterface
{
return null;
}

public function getJobQueue(): ?string
{
return config('queue.import_export_queue');
}
}
6 changes: 6 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@
use Laravel\Pennant\Feature;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;
use Filament\Actions\Imports\Jobs\ImportCsv;
use Filament\Actions\Exports\Jobs\PrepareCsvExport;
use Illuminate\Database\Eloquent\Relations\Relation;
use OpenSearch\Migrations\Filesystem\MigrationStorage;
use App\Overrides\Filament\Actions\Imports\Jobs\ImportCsvOverride;
use App\Overrides\Filament\Actions\Imports\Jobs\PrepareCsvExportOverride;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Types\Condition as GraphQLSearchByTypesCondition;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Definitions\SearchByDirective as GraphQLSearchByDirectiveAlias;
use App\Overrides\LastDragon_ru\LaraASP\GraphQL\SearchBy\Types\Condition as GraphQLSearchByTypesConditionOverride;
Expand All @@ -59,6 +63,8 @@ public function register(): void

$this->app->bind(GraphQLSearchByTypesCondition::class, GraphQLSearchByTypesConditionOverride::class);
$this->app->bind(GraphQLSearchByDirectiveAlias::class, GraphQLSearchByDirectiveOverride::class);
$this->app->bind(ImportCsv::class, ImportCsvOverride::class);
$this->app->bind(PrepareCsvExport::class, PrepareCsvExportOverride::class);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions config/queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,8 @@
],

'landlord_queue' => env('LANDLORD_SQS_QUEUE', 'landlord'),

'outbound_communication_queue' => env('OUTBOUND_COMMUNICATION_QUEUE', env('SQS_QUEUE', 'default')),

'import_export_queue' => env('IMPORT_EXPORT_QUEUE', env('SQS_QUEUE', 'default')),
];
7 changes: 7 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ services:
aidingapp.local:
build:
target: development
args:
TOTAL_QUEUE_WORKERS: '${TOTAL_QUEUE_WORKERS:-3}'
environment:
SSL_MODE: "mixed"
LANDLORD_MIGRATE: '${LANDLORD_MIGRATE:-true}'
Expand All @@ -18,6 +20,11 @@ services:
CACHE_FILAMENT_COMPONENTS: '${CACHE_FILAMENT_COMPONENTS:-false}'
PUID: '${SPIN_USER_ID:-9999}'
PGID: '${SPIN_GROUP_ID:-9999}'
SQS_QUEUE: '${SQS_QUEUE:-default}'
LANDLORD_SQS_QUEUE: '${LANDLORD_SQS_QUEUE:-landlord}'
OUTBOUND_COMMUNICATION_QUEUE: '${OUTBOUND_COMMUNICATION_QUEUE:-outbound-communication}'
AUDIT_QUEUE_QUEUE: '${AUDIT_QUEUE_QUEUE:-audit}'
IMPORT_EXPORT_QUEUE: '${IMPORT_EXPORT_QUEUE:-import-export}'
labels:
- "traefik.enable=true"
- "traefik.http.routers.aidingapp-app.rule=HostRegexp(`aidingapp.local`, `{subdomain:[a-z0-9]+}.aidingapp.local`)"
Expand Down
2 changes: 1 addition & 1 deletion docker/devops
25 changes: 25 additions & 0 deletions docker/generate-queues.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh

generate_services () {
QUEUE_NAME=$1
QUEUE_ENV_VAR=$2

for run in $(seq "$TOTAL_QUEUE_WORKERS"); do
if [ $run -eq 1 ]; then
SLEEP_TIME=0
else
SLEEP_TIME=3;
fi

cp -r "/tmp/s6-overlay-templates/laravel-queue/service" "/etc/s6-overlay/s6-rc.d/$QUEUE_NAME-queue-$run";
sed -i -e "s/VAR_QUEUE/$QUEUE_ENV_VAR/g" "/etc/s6-overlay/s6-rc.d/$QUEUE_NAME-queue-$run/run";
sed -i -e "s/TEMPLATE_SLEEP/$SLEEP_TIME/g" "/etc/s6-overlay/s6-rc.d/$QUEUE_NAME-queue-$run/run";
cp "/tmp/s6-overlay-templates/laravel-queue/laravel-queue" "/etc/s6-overlay/s6-rc.d/user/contents.d/$QUEUE_NAME-queue-$run";
done
}

generate_services "default" "\$SQS_QUEUE"
generate_services "landlord" "\$LANDLORD_SQS_QUEUE"
generate_services "outbound-communication" "\$OUTBOUND_COMMUNICATION_QUEUE"
generate_services "audit" "\$AUDIT_QUEUE_QUEUE"
generate_services "import-export" "\$IMPORT_EXPORT_QUEUE"
3 changes: 0 additions & 3 deletions docker/s6-overlay/s6-rc.d/laravel-queue/run

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
# Exit on error
set -e

SLEEP_TIME=TEMPLATE_SLEEP

# Check to see if an Artisan file exists and assume it means Laravel is configured.
if [ -f "$WEBUSER_HOME"/artisan ] && [ "${LARAVEL_QUEUE_ENABLED:="true"}" == "true" ]; then
echo "Starting Laravel Queue Worker..."
if [ -f "$WEBUSER_HOME/artisan" ] && [ "${LARAVEL_QUEUE_ENABLED:="true"}" == "true" ]; then
echo "Starting Laravel Queue Worker for queue VAR_QUEUE..."

cd "$WEBUSER_HOME"

USERNAME=$(id -nu "$PUID")

s6-setuidgid "$USERNAME" php "$WEBUSER_HOME"/artisan queue:work
s6-setuidgid "$USERNAME" php "$WEBUSER_HOME/artisan" queue:work --sleep=$SLEEP_TIME --queue=VAR_QUEUE
else
echo "👉 Skipping Laravel Queue Worker because we could not detect a Laravel install or it was specifically disabled..."

tail -f /dev/null
fi
fi

0 comments on commit b3b1d44

Please sign in to comment.