Refine NSE completion.

This commit is contained in:
Matthew Chen 2021-09-24 15:25:58 -03:00
parent fc45ec8f63
commit f511ca35df
5 changed files with 29 additions and 15 deletions

View File

@ -58,14 +58,7 @@ class NotificationService: UNNotificationServiceExtension {
}
Logger.flush()
let isSync = timeHasExpired
if isSync {
contentHandler(content)
} else {
DispatchQueue.global().asyncAfter(deadline: .now() + .milliseconds(1000)) {
contentHandler(content)
}
}
contentHandler(content)
}
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
@ -150,11 +143,17 @@ class NotificationService: UNNotificationServiceExtension {
}.catch { _ in
// Do nothing, Promise.timeout() will log timeouts.
}
fetchPromise.then { [weak self] () -> Promise<Void> in
fetchPromise.then(on: .global()) { [weak self] () -> Promise<Void> in
Logger.info("Waiting for processing to complete.")
guard let self = self else { return Promise.value(()) }
let processingCompletePromise = self.messageProcessor.processingCompletePromise()
processingCompletePromise.timeout(seconds: 20, description: "Message Processing Timeout.") {
processingCompletePromise.then(on: .global()) {
// Wait until all ACKs are enqueued.
Self.messageFetcherJob.pendingAcksPromise()
}.then(on: .global()) {
// Wait until all outgoing messages are sent.
Self.messageSender.pendingSendsPromise()
}.timeout(seconds: 20, description: "Message Processing Timeout.") {
NotificationServiceError.timeout
}.catch { _ in
// Do nothing, Promise.timeout() will log timeouts.

View File

@ -273,6 +273,12 @@ public class MessageFetcherJob: NSObject {
ackOperationQueue.addOperation(ackOperation)
}
public func pendingAcksPromise() -> Promise<Void> {
firstly(on: .global()) {
self.ackOperationQueue.waitUntilAllOperationsAreFinished()
}
}
// MARK: -
typealias EnvelopeJob = MessageProcessor.EnvelopeJob

View File

@ -654,15 +654,15 @@ private struct EncryptedEnvelope: PendingEnvelope, Dependencies {
guard let other = other as? EncryptedEnvelope else {
return false
}
// serverDeliveryTimestamp is a cheaper comparison and is likely
// to eliminate most candidates.
guard self.serverDeliveryTimestamp == other.serverDeliveryTimestamp else {
guard let serverGuid = encryptedEnvelope.serverGuid else {
owsFailDebug("Missing serverGuid.")
return false
}
guard self.encryptedEnvelopeData == other.encryptedEnvelopeData else {
guard let otherServerGuid = other.encryptedEnvelope.serverGuid else {
owsFailDebug("Missing other.serverGuid.")
return false
}
return true
return serverGuid == otherServerGuid
}
}

View File

@ -101,6 +101,9 @@ NS_SWIFT_NAME(OutgoingAttachmentInfo)
// TODO: Make this private.
- (void)sendMessageToRecipient:(OWSMessageSend *)messageSend;
// TODO: Make this private.
+ (NSOperationQueue *)globalSendingQueue;
@end
#pragma mark -

View File

@ -152,6 +152,12 @@ extension MessageSender {
}
return promises
}
public func pendingSendsPromise() -> Promise<Void> {
firstly(on: .global()) {
Self.globalSendingQueue().waitUntilAllOperationsAreFinished()
}
}
}
// MARK: -