Skip to content

Commit c12d4da

Browse files
author
Dan Butvinik
committed
delay compression routine by random interval (fixes #21)
1 parent 52ceea2 commit c12d4da

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

ImgBot.Function/Functions.cs

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace ImgBot.Function
1111
{
1212
public static class Functions
1313
{
14+
private static Random s_random = new Random();
15+
1416
[FunctionName("imageupdatemessage")]
1517
public static async Task RunImageUpdateMessage(
1618
[QueueTrigger("imageupdatemessage")]ImageUpdateMessage imageUpdateMessage,
@@ -84,6 +86,12 @@ public static async Task RunInstallationMessage(
8486
RepoOwner = installationMessage.Owner,
8587
};
8688

89+
// getting duplicate work happening at the same exact time in multiple threads
90+
// the most common case is install and add events on the same repo when the queue is asleep
91+
// the queue wakes up and dequeues both messages at the same time in 2 threads
92+
// wait a random amount of seconds so that one will win
93+
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(s_random.Next(0, 20)));
94+
8795
await CompressImages.RunAsync(compressImagesParameters);
8896
}
8997
}

0 commit comments

Comments
 (0)