Skip to content

Instantly share code, notes, and snippets.

@odinserj
Last active April 23, 2026 08:03
Show Gist options
  • Select an option

  • Save odinserj/a8332a3f486773baa009 to your computer and use it in GitHub Desktop.

Select an option

Save odinserj/a8332a3f486773baa009 to your computer and use it in GitHub Desktop.
// Zero-Clause BSD (more permissive than MIT, doesn't require copyright notice)
//
// Permission to use, copy, modify, and/or distribute this software for any purpose
// with or without fee is hereby granted.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
// OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
// THIS SOFTWARE.
public class DisableMultipleQueuedItemsFilter : JobFilterAttribute, IClientFilter, IServerFilter
{
private static readonly TimeSpan LockTimeout = TimeSpan.FromSeconds(5);
private static readonly TimeSpan FingerprintTimeout = TimeSpan.FromHours(1);
public void OnCreating(CreatingContext filterContext)
{
if (!AddFingerprintIfNotExists(filterContext.Connection, filterContext.Job))
{
filterContext.Canceled = true;
}
}
public void OnPerformed(PerformedContext filterContext)
{
RemoveFingerprint(filterContext.Connection, filterContext.Job);
}
private static bool AddFingerprintIfNotExists(IStorageConnection connection, Job job)
{
using (connection.AcquireDistributedLock(GetFingerprintLockKey(job), LockTimeout))
{
var fingerprint = connection.GetAllEntriesFromHash(GetFingerprintKey(job));
DateTimeOffset timestamp;
if (fingerprint != null &&
fingerprint.ContainsKey("Timestamp") &&
DateTimeOffset.TryParse(fingerprint["Timestamp"], null, DateTimeStyles.RoundtripKind, out timestamp) &&
DateTimeOffset.UtcNow <= timestamp.Add(FingerprintTimeout))
{
// Actual fingerprint found, returning.
return false;
}
// Fingerprint does not exist, it is invalid (no `Timestamp` key),
// or it is not actual (timeout expired).
connection.SetRangeInHash(GetFingerprintKey(job), new Dictionary<string, string>
{
{ "Timestamp", DateTimeOffset.UtcNow.ToString("o") }
});
return true;
}
}
private static void RemoveFingerprint(IStorageConnection connection, Job job)
{
using (connection.AcquireDistributedLock(GetFingerprintLockKey(job), LockTimeout))
using (var transaction = connection.CreateWriteTransaction())
{
transaction.RemoveHash(GetFingerprintKey(job));
transaction.Commit();
}
}
private static string GetFingerprintLockKey(Job job)
{
return String.Format("{0}:lock", GetFingerprintKey(job));
}
private static string GetFingerprintKey(Job job)
{
return String.Format("fingerprint:{0}", GetFingerprint(job));
}
private static string GetFingerprint(Job job)
{
string parameters = string.Empty;
if (job.Arguments != null)
{
parameters = string.Join(".", job.Arguments);
}
if (job.Type == null || job.Method == null)
{
return string.Empty;
}
var fingerprint = String.Format(
"{0}.{1}.{2}",
job.Type.FullName,
job.Method.Name, parameters);
return fingerprint;
}
void IClientFilter.OnCreated(CreatedContext filterContext)
{
}
void IServerFilter.OnPerforming(PerformingContext filterContext)
{
}
}
@DevineDevelopers
Copy link
Copy Markdown

@HenrikHoyer Did you ever figure out the code @afelinczak was talking about

@afelinczak
Copy link
Copy Markdown

afelinczak commented Sep 23, 2024

Hello, I missed the comment - sorry.
This is the fix we are using.

private static string ConvertArgument(object obj) => obj switch { CancellationToken => String.Empty, _ => JsonConvert.SerializeObject(obj) };

@FixRM
Copy link
Copy Markdown

FixRM commented Oct 31, 2024

Hangfire replaced it with null in Parameters

Hello @afelinczak. What do you mean? CancellationToken is a struct, it can't be null. We must pass it as the default but it'll be replaced with real token in runtime. So it shouldn't be null anyway.

Btw. There is at least one more "special" parameter: PerformContext. It is not well documented but is is used by popular extension Hangfire.Console

@pinki
Copy link
Copy Markdown

pinki commented Oct 1, 2025

The attribute does not work for me.
I tried applying it to the job's class and to the job's execution method.
Neither seems to work.

@Laubs
Copy link
Copy Markdown

Laubs commented Apr 23, 2026

By persisting the JobId, the system gains resilience against service restarts. If the service goes down and later recovers, the job can be re-scheduled and processed again. Since the same JobId is preserved, the fingerprint logic recognizes it as the same execution context and allows it to proceed, preventing the system from being stuck in a permanently locked state waiting for the FingerprintTimeout.

Tested with recurring jobs, which is why I couldn’t use OnPerforming, it is not being called.

public class DisableMultipleQueuedItemsFilterAttribute : JobFilterAttribute, IServerFilter, IElectStateFilter
{
    private static readonly ILog log =
        LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    private static readonly TimeSpan LockTimeout = TimeSpan.FromSeconds(5);
    private static readonly TimeSpan FingerprintTimeout = TimeSpan.FromHours(1);

    private const String TimestampKey = "Timestamp";
    private const String JobIdKey = "JobId";

    private const String FingerprintPrefix = "fingerprint:";
    private const String LockSuffix = ":lock";

    public void OnStateElection(ElectStateContext context)
    {
        log.Debug("Entering method DisableMultipleQueuedItemsFilterAttribute::OnStateElection");

        var backgroundJob = context.BackgroundJob;
        var job = backgroundJob.Job;
        var jobId = backgroundJob.Id;

        if (context.CandidateState is ProcessingState)
        {
            var connection = context.Connection;
            var added = AddFingerprintIfNotExists(connection, job, jobId);

            if (!added)
            {
                var deletedState = new DeletedState
                {
                    Reason = "Active fingerprint: an execution is already in progress."
                };

                context.CandidateState = deletedState;
            }
        }

        log.Debug("Exiting method DisableMultipleQueuedItemsFilterAttribute::OnStateElection");
    }

    public void OnPerformed(PerformedContext filterContext)
    {
        log.Debug("Entering method DisableMultipleQueuedItemsFilterAttribute::OnPerformed");

        var backgroundJob = filterContext.BackgroundJob;
        var job = backgroundJob.Job;
        var connection = filterContext.Connection;

        RemoveFingerprint(connection, job);

        log.Debug("Exiting method DisableMultipleQueuedItemsFilterAttribute::OnPerformed");
    }

    private static Boolean AddFingerprintIfNotExists(
        IStorageConnection connection,
        Job job,
        String backgroundJobId)
    {
        log.Debug("Entering method DisableMultipleQueuedItemsFilterAttribute::AddFingerprintIfNotExists");

        var fingerprintKey = GetFingerprintKey(job);
        var fingerprintLockKey = GetFingerprintLockKey(job);

        using (connection.AcquireDistributedLock(fingerprintLockKey, LockTimeout))
        {
            var fingerprint = connection.GetAllEntriesFromHash(fingerprintKey);

            if (fingerprint != null && fingerprint.ContainsKey(TimestampKey))
            {
                var hasJobId = fingerprint.ContainsKey(JobIdKey);

                if (hasJobId)
                {
                    var storedJobId = fingerprint[JobIdKey];

                    if (storedJobId == backgroundJobId)
                    {
                        UpdateFingerprint(connection, job, backgroundJobId);

                        log.Debug("Fingerprint belongs to the same JobId. Updating.");

                        log.Debug("Exiting method DisableMultipleQueuedItemsFilterAttribute::AddFingerprintIfNotExists");
                        return true;
                    }
                }

                var timestampString = fingerprint[TimestampKey];

                var parsed = DateTimeOffset.TryParse(
                    timestampString,
                    null,
                    DateTimeStyles.RoundtripKind,
                    out var timestamp);

                if (parsed)
                {
                    var expiration = timestamp.Add(FingerprintTimeout);
                    var now = DateTimeOffset.UtcNow;

                    if (now <= expiration)
                    {
                        log.Debug("Active fingerprint found.");

                        log.Debug("Exiting method DisableMultipleQueuedItemsFilterAttribute::AddFingerprintIfNotExists");
                        return false;
                    }
                }
            }

            UpdateFingerprint(connection, job, backgroundJobId);

            log.Debug("Fingerprint created.");

            log.Debug("Exiting method DisableMultipleQueuedItemsFilterAttribute::AddFingerprintIfNotExists");
            return true;
        }
    }

    private static void UpdateFingerprint(
        IStorageConnection connection,
        Job job,
        String backgroundJobId)
    {
        log.Debug("Entering method DisableMultipleQueuedItemsFilterAttribute::UpdateFingerprint");

        var fingerprintKey = GetFingerprintKey(job);

        var values = new Dictionary<String, String>
        {
            { TimestampKey, DateTimeOffset.UtcNow.ToString("o") },
            { JobIdKey, backgroundJobId }
        };

        connection.SetRangeInHash(fingerprintKey, values);

        log.Debug("Exiting method DisableMultipleQueuedItemsFilterAttribute::UpdateFingerprint");
    }

    private static void RemoveFingerprint(
        IStorageConnection connection,
        Job job)
    {
        log.Debug("Entering method DisableMultipleQueuedItemsFilterAttribute::RemoveFingerprint");

        var fingerprintKey = GetFingerprintKey(job);
        var fingerprintLockKey = GetFingerprintLockKey(job);

        using (connection.AcquireDistributedLock(fingerprintLockKey, LockTimeout))
        using (var transaction = connection.CreateWriteTransaction())
        {
            transaction.RemoveHash(fingerprintKey);
            transaction.Commit();
        }

        log.Debug("Exiting method DisableMultipleQueuedItemsFilterAttribute::RemoveFingerprint");
    }

    private static String GetFingerprint(Job job)
    {
        log.Debug("Entering method DisableMultipleQueuedItemsFilterAttribute::GetFingerprint");

        var args = job.Args;
        var parameters = String.Empty;

        if (args != null)
            parameters = String.Join(".", args);

        var jobType = job.Type;
        var jobMethod = job.Method;

        if (jobType == null || jobMethod == null)
            return String.Empty;

        var typeName = jobType.FullName;
        var methodName = jobMethod.Name;

        var payload = $"{typeName}.{methodName}.{parameters}";

        var encoding = System.Text.Encoding.UTF8;
        var payloadBytes = encoding.GetBytes(payload);

        var sha = SHA256.Create();
        var hashBytes = sha.ComputeHash(payloadBytes);

        var fingerprint = Convert.ToBase64String(hashBytes);

        log.Debug("Exiting method DisableMultipleQueuedItemsFilterAttribute::GetFingerprint");

        return fingerprint;
    }

    private static String GetFingerprintLockKey(Job job)
    {
        var fingerprintKey = GetFingerprintKey(job);
        var lockKey = String.Format("{0}{1}", fingerprintKey, LockSuffix);

        return lockKey;
    }

    private static String GetFingerprintKey(Job job)
    {
        var fingerprint = GetFingerprint(job);
        var key = String.Format("{0}{1}", FingerprintPrefix, fingerprint);

        return key;
    }

    public void OnPerforming(PerformingContext filterContext)
    {
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment