Prevent to Enqueue Duplicate Jobs in Hangfire


#1

I faced with a problem several days ago, I have seen several duplicate same jobs in Enqueued jobs,
Firstly I was thinking this is my logic problem but after doing patch I am facing with this problem again,

I enqueued job like:

BackgroundJob.Enqueue(() => RoutingCase.Start($"Routing {r.m_PatientName} ({r.m_RoutingFiles.Length + r.m_RoutingTempFiles.Length})=> {r.Server.Description}", r, null));

my patch to prevent duplicate jobs by hash of some properties is something like:
bool detectDuplicatedRouteJob = false; try { detectDuplicatedRouteJob = currentMonitorApi.Queues().Where(ser => ser.Name.StartsWith("route")) .Any(routeQueue => { //Check in Routing Processing jobs first bool dupl = currentMonitorApi.FetchedJobs(routeQueue.Name, 0, (int)routeQueue.Fetched).Any(pj => (pj.Value.Job.Args[1] as RoutingCase).GetHashCode() == r.GetHashCode()) || routeQueue.FirstJobs.Any(job => ((job.Value.Job.Args[1]) as RoutingCase).GetHashCode() == r.GetHashCode()); //check from Enqueued Routing Jobs also return dupl; }); } catch (Exception exp) { m_Logging.Log(LoggingMode.Warning, $"Failed to Check Duplicaye Route job for, Routing {r.m_PatientName} ({r.m_RoutingFiles.Length + r.m_RoutingTempFiles.Length})=> {r.Server.Description}, EXP:{exp}"); } if (detectDuplicatedRouteJob) { m_Logging.Log(LoggingMode.Warning, $"Find Duplicate route job for Routing {r.m_PatientName} ({r.m_RoutingFiles.Length + r.m_RoutingTempFiles.Length})=> {r.Server.Description}, " + $"Study should be checked in client side to ensure that is exist also..."); } else BackgroundJob.Enqueue(() => RoutingCase.Start($"Routing {r.m_PatientName} ({r.m_RoutingFiles.Length + r.m_RoutingTempFiles.Length})=> {r.Server.Description}", r, null));

but I think the problem is in Hangfire that sometime enqueued one job multiple.
is there any solution or patch.
based on questions:
here and here
Thanks in advance.