/// <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) { } } }
/// <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) { } }
/// <summary>Snippet for ListInstances</summary> public void ListInstances_RequestObject() { // Snippet: ListInstances(ListInstancesRequest,CallSettings) // Create client InstanceAdminClient instanceAdminClient = InstanceAdminClient.Create(); // Initialize request argument(s) ListInstancesRequest request = new ListInstancesRequest { ParentAsProjectName = new ProjectName("[PROJECT]"), }; // Make the request PagedEnumerable <ListInstancesResponse, Instance> response = instanceAdminClient.ListInstances(request); // Iterate over all response items, lazily performing RPCs as required foreach (Instance item in response) { // Do something with each item Console.WriteLine(item); } // Or iterate over pages (of server-defined size), performing one RPC per page foreach (ListInstancesResponse page in response.AsRawResponses()) { // Do something with each page of items Console.WriteLine("A page of results:"); foreach (Instance item in page) { Console.WriteLine(item); } } // Or retrieve a single page of known size (unless it's the final page), performing as many RPCs as required int pageSize = 10; Page <Instance> singlePage = response.ReadPage(pageSize); // Do something with the page of items Console.WriteLine($"A page of {pageSize} results (unless it's the final page):"); foreach (Instance item in singlePage) { Console.WriteLine(item); } // Store the pageToken, for when the next page is required. string nextPageToken = singlePage.NextPageToken; // End snippet }