Пример #1
0
        public static async Task DeleteContainerIfExists(string containerName)
        {
            var tcs = new TaskCompletionSource <bool>();

            using (await ContainerProvider.Lock.WaitAsync().ConfigureAwait(continueOnCapturedContext: false))
            {
                if (ContainerProvider.ExecutionStates[containerName] == ContainerExecutionState.Ready)
                {
                    ContainerProvider.ExecutionStates[containerName] = ContainerExecutionState.Deleted;
                    tcs.SetResult(result: true);
                }
                else if (ContainerProvider.ExecutionStates[containerName] == ContainerExecutionState.Deleted)
                {
                    return;
                }
                else
                {
                    ContainerProvider.DeletionTasks[containerName] = tcs;
                }
            }

            await tcs.Task.ConfigureAwait(continueOnCapturedContext : false);

            await ContainerProvider
            .DeleteContainer(containerName : containerName)
            .ConfigureAwait(continueOnCapturedContext: false);
        }
Пример #2
0
        private static async Task ExpireContainer(string containerName)
        {
            while (true)
            {
                var timeSinceLastExecution = DateTime.UtcNow - ContainerProvider.Containers[containerName].LastExecutionTime;
                var expirationTimeSpan     = TimeSpan.FromMinutes(ServerlessConfiguration.ContainerLifeInMinutes);

                if (timeSinceLastExecution >= expirationTimeSpan)
                {
                    var delete = false;
                    using (await ContainerProvider.Lock.WaitAsync().ConfigureAwait(continueOnCapturedContext: false))
                    {
                        if (ContainerProvider.ExecutionStates[containerName] == ContainerExecutionState.Ready)
                        {
                            ContainerProvider.ExecutionStates[containerName] = ContainerExecutionState.Deleted;
                            delete = true;
                        }
                        else if (ContainerProvider.ExecutionStates[containerName] == ContainerExecutionState.Deleted)
                        {
                            return;
                        }
                    }

                    if (delete)
                    {
                        await ContainerProvider
                        .DeleteContainer(containerName : containerName)
                        .ConfigureAwait(continueOnCapturedContext: false);

                        return;
                    }
                }

                await Task
                .Delay(delay : expirationTimeSpan - timeSinceLastExecution)
                .ConfigureAwait(continueOnCapturedContext: false);
            }
        }