From 386ddc243605e759e8de90fcf27da653fc34d844 Mon Sep 17 00:00:00 2001 From: Imran Momin Date: Sat, 26 Oct 2019 11:16:19 -0400 Subject: [PATCH 1/4] implemented newly added abstract method --- .../DocumentDbConnection.cs | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/Hangfire.AzureDocumentDB/DocumentDbConnection.cs b/Hangfire.AzureDocumentDB/DocumentDbConnection.cs index f21c972..77cde13 100644 --- a/Hangfire.AzureDocumentDB/DocumentDbConnection.cs +++ b/Hangfire.AzureDocumentDB/DocumentDbConnection.cs @@ -279,26 +279,22 @@ public override HashSet GetAllItemsFromSet(string key) } public override string GetFirstByLowestScoreFromSet(string key, double fromScore, double toScore) + { + return GetFirstByLowestScoreFromSet(key, fromScore, toScore, 1).FirstOrDefault(); + } + + public override List GetFirstByLowestScoreFromSet(string key, double fromScore, double toScore, int count) { if (key == null) throw new ArgumentNullException(nameof(key)); if (toScore < fromScore) throw new ArgumentException("The `toScore` value must be higher or equal to the `fromScore` value."); - SqlQuerySpec sql = new SqlQuerySpec - { - QueryText = "SELECT TOP 1 VALUE doc['value'] FROM doc WHERE doc.type = @type AND doc.key = @key " + - "AND (doc.score BETWEEN @from AND @to) ORDER BY doc.score", - Parameters = new SqlParameterCollection - { - new SqlParameter("@key", key), - new SqlParameter("@type", (int)DocumentTypes.Set), - new SqlParameter("@from", (int)fromScore), - new SqlParameter("@to", (int)toScore) - } - }; - - return Storage.Client.CreateDocumentQuery(Storage.CollectionUri, sql) + return Storage.Client.CreateDocumentQuery(Storage.CollectionUri) + .Where(s => s.DocumentType == DocumentTypes.Set && s.Key == key && s.Score >= fromScore && s.Score <= toScore) + .OrderBy(s => s.Score) + .Take(count) + .Select(s => s.Value) .ToQueryResult() - .FirstOrDefault(); + .ToList(); } #endregion From aa99c3dfee04054b906bd2a5bb962371057dbce3 Mon Sep 17 00:00:00 2001 From: Imran Momin Date: Mon, 28 Oct 2019 09:02:45 -0400 Subject: [PATCH 2/4] convert the camelCase keys to PascalCase --- .../StoredProcedure/setJobState.js | 10 ++++++++++ .../StoredProcedure/setJobState.ts | 17 +++++++++++++++++ Hangfire.AzureDocumentDB/package-lock.json | 6 +++--- Hangfire.AzureDocumentDB/package.json | 2 +- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/Hangfire.AzureDocumentDB/StoredProcedure/setJobState.js b/Hangfire.AzureDocumentDB/StoredProcedure/setJobState.js index dfef5cd..84034e4 100644 --- a/Hangfire.AzureDocumentDB/StoredProcedure/setJobState.js +++ b/Hangfire.AzureDocumentDB/StoredProcedure/setJobState.js @@ -4,6 +4,12 @@ function setJobState(id, state) { let response = getContext().getResponse(); let collectionLink = collection.getAltLink(); let documentLink = `${collectionLink}/docs/${id}`; + const keys = Object.keys(state.data); + for (const key of keys) { + const newKey = camelCaseToPascalCase(key); + state.data[newKey] = state.data[key]; + delete state.data[key]; + } response.setBody(false); let isAccepted = collection.readDocument(documentLink, (error, job) => { if (error) { @@ -35,6 +41,10 @@ function setJobState(id, state) { throw new Error("The call was not accepted"); } } + function camelCaseToPascalCase(input) { + return input.replace(/([A-Z])/g, '$1') + .replace(/^./, (match) => match.toUpperCase()); + } if (!isAccepted) { throw new Error("The call was not accepted"); } diff --git a/Hangfire.AzureDocumentDB/StoredProcedure/setJobState.ts b/Hangfire.AzureDocumentDB/StoredProcedure/setJobState.ts index c1219bf..9b9f3bc 100644 --- a/Hangfire.AzureDocumentDB/StoredProcedure/setJobState.ts +++ b/Hangfire.AzureDocumentDB/StoredProcedure/setJobState.ts @@ -10,6 +10,14 @@ function setJobState(id: string, state: IState) { let collectionLink: string = collection.getAltLink(); let documentLink: string = `${collectionLink}/docs/${id}`; + // convert the case for the data + const keys: Array = Object.keys(state.data); + for (const key of keys) { + const newKey = camelCaseToPascalCase(key); + state.data[newKey] = state.data[key]; + delete state.data[key]; + } + // default response response.setBody(false); @@ -56,6 +64,15 @@ function setJobState(id: string, state: IState) { } } + /** + * Convert the camel case to pascal + * @param input - The text which needs to be converted + */ + function camelCaseToPascalCase(input: string): string { + return input.replace(/([A-Z])/g, '$1') + .replace(/^./, (match) => match.toUpperCase()); + } + if (!isAccepted) { throw new Error("The call was not accepted"); } diff --git a/Hangfire.AzureDocumentDB/package-lock.json b/Hangfire.AzureDocumentDB/package-lock.json index 7b68d31..b8b498b 100644 --- a/Hangfire.AzureDocumentDB/package-lock.json +++ b/Hangfire.AzureDocumentDB/package-lock.json @@ -11,9 +11,9 @@ "dev": true }, "typescript": { - "version": "3.1.1", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.1.1.tgz", - "integrity": "sha512-Veu0w4dTc/9wlWNf2jeRInNodKlcdLgemvPsrNpfu5Pq39sgfFjvIIgTsvUHCoLBnMhPoUA+tFxsXjU6VexVRQ==", + "version": "3.6.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.6.4.tgz", + "integrity": "sha512-unoCll1+l+YK4i4F8f22TaNVPRHcD9PA3yCuZ8g5e0qGqlVlJ/8FSateOLLSagn+Yg5+ZwuPkL8LFUc0Jcvksg==", "dev": true } } diff --git a/Hangfire.AzureDocumentDB/package.json b/Hangfire.AzureDocumentDB/package.json index 438556f..d44e9dc 100644 --- a/Hangfire.AzureDocumentDB/package.json +++ b/Hangfire.AzureDocumentDB/package.json @@ -4,6 +4,6 @@ "private": true, "devDependencies": { "@types/documentdb-server": "0.0.32", - "typescript": "3.1.1" + "typescript": "3.6.4" } } From c4749d7fe3fcd1e1c8019abd0bd33e3309f749b8 Mon Sep 17 00:00:00 2001 From: Imran Momin Date: Tue, 29 Oct 2019 13:12:48 -0400 Subject: [PATCH 3/4] fixes when the key and pascalkey are same --- Hangfire.AzureDocumentDB/DocumentDbConnection.cs | 1 + Hangfire.AzureDocumentDB/StoredProcedure/setJobState.js | 6 ++++-- Hangfire.AzureDocumentDB/StoredProcedure/setJobState.ts | 6 ++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Hangfire.AzureDocumentDB/DocumentDbConnection.cs b/Hangfire.AzureDocumentDB/DocumentDbConnection.cs index 77cde13..11accc7 100644 --- a/Hangfire.AzureDocumentDB/DocumentDbConnection.cs +++ b/Hangfire.AzureDocumentDB/DocumentDbConnection.cs @@ -286,6 +286,7 @@ public override string GetFirstByLowestScoreFromSet(string key, double fromScore public override List GetFirstByLowestScoreFromSet(string key, double fromScore, double toScore, int count) { if (key == null) throw new ArgumentNullException(nameof(key)); + if (count <= 0) throw new ArgumentException("The value must be a positive number", nameof(count)); if (toScore < fromScore) throw new ArgumentException("The `toScore` value must be higher or equal to the `fromScore` value."); return Storage.Client.CreateDocumentQuery(Storage.CollectionUri) diff --git a/Hangfire.AzureDocumentDB/StoredProcedure/setJobState.js b/Hangfire.AzureDocumentDB/StoredProcedure/setJobState.js index 84034e4..92f1c5b 100644 --- a/Hangfire.AzureDocumentDB/StoredProcedure/setJobState.js +++ b/Hangfire.AzureDocumentDB/StoredProcedure/setJobState.js @@ -7,8 +7,10 @@ function setJobState(id, state) { const keys = Object.keys(state.data); for (const key of keys) { const newKey = camelCaseToPascalCase(key); - state.data[newKey] = state.data[key]; - delete state.data[key]; + if (key !== newKey) { + state.data[newKey] = state.data[key]; + delete state.data[key]; + } } response.setBody(false); let isAccepted = collection.readDocument(documentLink, (error, job) => { diff --git a/Hangfire.AzureDocumentDB/StoredProcedure/setJobState.ts b/Hangfire.AzureDocumentDB/StoredProcedure/setJobState.ts index 9b9f3bc..9474759 100644 --- a/Hangfire.AzureDocumentDB/StoredProcedure/setJobState.ts +++ b/Hangfire.AzureDocumentDB/StoredProcedure/setJobState.ts @@ -14,8 +14,10 @@ function setJobState(id: string, state: IState) { const keys: Array = Object.keys(state.data); for (const key of keys) { const newKey = camelCaseToPascalCase(key); - state.data[newKey] = state.data[key]; - delete state.data[key]; + if (key !== newKey) { + state.data[newKey] = state.data[key]; + delete state.data[key]; + } } // default response From 94dce86478e0c316792bd57fbccd482e16ec50eb Mon Sep 17 00:00:00 2001 From: Imran Momin Date: Wed, 30 Oct 2019 06:37:57 -0400 Subject: [PATCH 4/4] fixed the queries that cannot be served by gateway --- Hangfire.AzureDocumentDB/Queue/JobQueueMonitoringApi.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Hangfire.AzureDocumentDB/Queue/JobQueueMonitoringApi.cs b/Hangfire.AzureDocumentDB/Queue/JobQueueMonitoringApi.cs index 98d9aab..7400bab 100644 --- a/Hangfire.AzureDocumentDB/Queue/JobQueueMonitoringApi.cs +++ b/Hangfire.AzureDocumentDB/Queue/JobQueueMonitoringApi.cs @@ -29,14 +29,17 @@ public IEnumerable GetQueues() { SqlQuerySpec sql = new SqlQuerySpec { - QueryText = "SELECT DISTINCT VALUE doc['name'] FROM doc WHERE doc.type = @type", + QueryText = "SELECT VALUE doc['name'] FROM doc WHERE doc.type = @type", Parameters = new SqlParameterCollection { new SqlParameter("@type", (int)DocumentTypes.Queue) } }; - IEnumerable result = storage.Client.CreateDocumentQuery(storage.CollectionUri, sql).ToQueryResult(); + IEnumerable result = storage.Client.CreateDocumentQuery(storage.CollectionUri, sql) + .ToQueryResult() + .Distinct(); + queuesCache.Clear(); queuesCache.AddRange(result); cacheUpdated = DateTime.UtcNow;