<commit_before>﻿using System;
using System.Threading;
using System.Threading.Tasks;
using Foundatio.Extensions;

namespace Foundatio.Lock {
    public interface ILockProvider : IDisposable {
        Task<ILock> AcquireAsync(string name, TimeSpan? lockTimeout = null, CancellationToken cancellationToken = default(CancellationToken));
        Task<bool> IsLockedAsync(string name);
        Task ReleaseAsync(string name);
        Task RenewAsync(string name, TimeSpan? lockExtension = null);
    }

    public interface ILock : IDisposable {
        Task RenewAsync(TimeSpan? lockExtension = null);
    }

    public static class LockProviderExtensions {
        public static Task<ILock> AcquireAsync(this ILockProvider provider, string name, TimeSpan? lockTimeout = null, TimeSpan? acquireTimeout = null) {
            return provider.AcquireAsync(name, lockTimeout, acquireTimeout.ToCancellationToken(TimeSpan.FromMinutes(1)));
        }

        public static async Task TryUsingAsync(this ILockProvider locker, string name, Func<CancellationToken, Task> work, TimeSpan? lockTimeout = null, CancellationToken cancellationToken = default(CancellationToken)) {
            using (var l = await locker.AcquireAsync(name, lockTimeout, cancellationToken).AnyContext())
                if (l != null)
                    await work(cancellationToken).AnyContext();
        }

        public static async Task TryUsingAsync(this ILockProvider locker, string name, Func<Task> work, TimeSpan? lockTimeout = null, CancellationToken cancellationToken = default(CancellationToken)) {
            using (var l = await locker.AcquireAsync(name, lockTimeout, cancellationToken).AnyContext())
                if (l != null)
                    await work().AnyContext();
        }

        public static async Task TryUsingAsync(this ILockProvider locker, string name, Func<CancellationToken, Task> work, TimeSpan? lockTimeout = null, TimeSpan? acquireTimeout = null) {
            var cancellationToken = acquireTimeout?.ToCancellationToken() ?? default(CancellationToken);
            using (var l = await locker.AcquireAsync(name, lockTimeout, cancellationToken).AnyContext())
                if (l != null)
                    await work(cancellationToken).AnyContext();
        }

        public static async Task TryUsingAsync(this ILockProvider locker, string name, Func<Task> work, TimeSpan? lockTimeout = null, TimeSpan? acquireTimeout = null) {
            var cancellationToken = acquireTimeout?.ToCancellationToken() ?? default(CancellationToken);
            using (var l = await locker.AcquireAsync(name, lockTimeout, cancellationToken).AnyContext())
                if (l != null)
                    await work().AnyContext();
        }
    }
}<commit_msg>Adding bool return to TryUsingLock so we can know if it succeeded.<commit_after>using System;
using System.Threading;
using System.Threading.Tasks;
using Foundatio.Extensions;

namespace Foundatio.Lock {
    public interface ILockProvider : IDisposable {
        Task<ILock> AcquireAsync(string name, TimeSpan? lockTimeout = null, CancellationToken cancellationToken = default(CancellationToken));
        Task<bool> IsLockedAsync(string name);
        Task ReleaseAsync(string name);
        Task RenewAsync(string name, TimeSpan? lockExtension = null);
    }

    public interface ILock : IDisposable {
        Task RenewAsync(TimeSpan? lockExtension = null);
    }

    public static class LockProviderExtensions {
        public static Task<ILock> AcquireAsync(this ILockProvider provider, string name, TimeSpan? lockTimeout = null, TimeSpan? acquireTimeout = null) {
            return provider.AcquireAsync(name, lockTimeout, acquireTimeout.ToCancellationToken(TimeSpan.FromMinutes(1)));
        }

        public static async Task<bool> TryUsingAsync(this ILockProvider locker, string name, Func<CancellationToken, Task> work, TimeSpan? lockTimeout = null, CancellationToken cancellationToken = default(CancellationToken)) {
            using (var l = await locker.AcquireAsync(name, lockTimeout, cancellationToken).AnyContext()) {
                if (l != null) {
                    await work(cancellationToken).AnyContext();
                    return true;
                }
            }

            return false;
        }

        public static async Task<bool> TryUsingAsync(this ILockProvider locker, string name, Func<Task> work, TimeSpan? lockTimeout = null, CancellationToken cancellationToken = default(CancellationToken)) {
            using (var l = await locker.AcquireAsync(name, lockTimeout, cancellationToken).AnyContext()) {
                if (l != null) {
                    await work().AnyContext();
                    return true;
                }
            }

            return false;
        }

        public static async Task<bool> TryUsingAsync(this ILockProvider locker, string name, Func<CancellationToken, Task> work, TimeSpan? lockTimeout = null, TimeSpan? acquireTimeout = null) {
            var cancellationToken = acquireTimeout?.ToCancellationToken() ?? default(CancellationToken);
            using (var l = await locker.AcquireAsync(name, lockTimeout, cancellationToken).AnyContext()) {
                if (l != null) {
                    await work(cancellationToken).AnyContext();
                    return true;
                }
            }

            return false;
        }

        public static async Task<bool> TryUsingAsync(this ILockProvider locker, string name, Func<Task> work, TimeSpan? lockTimeout = null, TimeSpan? acquireTimeout = null) {
            var cancellationToken = acquireTimeout?.ToCancellationToken() ?? default(CancellationToken);
            using (var l = await locker.AcquireAsync(name, lockTimeout, cancellationToken).AnyContext()) {
                if (l != null) {
                    await work().AnyContext();
                    return true;
                }
            }

            return false;
        }
    }
}
{'fraction_non_alphanumeric': 0.08665105386416862, 'fraction_numerical': 0.00033456005352960856, 'mean_word_length': 2.825335892514395, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4308220', 'n_tokens_mistral': 1448, 'n_tokens_neox': 1455, 'n_words': 409}