public async Task RenewHubCacheEntries()

in DeviceBridge/Providers/StorageProvider.cs [282:326]


        public async Task RenewHubCacheEntries(Logger logger, List<string> deviceIds)
        {
            try
            {
                logger.Info("Renewing Hub cache entries for {count} devices", deviceIds.Count);

                // Add device Ids to a Data Table that we'll bulk copy to the DB.
                var dt = new DataTable();
                dt.Columns.Add("DeviceId");

                foreach (var deviceId in deviceIds)
                {
                    var row = dt.NewRow();
                    row["DeviceId"] = deviceId;
                    dt.Rows.Add(row);
                }

                using SqlConnection connection = new SqlConnection(_connectionString);
                using SqlCommand command = new SqlCommand(string.Empty, connection);
                await connection.OpenAsync();

                // Create a target temp table.
                command.CommandText = "CREATE TABLE #CacheEntriesToRenewTmpTable(DeviceId VARCHAR(255) NOT NULL PRIMARY KEY)";
                await command.ExecuteNonQueryAsync();

                // Bulk copy the device Ids to renew to the temp table, 1000 records at a time.
                using SqlBulkCopy bulkcopy = new SqlBulkCopy(connection);
                bulkcopy.BulkCopyTimeout = BulkCopyBatchTimeout;
                bulkcopy.BatchSize = BulkCopyBatchSize;
                bulkcopy.DestinationTableName = "#CacheEntriesToRenewTmpTable";
                await bulkcopy.WriteToServerAsync(dt);

                // Renew the Hub cache timestamp for every device Id in the temp table.
                command.CommandTimeout = 300; // The operation should take no longer than 5 minutes
                command.CommandText = @"UPDATE HubCache SET RenewedAt = GETUTCDATE()
                                        FROM HubCache
                                        INNER JOIN #CacheEntriesToRenewTmpTable Temp ON (Temp.DeviceId = HubCache.DeviceId)
                                        DROP TABLE #CacheEntriesToRenewTmpTable";
                await command.ExecuteNonQueryAsync();
            }
            catch (Exception e)
            {
                throw TranslateSqlException(e);
            }
        }