Skip to content

Commit

Permalink
fix: Use deleteHooks() method when shutting down (#32)
Browse files Browse the repository at this point in the history
* fix: Use deleteHooks() method when shutting down

* lint project

* update dependencies

* Update .codecov.yml

* Change configure to parseServerSwiftConfigure

* update delete hooks

* Update .codecov.yml
  • Loading branch information
cbaker6 committed May 28, 2023
1 parent 9ee15af commit 7276ce7
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 85 deletions.
2 changes: 1 addition & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ coverage:
status:
patch:
default:
target: auto
target: 46
changes: false
project:
default:
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ jobs:
- uses: actions/checkout@v3
- name: Use multiple cores
run: defaults write com.apple.dt.XCBuild EnableSwiftBuildSystemIntegration 1
- name: Lint
run: set -o pipefail && env NSUnbufferedIO=YES swiftlint --strict
- name: Build and Test
run: swift test --enable-code-coverage
run: set -o pipefail && env NSUnbufferedIO=YES swift test --enable-code-coverage | xcpretty -c
env:
DEVELOPER_DIR: ${{ env.CI_XCODE }}
- name: Prepare codecov
Expand Down Expand Up @@ -51,7 +53,7 @@ jobs:
release-version: "5.7"
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Build and Test
run: swift test --enable-test-discovery --enable-code-coverage
run: set -o pipefail && env NSUnbufferedIO=YES swift test --enable-test-discovery --enable-code-coverage
- name: Update codecov config
run: cat .codecov.yml | curl --data-binary @- https://codecov.io/validate
- name: Prepare codecov
Expand Down
8 changes: 8 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
disabled_rules:
- file_length
- identifier_name
- blanket_disable_command
excluded: # paths to ignore during linting. Takes precedence over `included`.
- DerivedData
- .build
- .dependencies
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/netreconlab/Parse-Swift.git",
"state" : {
"revision" : "0902da8b34cd11d6ba69e04d208d5a8505f0be9d",
"version" : "5.6.0"
"revision" : "8ae529657e32d4b65c8c429dc5296ab865c4721d",
"version" : "5.7.0"
}
},
{
Expand Down
6 changes: 4 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// swift-tools-version:5.6
import PackageDescription

// swiftlint:disable line_length

let package = Package(
name: "ParseServerSwift",
platforms: [
Expand All @@ -16,7 +18,7 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", .upToNextMajor(from: "4.76.2")),
.package(url: "https://github.com/netreconlab/Parse-Swift.git",
.upToNextMajor(from: "5.6.0")),
.upToNextMajor(from: "5.7.0"))
],
targets: [
.target(
Expand All @@ -35,7 +37,7 @@ let package = Package(
]),
.testTarget(name: "ParseServerSwiftTests", dependencies: [
.target(name: "ParseServerSwift"),
.product(name: "XCTVapor", package: "vapor"),
.product(name: "XCTVapor", package: "vapor")
])
]
)
18 changes: 13 additions & 5 deletions Sources/App/entrypoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import ParseServerSwift
/// This extension is temporary and can be removed once Vapor gets this support.
private extension Vapor.Application {
static let baseExecutionQueue = DispatchQueue(label: "vapor.codes.entrypoint")

func runFromAsyncMainEntrypoint() async throws {
try await withCheckedThrowingContinuation { continuation in
Vapor.Application.baseExecutionQueue.async { [self] in
Expand All @@ -33,11 +33,19 @@ enum Entrypoint {
static func main() async throws {
var env = try Environment.detect()
try LoggingSystem.bootstrap(from: &env)

let app = Application(env)
defer { app.shutdown() }

try await configure(app)

defer {
Task {
// This may not delete all because it's async
// Be sure to delete manually in dashboard
await deleteHooks(app)
}
app.shutdown()
}

try await parseServerSwiftConfigure(app)
try await app.runFromAsyncMainEntrypoint()
}
}
2 changes: 2 additions & 0 deletions Sources/ParseServerSwift/Extensions/Parse+Vapor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public extension ParseHookRequestable {
In a single Parse Server environment, use options().
*/
func options(_ request: Request,
// swiftlint:disable:next line_length
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) throws -> API.Options {
var options = self.options()
options.insert(.serverURL(try serverURLString(request.url,
Expand All @@ -43,6 +44,7 @@ public extension ParseHookRequestable {
*/
func hydrateUser(options: API.Options = [],
request: Request,
// swiftlint:disable:next line_length
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> Self {
var updatedOptions = try self.options(request, parseServerURLStrings: parseServerURLStrings)
updatedOptions = updatedOptions.union(options)
Expand Down
42 changes: 25 additions & 17 deletions Sources/ParseServerSwift/Models/HookFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,17 @@ extension HookFunction {
.delete(options: [.serverURL(parseServerURLString)])
default:
throw ParseError(code: .otherCause,
// swiftlint:disable:next line_length
message: "Method \(method) is not supported for Hook Function: \"\(String(describing: hookFunction))\"")
}
// swiftlint:disable:next line_length
configuration.logger.notice("Successful \(method); Hook Function: \"\(String(describing: hookFunction))\" on server: \(parseServerURLString)")
} catch {
if error.containedIn([.webhookError]) && method == .POST {
// swiftlint:disable:next line_length
configuration.logger.warning("Hook Function: \"\(String(describing: hookFunction))\"; warning: \(error); on server: \(parseServerURLString)")
} else {
// swiftlint:disable:next line_length
configuration.logger.error("Could not \(method) Hook Function: \"\(String(describing: hookFunction))\"; error: \(error); on server: \(parseServerURLString)")
}
}
Expand All @@ -68,7 +72,7 @@ extension HookFunction {

// MARK: HookFunction - Fetch
public extension HookFunction {

/**
Fetches a Parse Cloud Code hook function.
- parameter path: A variadic list of paths.
Expand All @@ -80,8 +84,8 @@ public extension HookFunction {
Will log an error for each `parseServerURLString` that returns an error.
*/
static func fetch(_ path: PathComponent...,
name: String,
parseServerURLStrings: [String]) async throws -> [String: HookFunction] {
name: String,
parseServerURLStrings: [String]) async throws -> [String: HookFunction] {
try await fetch(path, name: name, parseServerURLStrings: parseServerURLStrings)
}

Expand All @@ -96,11 +100,11 @@ public extension HookFunction {
Will log an error for each `parseServerURLString` that returns an error.
*/
static func fetch(_ path: [PathComponent],
name: String,
parseServerURLStrings: [String]) async throws -> [String: HookFunction] {
name: String,
parseServerURLStrings: [String]) async throws -> [String: HookFunction] {
try await method(.PUT, path, name: name, parseServerURLStrings: parseServerURLStrings)
}

/**
Fetches all Parse Cloud Code hook function.
- parameter path: A variadic list of paths.
Expand Down Expand Up @@ -140,6 +144,7 @@ public extension HookFunction {
hookFunctions[parseServerURLString] = try await hookFunction
.fetchAll(options: [.serverURL(parseServerURLString)])
} catch {
// swiftlint:disable:next line_length
configuration.logger.error("Could not fetchAll function: \"\(String(describing: hookFunction))\"; error: \(error); on server: \(parseServerURLString)")
}
}
Expand All @@ -149,7 +154,7 @@ public extension HookFunction {

// MARK: HookFunction - Create
public extension HookFunction {

/**
Creates a Parse Cloud Code hook function.
- parameter path: A variadic list of paths.
Expand All @@ -163,6 +168,7 @@ public extension HookFunction {
*/
static func create(_ path: PathComponent...,
name: String,
// swiftlint:disable:next line_length
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: HookFunction] {
try await create(path, name: name, parseServerURLStrings: parseServerURLStrings)
}
Expand All @@ -180,14 +186,15 @@ public extension HookFunction {
*/
static func create(_ path: [PathComponent],
name: String,
// swiftlint:disable:next line_length
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: HookFunction] {
try await method(.POST, path, name: name, parseServerURLStrings: parseServerURLStrings)
}
}

// MARK: HookFunction - Update
public extension HookFunction {

/**
Updates a Parse Cloud Code hook function.
- parameter path: A variadic list of paths.
Expand All @@ -201,6 +208,7 @@ public extension HookFunction {
*/
static func update(_ path: PathComponent...,
name: String,
// swiftlint:disable:next line_length
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: HookFunction] {
try await update(path, name: name, parseServerURLStrings: parseServerURLStrings)
}
Expand All @@ -218,14 +226,15 @@ public extension HookFunction {
*/
static func update(_ path: [PathComponent],
name: String,
// swiftlint:disable:next line_length
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: HookFunction] {
try await method(.PUT, path, name: name, parseServerURLStrings: parseServerURLStrings)
}
}

// MARK: HookFunction - Delete
public extension HookFunction {

/**
Removes a Parse Cloud Code hook function.
- parameter path: A variadic list of paths.
Expand All @@ -238,6 +247,7 @@ public extension HookFunction {
*/
static func delete(_ path: PathComponent...,
name: String,
// swiftlint:disable:next line_length
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws {
try await delete(path, name: name, parseServerURLStrings: parseServerURLStrings)
}
Expand All @@ -254,6 +264,7 @@ public extension HookFunction {
*/
static func delete(_ path: [PathComponent],
name: String,
// swiftlint:disable:next line_length
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws {
try await method(.DELETE, path, name: name, parseServerURLStrings: parseServerURLStrings)
}
Expand All @@ -276,8 +287,7 @@ public extension RoutesBuilder {
name: String,
use closure: @escaping (Request) async throws -> Response
) -> Route
where Response: AsyncResponseEncodable
{
where Response: AsyncResponseEncodable {
self.on(path,
name: name,
use: closure)
Expand All @@ -298,8 +308,7 @@ public extension RoutesBuilder {
name: String,
use closure: @escaping (Request) async throws -> Response
) -> Route
where Response: AsyncResponseEncodable
{
where Response: AsyncResponseEncodable {
self.on(path,
name: name,
use: closure)
Expand All @@ -322,8 +331,7 @@ public extension RoutesBuilder {
name: String,
use closure: @escaping (Request) async throws -> Response
) -> Route
where Response: AsyncResponseEncodable
{
where Response: AsyncResponseEncodable {
self.on(path,
body: body,
name: name,
Expand All @@ -347,13 +355,13 @@ public extension RoutesBuilder {
name: String,
use closure: @escaping (Request) async throws -> Response
) -> Route
where Response: AsyncResponseEncodable
{
where Response: AsyncResponseEncodable {
Task {
do {
await configuration.hooks.updateFunctions(try await HookFunction.create(path,
name: name))
} catch {
// swiftlint:disable:next line_length
configuration.logger.error("Could not create HookFunction route for path: \(path); name: \(name) on servers: \(configuration.parseServerURLStrings) because of error: \(error)")
}
}
Expand Down
Loading

0 comments on commit 7276ce7

Please sign in to comment.