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 #53 from imranmomin/develop
Browse files Browse the repository at this point in the history
3.1.0
  • Loading branch information
imranmomin committed Mar 11, 2020
2 parents ce27098 + 64fc3de commit cee31f3
Show file tree
Hide file tree
Showing 60 changed files with 1,668 additions and 1,591 deletions.
9 changes: 6 additions & 3 deletions Hangfire.AzureDocumentDB.sln
@@ -1,9 +1,9 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25123.0
# Visual Studio Version 16
VisualStudioVersion = 16.0.29806.167
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hangfire.AzureDocumentDB", "Hangfire.AzureDocumentDB\Hangfire.AzureDocumentDB.csproj", "{E0AD3801-5504-49A7-80C6-8B4373DDE0E5}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hangfire.AzureDocumentDB", "src\Hangfire.AzureDocumentDB.csproj", "{E0AD3801-5504-49A7-80C6-8B4373DDE0E5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -19,4 +19,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C15DCC20-26DF-41C7-8C83-0A8D63706B22}
EndGlobalSection
EndGlobal
27 changes: 0 additions & 27 deletions Hangfire.AzureDocumentDB/Helper/QueryHelper.cs

This file was deleted.

53 changes: 0 additions & 53 deletions Hangfire.AzureDocumentDB/StoredProcedure/setJobState.js

This file was deleted.

81 changes: 0 additions & 81 deletions Hangfire.AzureDocumentDB/StoredProcedure/setJobState.ts

This file was deleted.

24 changes: 21 additions & 3 deletions README.md
Expand Up @@ -4,17 +4,28 @@
[![Latest version](https://img.shields.io/nuget/v/Hangfire.AzureDocumentDB.svg)](https://www.nuget.org/packages/Hangfire.AzureDocumentDB)
[![Build status](https://ci.appveyor.com/api/projects/status/uvxh94dhxcokga47?svg=true)](https://ci.appveyor.com/project/imranmomin/hangfire-azuredocumentdb)

This repo will add a [Microsoft Azure DocumentDB](https://azure.microsoft.com/en-ca/services/documentdb) storage support to [Hangfire](http://hangfire.io) - fire-and-forget, delayed and recurring tasks runner for .NET. Scalable and reliable background job runner. Supports multiple servers, CPU and I/O intensive, long-running and short-running jobs.
This repo will add a [Microsoft Azure Cosmos DB](https://azure.microsoft.com/en-ca/services/cosmos-db) storage support to [Hangfire](http://hangfire.io) - fire-and-forget, delayed and recurring tasks runner for .NET. Scalable and reliable background job runner. Supports multiple servers, CPU and I/O intensive, long-running and short-running jobs.


## Installation

[Hangfire.AzureDocumentDB](https://www.nuget.org/packages/Hangfire.AzureDocumentDB) is available as a NuGet package. Install it using the NuGet Package Console window:
[Hangfire.AzureDocumentDB](https://www.nuget.org/packages/Hangfire.AzureDocumentDB) is available on NuGet.


Package Manager
```powershell
PM> Install-Package Hangfire.AzureDocumentDB
```

.NET CLI
```
> dotnet add package Hangfire.AzureDocumentDB
```

PackageReference
```xml
<PackageReference Include="Hangfire.AzureDocumentDB" Version="0.0.0" />
```

## Usage

Expand All @@ -36,7 +47,8 @@ Hangfire.Azure.DocumentDbStorageOptions options = new Hangfire.Azure.DocumentDbS
CountersAggregateInterval = TimeSpan.FromMinutes(2),
QueuePollInterval = TimeSpan.FromSeconds(15),
ConnectionMode = ConnectionMode.Direct,
ConnectionProtocol = Protocol.Tcp
ConnectionProtocol = Protocol.Tcp,
EnablePartition = false // default: false true; to enable partition on /type
};

GlobalConfiguration.Configuration.UseAzureDocumentDbStorage("<url>", "<authSecret>", "<databaseName>", "<collectionName>", options);
Expand All @@ -47,6 +59,12 @@ Hangfire.Azure.DocumentDbStorage storage = new Hangfire.Azure.DocumentDbStorage(
GlobalConfiguration.Configuration.UseStorage(storage);
```

## Recommendations
- Keep seperate database/collection for the hangfire. (Now you can [enable free tier](https://docs.microsoft.com/en-us/azure/cosmos-db/optimize-dev-test#azure-cosmos-db-free-tier) on Azure)
- Enable partitioning by ```/type```

## SDK Support
This package only support using [Microsoft.Azure.DocumentDB](https://www.nuget.org/packages/Microsoft.Azure.DocumentDB/). If you want the support for the latest SDK v3 [Microsoft.Azure.Cosmos](https://www.nuget.org/packages/Microsoft.Azure.Cosmos), you will have to use [Hangfire.AzureCosmosDb](https://github.com/imranmomin/Hangfire.AzureCosmosDb)

## Questions? Problems?

Expand Down
@@ -1,18 +1,18 @@
using System;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;

using Hangfire.Server;
using Hangfire.Azure.Documents;
using Hangfire.Azure.Helper;
using Hangfire.Logging;
using Hangfire.Server;

using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;

using Hangfire.Azure.Helper;
using Hangfire.Azure.Documents;

namespace Hangfire.Azure
{
#pragma warning disable 618
Expand All @@ -23,6 +23,7 @@ internal class CountersAggregator : IServerComponent
private const string DISTRIBUTED_LOCK_KEY = "locks:counters:aggragator";
private readonly TimeSpan defaultLockTimeout;
private readonly DocumentDbStorage storage;
private readonly PartitionKey partitionKey = new PartitionKey((int)DocumentTypes.Counter);

public CountersAggregator(DocumentDbStorage storage)
{
Expand All @@ -36,9 +37,10 @@ public void Execute(CancellationToken cancellationToken)
{
logger.Trace("Aggregating records in 'Counter' table.");

List<Counter> rawCounters = storage.Client.CreateDocumentQuery<Counter>(storage.CollectionUri)
List<Counter> rawCounters = storage.Client.CreateDocumentQueryAsync<Counter>(storage.CollectionUri, new FeedOptions { PartitionKey = partitionKey })
.Where(c => c.DocumentType == DocumentTypes.Counter && c.Type == CounterTypes.Raw)
.ToQueryResult();
.ToQueryResult()
.ToList();

Dictionary<string, (int Value, DateTime? ExpireOn, List<Counter> Counters)> counters = rawCounters.GroupBy(c => c.Key)
.ToDictionary(k => k.Key, v => (Value: v.Sum(c => c.Value), ExpireOn: v.Max(c => c.ExpireOn), Counters: v.ToList()));
Expand All @@ -55,7 +57,7 @@ public void Execute(CancellationToken cancellationToken)

try
{
Task<DocumentResponse<Counter>> readTask = storage.Client.ReadDocumentWithRetriesAsync<Counter>(uri, cancellationToken: cancellationToken);
Task<DocumentResponse<Counter>> readTask = storage.Client.ReadDocumentWithRetriesAsync<Counter>(uri, new RequestOptions { PartitionKey = partitionKey }, cancellationToken: cancellationToken);
readTask.Wait(cancellationToken);

if (readTask.Result.StatusCode == HttpStatusCode.OK)
Expand Down Expand Up @@ -90,15 +92,15 @@ public void Execute(CancellationToken cancellationToken)
}
}

Task<ResourceResponse<Document>> task = storage.Client.UpsertDocumentWithRetriesAsync(storage.CollectionUri, aggregated, cancellationToken: cancellationToken);
Task<ResourceResponse<Document>> task = storage.Client.UpsertDocumentWithRetriesAsync(storage.CollectionUri, aggregated, new RequestOptions { PartitionKey = partitionKey }, cancellationToken: cancellationToken);
Task continueTask = task.ContinueWith(t =>
{
if (t.Result.StatusCode == HttpStatusCode.Created || t.Result.StatusCode == HttpStatusCode.OK)
{
string ids = string.Join(",", data.Counters.Select(c => $"'{c.Id}'").ToArray());
string query = $"SELECT doc._self FROM doc WHERE doc.type = {(int)DocumentTypes.Counter} AND doc.counter_type = {(int)CounterTypes.Raw} AND doc.id IN ({ids})";
int deleted = storage.Client.ExecuteDeleteDocuments(query);
int deleted = storage.Client.ExecuteDeleteDocuments(query, new RequestOptions { PartitionKey = partitionKey }, cancellationToken);
logger.Trace($"Total {deleted} records from the 'Counter:{aggregated.Key}' were aggregated.");
}
Expand Down

0 comments on commit cee31f3

Please sign in to comment.