Skip to content

Commit

Permalink
Merge pull request #6 from raisedapp/develop
Browse files Browse the repository at this point in the history
First beta!
  • Loading branch information
felixclase authored Nov 28, 2019
2 parents 0236ede + 9c203a8 commit 8afd3f8
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 109 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Hangfire.Storage.SQLite
[![NuGet](https://buildstats.info/nuget/Hangfire.Storage.SQLite)](https://www.nuget.org/packages/Hangfire.Storage.SQLite)
[![Actions Status](https://github.com/felixclase/Hangfire.Storage.SQLite/workflows/CI-HS-SQLITE/badge.svg)](https://github.com/felixclase/Hangfire.Storage.SQLite/actions)
[![Actions Status](https://github.com/raisedapp/Hangfire.Storage.SQLite/workflows/CI-HS-SQLITE/badge.svg)](https://github.com/raisedapp/Hangfire.Storage.SQLite/actions)
[![Official Site](https://img.shields.io/badge/site-hangfire.io-blue.svg)](http://hangfire.io)
[![License MIT](https://img.shields.io/badge/license-MIT-green.svg)](http://opensource.org/licenses/MIT)

Expand All @@ -13,8 +13,8 @@ This project was created by abandonment **Hangfire.SQLite** storage (https://git
## Build Status
`Platform` | `Master` | `Develop`
--- | --- | ---
**Windows** | [![Build Status](https://circleci.com/gh/felixclase/Hangfire.Storage.SQLite/tree/master.svg?style=svg)](https://circleci.com/gh/felixclase/Hangfire.Storage.SQLite/tree/master) | [![Build Status](https://circleci.com/gh/felixclase/Hangfire.Storage.SQLite/tree/develop.svg?style=svg)](https://circleci.com/gh/felixclase/Hangfire.Storage.SQLite/tree/develop)
**Linux / Mac OS** | [![Build Status](https://travis-ci.org/felixclase/Hangfire.Storage.SQLite.svg?branch=master)](https://travis-ci.org/felixclase/Hangfire.Storage.SQLite/) | [![Build Status](https://travis-ci.org/felixclase/Hangfire.Storage.SQLite.svg?branch=develop)](https://travis-ci.org/felixclase/Hangfire.Storage.SQLite/)
**Windows** | [![Build Status](https://circleci.com/gh/raisedapp/Hangfire.Storage.SQLite/tree/master.svg?style=svg)](https://circleci.com/gh/raisedapp/Hangfire.Storage.SQLite/tree/master) | [![Build Status](https://circleci.com/gh/raisedapp/Hangfire.Storage.SQLite/tree/develop.svg?style=svg)](https://circleci.com/gh/raisedapp/Hangfire.Storage.SQLite/tree/develop)
**Linux / Mac OS** | [![Build Status](https://travis-ci.org/raisedapp/Hangfire.Storage.SQLite.svg?branch=master)](https://travis-ci.org/raisedapp/Hangfire.Storage.SQLite/) | [![Build Status](https://travis-ci.org/raisedapp/Hangfire.Storage.SQLite.svg?branch=develop)](https://travis-ci.org/raisedapp/Hangfire.Storage.SQLite/)

## Installation

Expand Down Expand Up @@ -50,4 +50,4 @@ If this project help you reduce time to develop, you can give me a cup of coffee
[![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=RMLQM296TCM38&item_name=For+the+development+of+Hangfire.Storage.SQLite&currency_code=USD&source=url)

## License
This project is under MIT license. You can obtain the license copy [here](https://github.com/felixclase/Hangfire.Storage.SQLite/blob/develop/LICENSE).
This project is under MIT license. You can obtain the license copy [here](https://github.com/raisedapp/Hangfire.Storage.SQLite/blob/develop/LICENSE).
15 changes: 3 additions & 12 deletions src/main/Hangfire.Storage.SQLite/Entities/HangfireList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,17 @@ namespace Hangfire.Storage.SQLite.Entities
[Table("List")]
public class HangfireList
{
private string _listPK = string.Empty;

[PrimaryKey]
public string ListPK {
get => Id + "_" + Key;
set => _listPK = value;
}

//[AutoIncrement]
[Indexed(Name = "IX_List_Id", Order = 1, Unique = false)]
[PrimaryKey, AutoIncrement]
public int Id { get; set; }

[MaxLength(DefaultValues.MaxLengthKeyColumn)]
[Indexed(Name = "IX_List_Key", Order = 2, Unique = false)]
[Indexed(Name = "IX_List_Key", Order = 1, Unique = false)]
public string Key { get; set; }

[MaxLength(DefaultValues.MaxLengthVarCharColumn)]
public string Value { get; set; }

[Indexed(Name = "IX_List_ExpireAt", Order = 3, Unique = false)]
[Indexed(Name = "IX_List_ExpireAt", Order = 2, Unique = false)]
public DateTime ExpireAt { get; set; }
}
}
9 changes: 2 additions & 7 deletions src/main/Hangfire.Storage.SQLite/Entities/Hash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@ namespace Hangfire.Storage.SQLite.Entities
{
public class Hash
{
private string _hashPK = string.Empty;

[PrimaryKey]
public string HashPK {
get => Key + "_" + Field;
set => _hashPK = value;
}
[PrimaryKey, AutoIncrement]
public int Id { get; set; }

[MaxLength(DefaultValues.MaxLengthKeyColumn)]
[Indexed(Name = "IX_Hash_Key", Order = 1, Unique = false)]
Expand Down
9 changes: 2 additions & 7 deletions src/main/Hangfire.Storage.SQLite/Entities/JobParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@ namespace Hangfire.Storage.SQLite.Entities
{
public class JobParameter
{
private string _jobParameterPK = string.Empty;

[PrimaryKey]
public string JobParameterPK {
get => JobId + "_" + Name;
set => _jobParameterPK = value;
}
[PrimaryKey, AutoIncrement]
public int Id { get; set; }

[Indexed(Name = "IX_JobParameter_JobId", Order = 1, Unique = false)]
public int JobId { get; set; }
Expand Down
14 changes: 3 additions & 11 deletions src/main/Hangfire.Storage.SQLite/Entities/JobQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,16 @@ namespace Hangfire.Storage.SQLite.Entities
{
public class JobQueue
{
private string _jobQueuePK = string.Empty;
[PrimaryKey]
public string JobQueuePK {
get => Id + "_" + Queue;
set => _jobQueuePK = value;
}

//[AutoIncrement]
[Indexed(Name = "IX_JobQueue_Id", Order = 1, Unique = false)]
[PrimaryKey, AutoIncrement]
public int Id { get; set; }

public int JobId { get; set; }

[MaxLength(DefaultValues.MaxLengthQueueColumn)]
[Indexed(Name = "IX_JobQueue_Queue", Order = 2, Unique = false)]
[Indexed(Name = "IX_JobQueue_Queue", Order = 1, Unique = false)]
public string Queue { get; set; }

[Indexed(Name = "IX_JobQueue_FetchedAt", Order = 3, Unique = false)]
[Indexed(Name = "IX_JobQueue_FetchedAt", Order = 2, Unique = false)]
public DateTime FetchedAt { get; set; }
}
}
6 changes: 2 additions & 4 deletions src/main/Hangfire.Storage.SQLite/Entities/Set.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ namespace Hangfire.Storage.SQLite.Entities
{
public class Set
{
private string _setPK = string.Empty;

[PrimaryKey]
public string SetPK { get { return Key + "_" + Value; } set { _setPK = value; } }
[PrimaryKey, AutoIncrement]
public int Id { get; set; }

[MaxLength(DefaultValues.MaxLengthKeyColumn)]
[Indexed(Name = "IX_Set_Key", Order = 1, Unique = false)]
Expand Down
13 changes: 2 additions & 11 deletions src/main/Hangfire.Storage.SQLite/Entities/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,10 @@ namespace Hangfire.Storage.SQLite.Entities
{
public class State
{
private string _statePK = string.Empty;

[PrimaryKey]
public string StatePK {
get => Id + "_" + JobId;
set => _statePK = value;
}

//[AutoIncrement]
[Indexed(Name = "IX_State_Id", Order = 1, Unique = false)]
[PrimaryKey, AutoIncrement]
public int Id { get; set; }

[Indexed(Name = "IX_State_JobId", Order = 2, Unique = false)]
[Indexed(Name = "IX_State_JobId", Order = 1, Unique = false)]
public int JobId { get; set; }

[MaxLength(DefaultValues.MaxLengthStateNameColumn)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ public override Dictionary<string, string> GetAllEntriesFromHash(string key)
var result = DbContext
.HashRepository
.Where(_ => _.Key == key)
.AsEnumerable()
.Select(_ => new { _.Field, _.Value })
.ToList()
.ToDictionary(x => x.Field, x => x.Value);

return result.Count != 0 ? result : null;
Expand Down
2 changes: 1 addition & 1 deletion src/main/Hangfire.Storage.SQLite/SQLiteFetchedJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Hangfire.Storage.SQLite
/// <summary>
///
/// </summary>
public class SQLiteFetchedJob
public class SQLiteFetchedJob : IFetchedJob
{
private readonly HangfireDbContext _dbContext;
private readonly int _id;
Expand Down
40 changes: 11 additions & 29 deletions src/main/Hangfire.Storage.SQLite/SQLiteJobQueue.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Hangfire.Storage.SQLite.Entities;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -30,7 +31,6 @@ public SQLiteJobQueue(HangfireDbContext connection, SQLiteStorageOptions storage
/// <returns></returns>
public IFetchedJob Dequeue(string[] queues, CancellationToken cancellationToken)
{
/*
if (queues == null)
{
throw new ArgumentNullException(nameof(queues));
Expand All @@ -41,51 +41,35 @@ public IFetchedJob Dequeue(string[] queues, CancellationToken cancellationToken)
throw new ArgumentException("Queue array must be non-empty.", nameof(queues));
}

var fetchConditions = new[]
{
Query.EQ("FetchedAt", null),
Query.LT("FetchedAt", DateTime.UtcNow.AddSeconds(_storageOptions.InvisibilityTimeout.Negate().TotalSeconds))
};
var fetchConditionsIndex = 0;
JobQueue fetchedJob = null;
while (fetchedJob == null)
{
cancellationToken.ThrowIfCancellationRequested();

var fetchCondition = fetchConditions[fetchConditionsIndex];
foreach (var queue in queues)
{
var lockQueue = string.Intern($"f13333e1-a0c8-48c8-bf8c-788e89030329_{queue}");
lock (lockQueue)
{
fetchedJob = _connection.JobQueue.FindOne(Query.And(fetchCondition, Query.EQ("Queue", queue)));
fetchedJob = _dbContext.JobQueueRepository.FirstOrDefault(_ => _.Queue == queue &&
(_.FetchedAt == DateTime.MinValue));

if (fetchedJob != null)
{
fetchedJob.FetchedAt = DateTime.UtcNow;
_connection.JobQueue.Update(fetchedJob);
_dbContext.Database.Update(fetchedJob);

break;
}
}
}

if (fetchedJob == null && fetchConditionsIndex == fetchConditions.Length - 1)
{
// ...and we are out of fetch conditions as well.
// Wait for a while before polling again.
cancellationToken.WaitHandle.WaitOne(_storageOptions.QueuePollInterval);
cancellationToken.ThrowIfCancellationRequested();
}
// Move on to next fetch condition
fetchConditionsIndex = (fetchConditionsIndex + 1) % fetchConditions.Length;
// Wait for a while before polling again.
cancellationToken.WaitHandle.WaitOne(_storageOptions.QueuePollInterval);
cancellationToken.ThrowIfCancellationRequested();
}

return new LiteDbFetchedJob(_connection, fetchedJob.Id, fetchedJob.JobId, fetchedJob.Queue);
*/
return null;
return new SQLiteFetchedJob(_dbContext, fetchedJob.Id, fetchedJob.JobId, fetchedJob.Queue);
}

/// <summary>
Expand All @@ -95,13 +79,11 @@ public IFetchedJob Dequeue(string[] queues, CancellationToken cancellationToken)
/// <param name="jobId"></param>
public void Enqueue(string queue, string jobId)
{
/*
_connection.JobQueue.Insert(new JobQueue
_dbContext.Database.Insert(new JobQueue
{
JobId = int.Parse(jobId),
Queue = queue
});
*/
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public SQLiteJobQueueMonitoringApi(HangfireDbContext connection)
/// <returns></returns>
public EnqueuedAndFetchedCountDto GetEnqueuedAndFetchedCount(string queue)
{
var enqueuedCount = _dbContext.JobQueueRepository.Count(_ => _.Queue == queue && _.FetchedAt == null);
var enqueuedCount = _dbContext.JobQueueRepository.Count(_ => _.Queue == queue && _.FetchedAt == DateTime.MinValue);

var fetchedCount = _dbContext.JobQueueRepository.Count(_ => _.Queue == queue && _.FetchedAt != null);
var fetchedCount = _dbContext.JobQueueRepository.Count(_ => _.Queue == queue && _.FetchedAt != DateTime.MinValue);

return new EnqueuedAndFetchedCountDto
{
Expand All @@ -49,7 +49,7 @@ public EnqueuedAndFetchedCountDto GetEnqueuedAndFetchedCount(string queue)
public IEnumerable<int> GetEnqueuedJobIds(string queue, int from, int perPage)
{
return _dbContext.JobQueueRepository
.Where(_ => _.Queue == queue && _.FetchedAt == null)
.Where(_ => _.Queue == queue && _.FetchedAt == DateTime.MinValue)
.Skip(from)
.Take(perPage)
.Select(_ => _.JobId)
Expand All @@ -70,7 +70,7 @@ public IEnumerable<int> GetEnqueuedJobIds(string queue, int from, int perPage)
public IEnumerable<int> GetFetchedJobIds(string queue, int from, int perPage)
{
return _dbContext.JobQueueRepository
.Where(_ => _.Queue == queue && _.FetchedAt != null)
.Where(_ => _.Queue == queue && _.FetchedAt != DateTime.MinValue)
.Skip(from)
.Take(perPage)
.Select(_ => _.JobId)
Expand Down
18 changes: 10 additions & 8 deletions src/main/Hangfire.Storage.SQLite/SQLiteMonitoringApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,11 @@ private JobList<EnqueuedJobDto> EnqueuedJobs(HangfireDbContext connection, IEnum
.Where(_ => jobIds.Contains(_.Id))
.ToList();

//SQLITE PCL LIMITED LAMBDA SUPPORT!!
var enqueuedJobs = connection.JobQueueRepository
.Where(_ => _.FetchedAt == null && jobs.Select(x => x.Id).Contains(_.JobId))
.Where(_ => _.FetchedAt == DateTime.MinValue)
.ToList()
.Where(_ => jobs.Select(x => x.Id).Contains(_.JobId))
.ToList();

var jobsFiltered = enqueuedJobs
Expand Down Expand Up @@ -225,21 +228,23 @@ private Dictionary<DateTime, long> GetHourlyTimelineStats(HangfireDbContext conn

private JobList<FetchedJobDto> FetchedJobs(HangfireDbContext connection, IEnumerable<int> jobIds)
{
/*
var jobs = connection.HangfireJobRepository
.Where(_ => jobIds.Contains(_.Id))
.ToList();

//SQLITE LAMBDA SUPPORT IS LIMITED!!
var jobIdToJobQueueMap = connection.JobQueueRepository
.Where(_ => _.FetchedAt != null && jobs.Select(x => x.Id).Contains(_.JobId))
.Where(_ => _.FetchedAt != DateTime.MinValue).ToList()
.Where(_ => jobs.Select(x => x.Id).Contains(_.JobId))
.AsEnumerable().ToDictionary(_ => _.JobId, _ => _);

IEnumerable<JobDto> jobsFiltered = jobs.Where(_ => jobIdToJobQueueMap.ContainsKey(_.Id));
var jobsFiltered = jobs.Where(_ => jobIdToJobQueueMap.ContainsKey(_.Id));

List<JobDetailedDto> joinedJobs = jobsFiltered
.Select(job =>
{
var state = job.StateHistory.FirstOrDefault(s => s.Name == job.StateName);
var state = connection.StateRepository.FirstOrDefault(s => s.Name == job.StateName);

return new JobDetailedDto
{
Id = job.Id,
Expand Down Expand Up @@ -270,9 +275,6 @@ private JobList<FetchedJobDto> FetchedJobs(HangfireDbContext connection, IEnumer
}

return new JobList<FetchedJobDto>(result);
*/

return null;
}

public JobList<DeletedJobDto> DeletedJobs(int from, int count)
Expand Down
Loading

0 comments on commit 8afd3f8

Please sign in to comment.