Skip to content

Commit

Permalink
#90: Provide access to RemoveTags
Browse files Browse the repository at this point in the history
  • Loading branch information
r-win committed Jul 2, 2024
1 parent 364e7d7 commit fd095a0
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 25 deletions.
2 changes: 1 addition & 1 deletion samples/Hangfire.MvcApplication/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
-->
<configuration>
<connectionStrings>
<add name="Storage" value="SqlServer" />
<add name="DefaultConnection" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Hangfire.MvcApplication.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="DefaultRedisConnection" connectionString="localhost" />
<add name="DefaultMySqlConnection" connectionString="Datasource=localhost;Database=Core_Hangfire_Sample;uid=root;pwd=admin;Allow User Variables=true" />
<add name="DefaultSqliteConnection" connectionString="C:\Temp\hangfire-tags-sample.db" />
<add name="DefaultMongoConnection" connectionString="mongodb://admin:admin@localhost/admin" />
</connectionStrings>
<appSettings>
<add key="Storage" value="SqlServer" />
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
Expand Down
79 changes: 79 additions & 0 deletions src/FaceIT.Hangfire.Tags/Extensions/HangfireExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,33 @@ public static string AddTags(this string jobid, params string[] tags)
return jobid;
}

/// <summary>
/// Removes the tags from the job with the specified id.
/// </summary>
/// <param name="jobid">The job identifier</param>
/// <param name="tags">One or more tags</param>
/// <returns>The job identifier</returns>
public static string RemoveTags(this string jobid, IEnumerable<string> tags)
{
return jobid.RemoveTags(tags.ToArray());
}

/// <summary>
/// Removes the tags from the job with the specified id.
/// </summary>
/// <param name="jobid">The job identifier</param>
/// <param name="tags">One or more tags</param>
/// <returns>The job identifier</returns>
public static string RemoveTags(this string jobid, params string[] tags)
{
using (var storage = new TagsStorage(JobStorage.Current))
{
storage.RemoveTags(jobid, tags);
}

return jobid;
}

/// <summary>
/// Retrieves the tags of the job with the specified id.
/// </summary>
Expand All @@ -58,6 +85,17 @@ public static PerformContext AddTags(this PerformContext context, IEnumerable<st
return context.AddTags(tags.ToArray());
}

/// <summary>
/// Removes the tags from the job with the specified context.
/// </summary>
/// <param name="context">The job context</param>
/// <param name="tags">One or more tags</param>
/// <returns>The job context</returns>
public static PerformContext RemoveTags(this PerformContext context, IEnumerable<string> tags)
{
return context.RemoveTags(tags.ToArray());
}

/// <summary>
/// Adds the tags to the job with the specified context.
/// </summary>
Expand All @@ -70,6 +108,18 @@ public static PerformContext AddTags(this PerformContext context, params string[
return context;
}

/// <summary>
/// Removes the tags from the job with the specified context.
/// </summary>
/// <param name="context">The job context</param>
/// <param name="tags">One or more tags</param>
/// <returns>The job context</returns>
public static PerformContext RemoveTags(this PerformContext context, params string[] tags)
{
context.BackgroundJob.Id.RemoveTags(tags);
return context;
}

/// <summary>
/// Adds the tags to the job with the specified context.
/// </summary>
Expand All @@ -82,6 +132,18 @@ public static PerformContext AddTags(this PerformContext context, JobStorage job
return context.AddTags(jobStorage, tags.ToArray());
}

/// <summary>
/// Removes the tags from the job with the specified context.
/// </summary>
/// <param name="context">The job context</param>
/// <param name="jobStorage">An instance of a job storage, only required if it differs from JobStorage.Current</param>
/// <param name="tags">One or more tags</param>
/// <returns>The job context</returns>
public static PerformContext RemoveTags(this PerformContext context, JobStorage jobStorage, IEnumerable<string> tags)
{
return context.RemoveTags(jobStorage, tags.ToArray());
}

/// <summary>
/// Adds the tags to the job with the specified context.
/// </summary>
Expand All @@ -99,6 +161,23 @@ public static PerformContext AddTags(this PerformContext context, JobStorage job
return context;
}

/// <summary>
/// Removes the tags from the job with the specified context.
/// </summary>
/// <param name="context">The job context</param>
/// <param name="jobStorage">An instance of a job storage, only required if it differs from JobStorage.Current</param>
/// <param name="tags">One or more tags</param>
/// <returns>The job context</returns>
public static PerformContext RemoveTags(this PerformContext context, JobStorage jobStorage, params string[] tags)
{
using (var storage = new TagsStorage(jobStorage ?? JobStorage.Current))
{
storage.RemoveTags(context.BackgroundJob.Id, tags);
}

return context;
}

/// <summary>
/// Gets the tags monitoring API, which can be used to query tags
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/FaceIT.Hangfire.Tags/FaceIT.Hangfire.Tags.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Authors>Erwin Bovendeur</Authors>
<Company>faceit</Company>
<Version>1.9.0-beta.4</Version>
<Version>1.9.0-beta.5</Version>
<Description>Add tags to Hangfire backgroundjobs</Description>
<Copyright />
<PackageProjectUrl>https://github.com/face-it/Hangfire.Tags</PackageProjectUrl>
Expand Down
4 changes: 3 additions & 1 deletion src/FaceIT.Hangfire.Tags/Storage/ITagsStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public interface ITagsStorage : IDisposable
/// </summary>
/// <param name="id">The job id</param>
/// <param name="tag">The tag to remove</param>
void Removetag(string id, string tag);
void RemoveTag(string id, string tag);

void RemoveTags(string id, IEnumerable<string> tags);

/// <summary>
/// Expire data for tags
Expand Down
38 changes: 16 additions & 22 deletions src/FaceIT.Hangfire.Tags/Storage/TagsStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,7 @@ public void InitTags(string jobid)

public void AddTag(string jobid, string tag)
{
using (var tran = Connection.CreateWriteTransaction())
{
if (!(tran is JobStorageTransaction))
throw new NotSupportedException(" Storage transactions must implement JobStorageTransaction");

var cleanTag = tag.Clean(_options?.Clean ?? Clean.Default, _options?.MaxTagLength);
var score = DateTime.Now.Ticks;

tran.AddToSet("tags", cleanTag, score);
tran.AddToSet(jobid.GetSetKey(), cleanTag, score);
tran.AddToSet(cleanTag.GetSetKey(), jobid, score);
tran.Commit();
}
AddTags(jobid, new[] {tag});
}

public void AddTags(string jobid, IEnumerable<string> tags)
Expand All @@ -125,24 +113,30 @@ public void AddTags(string jobid, IEnumerable<string> tags)
}
}

public void Removetag(string jobid, string tag)
public void RemoveTag(string jobid, string tag)
{
RemoveTags(jobid, new[] { tag });
}

public void RemoveTags(string jobid, IEnumerable<string> tags)
{
using (var tran = Connection.CreateWriteTransaction())
{
if (!(tran is JobStorageTransaction))
throw new NotSupportedException(" Storage transactions must implement JobStorageTransaction");

var cleanTag = tag.Clean(_options?.Clean ?? Clean.Default, _options?.MaxTagLength);
foreach (var tag in tags)
{
var cleanTag = tag.Clean(_options?.Clean ?? Clean.Default, _options?.MaxTagLength);

tran.RemoveFromSet(jobid.GetSetKey(), cleanTag);
tran.RemoveFromSet(cleanTag.GetSetKey(), jobid);
tran.RemoveFromSet(jobid.GetSetKey(), cleanTag);
tran.RemoveFromSet(cleanTag.GetSetKey(), jobid);

if (Connection.GetSetCount(cleanTag.GetSetKey()) == 0)
{
// Remove the tag, it's no longer in use
tran.RemoveFromSet("tags", cleanTag);
if (Connection.GetSetCount(cleanTag.GetSetKey()) == 0)
{
tran.RemoveFromSet("tags", cleanTag); // Use a set, because it merges by default, where a list only adds
}
}

tran.Commit();
}
}
Expand Down

0 comments on commit fd095a0

Please sign in to comment.