Skip to content
This repository has been archived by the owner on Feb 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #47 from imranmomin/develop
Browse files Browse the repository at this point in the history
3.0.3
  • Loading branch information
imranmomin authored Nov 3, 2019
2 parents a292947 + eecf316 commit 7d6b01c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 21 deletions.
27 changes: 12 additions & 15 deletions Hangfire.AzureDocumentDB/DocumentDbConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,26 +279,23 @@ public override HashSet<string> GetAllItemsFromSet(string key)
}

public override string GetFirstByLowestScoreFromSet(string key, double fromScore, double toScore)
{
return GetFirstByLowestScoreFromSet(key, fromScore, toScore, 1).FirstOrDefault();
}

public override List<string> 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.");

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<string>(Storage.CollectionUri, sql)
return Storage.Client.CreateDocumentQuery<Set>(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
Expand Down
7 changes: 5 additions & 2 deletions Hangfire.AzureDocumentDB/Queue/JobQueueMonitoringApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ public IEnumerable<string> 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<string> result = storage.Client.CreateDocumentQuery<string>(storage.CollectionUri, sql).ToQueryResult();
IEnumerable<string> result = storage.Client.CreateDocumentQuery<string>(storage.CollectionUri, sql)
.ToQueryResult()
.Distinct();

queuesCache.Clear();
queuesCache.AddRange(result);
cacheUpdated = DateTime.UtcNow;
Expand Down
12 changes: 12 additions & 0 deletions Hangfire.AzureDocumentDB/StoredProcedure/setJobState.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ 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);
if (key !== newKey) {
state.data[newKey] = state.data[key];
delete state.data[key];
}
}
response.setBody(false);
let isAccepted = collection.readDocument(documentLink, (error, job) => {
if (error) {
Expand Down Expand Up @@ -35,6 +43,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");
}
Expand Down
19 changes: 19 additions & 0 deletions Hangfire.AzureDocumentDB/StoredProcedure/setJobState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ 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<string> = Object.keys(state.data);
for (const key of keys) {
const newKey = camelCaseToPascalCase(key);
if (key !== newKey) {
state.data[newKey] = state.data[key];
delete state.data[key];
}
}

// default response
response.setBody(false);

Expand Down Expand Up @@ -56,6 +66,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");
}
Expand Down
6 changes: 3 additions & 3 deletions Hangfire.AzureDocumentDB/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Hangfire.AzureDocumentDB/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"private": true,
"devDependencies": {
"@types/documentdb-server": "0.0.32",
"typescript": "3.1.1"
"typescript": "3.6.4"
}
}

0 comments on commit 7d6b01c

Please sign in to comment.