0

We switched to WebJobs with our background tasks that are starting to work when a new item lands on an Azure Queue. Now we have some weird issues that he seems to have problems accessing Redis RedLock and Storage that I can't explain.

Now the biggest issue we have is RedLock. We are using RedLock.Net for distributed locking. Now this works all fine in our production web application and it also worked on the background workers we had but as soon as we switched to WebJobs he basically failed to aquire the lock. To back this up with some code...we are locking like this:

        using (var redisLock = await _redLockConnection.RedisLockFactory.CreateAsync(resource, UserLockExpiryTime, UserLockWaitTime, UserLockRetryTime))
        {
            // make sure we got the lock
            if (redisLock.IsAcquired)
            {
                // execute code...
            }
            else
            {
                throw new CouldNotAcquireRedLockException();
            }
        }

The problem here is, IsAcquired is always false within a Webjob and I have no clue why!?

The second thing that maybe relates to this problem is deleting a blob file in azure storage that fails with a 404 only within a WebJob.

var file = _blobContainer.GetBlockBlobReference("file.txt");
file?.Delete();

This will fail with a 404 Not found exception within the WebJob.

Is there anything I missed setting up the webjob? Could it be an access problem for write operations? Would be glad for any help!

Hypi
  • 127
  • 1
  • 8
  • ``deleting a blob file in azure storage that fails with a 404 only within a WebJob`` Please check if Blob is existing via Azure portal or Azure storage explorer, or call [Exists method](https://learn.microsoft.com/en-us/dotnet/api/microsoft.windowsazure.storage.blob.cloudblob.exists?view=azurestorage-8.1.3#Microsoft_WindowsAzure_Storage_Blob_CloudBlob_Exists_Microsoft_WindowsAzure_Storage_Blob_BlobRequestOptions_Microsoft_WindowsAzure_Storage_OperationContext_) to check existence of the blob. – Fei Han Oct 17 '17 at 08:09

1 Answers1

0

IsAcquired is always false within a Webjob

I do a test with the following code using RedLock.net in an Azure WebJob, I can acquire lock on resource if the lock is available.

public static void ProcessQueueMessage([QueueTrigger("mymes")] string message, TextWriter log)
{
    var azureEndPoint = new RedisLockEndPoint
    {
        EndPoint = new DnsEndPoint("{YOUR_CACHE}.redis.cache.windows.net", 6380),
        Password = "YOUR_ACCESS_KEY",
        Ssl = true
    };

    var eps = new[] { azureEndPoint };


    var rlf = new RedisLockFactory(eps);

    var resource = "https://{storageaccount}.blob.core.windows.net/{containername}/test.txt";
    var expiry = TimeSpan.FromSeconds(50);
    var wait = TimeSpan.FromSeconds(10);
    var retry = TimeSpan.FromSeconds(1);

    using (var redisLock = rlf.Create(resource, expiry, wait, retry))
    {
        Console.WriteLine("Lock acquired: " + redisLock.IsAcquired);
    }

    log.WriteLine(message);
}

Result of test: enter image description here

deleting a blob file in azure storage that fails with a 404

As I mentioned in comment, Please check if that Blob is existing via Azure portal or Azure storage explorer, or call Exists method to check existence of the blob before you delete it.

Fei Han
  • 26,415
  • 1
  • 30
  • 41