Ejemplo n.º 1
0
    /// <summary>
    /// Deletes 10 oldest instances if the number of instances is more than 14.
    /// This is to clean up the stale instances in case of instance cleanup code may not get triggered.
    /// </summary>
    private async Task DeleteStaleInstancesAsync()
    {
        InstanceAdminClient instanceAdminClient = InstanceAdminClient.Create();
        var listInstancesRequest = new ListInstancesRequest
        {
            Filter = "name:my-instance-processing-units- OR name:my-instance-multi-region-",
            ParentAsProjectName = ProjectName.FromProject(ProjectId)
        };
        var instances = instanceAdminClient.ListInstances(listInstancesRequest);

        if (instances.Count() < 15)
        {
            return;
        }

        var instancesToDelete = instances
                                .OrderBy(db => long.TryParse(
                                             db.InstanceName.InstanceId.Replace("my-instance-processing-units-", "")
                                             .Replace("my-instance-multi-region-", ""),
                                             out long creationDate) ? creationDate : long.MaxValue)
                                .Take(10);

        // Delete the instances.
        foreach (var instance in instancesToDelete)
        {
            try
            {
                await instanceAdminClient.DeleteInstanceAsync(instance.InstanceName);
            }
            catch (Exception) { }
        }
    }
Ejemplo n.º 2
0
    /// <summary>
    /// Deletes 10 oldest instances if the number of instances is more than 14.
    /// This is to clean up the stale instances in case of instance cleanup code may not get triggered.
    /// </summary>
    private async Task DeleteStaleInstancesAsync()
    {
        InstanceAdminClient instanceAdminClient = InstanceAdminClient.Create();
        var listInstancesRequest = new ListInstancesRequest
        {
            Filter = "name:my-instance-processing-units- OR name:my-instance-multi-region-",
            ParentAsProjectName = ProjectName.FromProject(ProjectId)
        };
        var instances = instanceAdminClient.ListInstances(listInstancesRequest);

        if (instances.Count() < 15)
        {
            return;
        }

        long fiveHoursAgo = DateTimeOffset.UtcNow.AddHours(-5).ToUnixTimeMilliseconds();

        var instancesToDelete = instances
                                .Select(db => (db, CreationUnixTimeMilliseconds(db)))
                                .Where(pair => pair.Item2 < fiveHoursAgo)
                                .Select(pair => pair.db);

        // Delete the instances.
        foreach (var instance in instancesToDelete)
        {
            try
            {
                await instanceAdminClient.DeleteInstanceAsync(instance.InstanceName);
            }
            catch (Exception) { }
        }
Ejemplo n.º 3
0
        /// <summary>Snippet for DeleteInstanceAsync</summary>
        public async Task DeleteInstanceAsync()
        {
            // Snippet: DeleteInstanceAsync(InstanceName,CallSettings)
            // Additional: DeleteInstanceAsync(InstanceName,CancellationToken)
            // Create client
            InstanceAdminClient instanceAdminClient = await InstanceAdminClient.CreateAsync();

            // Initialize request argument(s)
            InstanceName name = new InstanceName("[PROJECT]", "[INSTANCE]");
            // Make the request
            await instanceAdminClient.DeleteInstanceAsync(name);

            // End snippet
        }
Ejemplo n.º 4
0
        public async Task DeleteInstanceAsync_RequestObject()
        {
            // Snippet: DeleteInstanceAsync(DeleteInstanceRequest,CallSettings)
            // Create client
            InstanceAdminClient instanceAdminClient = await InstanceAdminClient.CreateAsync();

            // Initialize request argument(s)
            DeleteInstanceRequest request = new DeleteInstanceRequest
            {
                InstanceName = new InstanceName("[PROJECT]", "[INSTANCE]"),
            };
            // Make the request
            await instanceAdminClient.DeleteInstanceAsync(request);

            // End snippet
        }