From a57ac9f690fd3206b55234e3a796c494975650d5 Mon Sep 17 00:00:00 2001 From: saroar Date: Sat, 13 Jan 2024 15:30:28 +0000 Subject: [PATCH] refactor - fixed send notification issue - Docker file & Docker Compose file update --- Dockerfile | 26 +++++++++++-------- Sources/App/Jobs/EmailJobMongoQueue.swift | 2 +- .../AuthenticationHandler.swift | 1 - Sources/App/Webbsockets/ChatClient.swift | 11 +++++--- docker-compose.yml | 18 +++---------- 5 files changed, 27 insertions(+), 31 deletions(-) diff --git a/Dockerfile b/Dockerfile index 01b73c8..de89d49 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,8 +6,8 @@ FROM swift:5.9-jammy as build # Install OS updates RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \ && apt-get -q update \ - && apt-get -q dist-upgrade -y\ - && rm -rf /var/lib/apt/lists/* + && apt-get -q dist-upgrade -y \ + && apt-get install -y libjemalloc-dev # Set up a build area WORKDIR /build @@ -23,11 +23,11 @@ RUN swift package resolve --skip-update \ # Copy entire repo into container COPY . . -# Build everything, with optimizations -RUN swift build -c release --static-swift-stdlib \ - # Workaround for https://github.com/apple/swift/pull/68669 - # This can be removed as soon as 5.9.1 is released, but is harmless if left in. - -Xlinker -u -Xlinker _swift_backtrace_isThunkFunction +# Build everything, with optimizations, with static linking, and using jemalloc +# N.B.: The static version of jemalloc is incompatible with the static Swift runtime. +RUN swift build -c release \ + --static-swift-stdlib \ + -Xlinker -ljemalloc # Switch to the staging area WORKDIR /staging @@ -35,6 +35,9 @@ WORKDIR /staging # Copy main executable to staging area RUN cp "$(swift build --package-path /build -c release --show-bin-path)/App" ./ +# Copy static swift backtracer binary to staging area +RUN cp "/usr/libexec/swift/linux/swift-backtrace-static" ./ + # Copy resources bundled by SPM to staging area RUN find -L "$(swift build --package-path /build -c release --show-bin-path)/" -regex '.*\.resources$' -exec cp -Ra {} ./ \; @@ -46,17 +49,18 @@ RUN [ -d /build/Resources ] && { mv /build/Resources ./Resources && chmod -R a-w # ================================ # Run image # ================================ -FROM swift:5.9-jammy-slim +FROM ubuntu:jammy # Make sure all system packages are up to date, and install only essential packages. RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \ && apt-get -q update \ && apt-get -q dist-upgrade -y \ && apt-get -q install -y \ - libcurl4 \ + libjemalloc2 \ ca-certificates \ tzdata \ - + libcurl4 \ +# If your app or its dependencies import FoundationNetworking, also install `libcurl4`. # If your app or its dependencies import FoundationXML, also install `libxml2`. # libxml2 \ && rm -r /var/lib/apt/lists/* @@ -72,7 +76,7 @@ COPY --from=build --chown=vapor:vapor /staging /app COPY --from=build --chown=vapor:vapor /build/.build/release /app # Provide configuration needed by the built-in crash reporter and some sensible default behaviors. -ENV SWIFT_ROOT=/usr SWIFT_BACKTRACE=enable=yes,sanitize=yes,threads=all,images=all,interactive=no +ENV SWIFT_BACKTRACE=enable=yes,sanitize=yes,threads=all,images=all,interactive=no,swift-backtrace=./swift-backtrace-static # Ensure all further commands run as the vapor user USER vapor:vapor diff --git a/Sources/App/Jobs/EmailJobMongoQueue.swift b/Sources/App/Jobs/EmailJobMongoQueue.swift index 0fd557d..98fdf0d 100644 --- a/Sources/App/Jobs/EmailJobMongoQueue.swift +++ b/Sources/App/Jobs/EmailJobMongoQueue.swift @@ -16,7 +16,7 @@ struct EmailJobMongoQueue: RecurringTask { typealias ExecutionContext = Application // Ensures only one user engagement prompt exists per user - var uniqueTaskKey: String { vCodeAttempt.id!.hexString } + var uniqueTaskKey: String { vCodeAttempt.id!.hexString } ///: Fixed OPS so stupid please fixed me // The amount of time we expect this task to take if it's very slow // This is optional, and has a sensible default. Note; it will not be the task's actual deadline. diff --git a/Sources/App/RouteHandlers/AuthEngineHandlers/AuthenticationHandler.swift b/Sources/App/RouteHandlers/AuthEngineHandlers/AuthenticationHandler.swift index 9140ef4..3946cc5 100644 --- a/Sources/App/RouteHandlers/AuthEngineHandlers/AuthenticationHandler.swift +++ b/Sources/App/RouteHandlers/AuthEngineHandlers/AuthenticationHandler.swift @@ -48,7 +48,6 @@ public func authenticationHandler( throw Abort(.internalServerError, reason: "Failed to retrieve attempt ID after saving.") } - let emailPayload = EmailPayload(otpEmail, to: email) let task = EmailJobMongoQueue(payload: emailPayload, vCodeAttempt: smsAttempt) diff --git a/Sources/App/Webbsockets/ChatClient.swift b/Sources/App/Webbsockets/ChatClient.swift index c10590a..670d0b2 100644 --- a/Sources/App/Webbsockets/ChatClient.swift +++ b/Sources/App/Webbsockets/ChatClient.swift @@ -54,11 +54,14 @@ actor WebsocketClients { } catch { messageCreate.isDelivered = false + print("Send Msg to User: \(senderID) error: \(error)") + throw Abort(.notFound, reason: "Send Msg to User: \(senderID) error: \(error)") } } - @Sendable func send(message: MessageModel, req: Request, socket: WebSocket) async throws { + @Sendable + func send(message: MessageModel, req: Request, socket: WebSocket) async throws { if !req.loggedIn { logger.error("\(#line) Unauthorized send message") throw Abort(.unauthorized) @@ -80,7 +83,8 @@ actor WebsocketClients { .get() } - @Sendable private func sendNotificationToConversationMembers( + @Sendable + private func sendNotificationToConversationMembers( msgItem: MessageItem, senderID: ObjectId, with req: Request @@ -108,7 +112,8 @@ actor WebsocketClients { .get() else { - throw Abort(.notFound, reason: "User not found from \(#function)") + continue +// throw Abort(.notFound, reason: "User not found from \(#function)") } try await req.apns.send( diff --git a/docker-compose.yml b/docker-compose.yml index 14bb7ac..ae2b8b4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,9 +17,9 @@ x-shared_environment: &shared_environment REDIS_URL_PRODUCTION: '${REDIS_URL_PRODUCTION}' services: - mongo: + addame_mongo: image: mongo:latest - container_name: AddameMongoPro + container_name: addameMongoPro environment: - AUTH=yes - MONGODB_ADMIN_USER='${MONGODB_ADMIN_USER}' @@ -52,25 +52,13 @@ services: - ./.env:/app/.env - ./.env.production:/app/.env.production depends_on: - - mongo + - addame_mongo networks: - addame_pro_gateway restart: unless-stopped env_file: - ./.env - redis: - container_name: redisProAddame - image: 'bitnami/redis:5.0' - environment: - - ALLOW_EMPTY_PASSWORD=yes - - REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL - restart: unless-stopped - volumes: - - redis-persistence-production:/bitnami/redis/data - networks: - - addame_pro_gateway - volumes: addameServer: redis-persistence-production: