@@ -61,7 +61,7 @@ public async Task<string> AnnounceTask(string taskType, string taskSource, strin
6161 } ;
6262
6363 // get the table name
64- var tableName = GetTaskTable ( ) ;
64+ var tableName = GetTaskTable ( taskKey ) ;
6565
6666 // add the entity
6767 await AddEntityToTable < AzureTableTaskEntity > ( tableName , taskEntity , cancellationToken ) ;
@@ -102,7 +102,7 @@ public async Task UpdateTaskStatus(string taskKey, TaskStatus taskStatus, string
102102 taskEntity . TaskEndDate = DateTimeOffset . UtcNow ;
103103
104104 // get the table name
105- var tableName = GetTaskTable ( ) ;
105+ var tableName = GetTaskTable ( taskKey ) ;
106106
107107 // update the entity
108108 await UpdateEntityInTable < AzureTableTaskEntity > ( tableName , taskEntity ) ;
@@ -115,7 +115,7 @@ public async Task UpdateTaskStatus(string taskKey, TaskStatus taskStatus, string
115115 else if ( taskStatus == TaskStatus . Failed )
116116 {
117117 // store the task in the poisioned table
118- await AddEntityToTable < AzureTableTaskEntity > ( GetFailedTaskTable ( ) , taskEntity ) ;
118+ await AddEntityToTable < AzureTableTaskEntity > ( GetFailedTaskTable ( taskKey ) , taskEntity ) ;
119119
120120 // remove the task from running table
121121 await DeleteEntityByKeys ( GetRunningTaskTable ( ) , taskKey , taskKey ) ;
@@ -133,7 +133,7 @@ public async Task UpdateTaskWorker(string taskId, string taskWorker)
133133 } ;
134134
135135 // get the table name
136- var tableName = GetTaskTable ( ) ;
136+ var tableName = GetTaskTable ( taskId ) ;
137137
138138 // update the entity
139139 await UpdateEntityInTable < AzureTableTaskEntity > ( tableName , taskEntity ) ;
@@ -194,10 +194,7 @@ public async Task MergePendingMessages(DateTimeOffset flushTime, string taskKey,
194194 return ;
195195
196196 // get the table name
197- var tableName = GetTaskMessagesTable ( ) ;
198-
199- // get the table client
200- var tableClient = _tableServiceClient . GetTableClient ( tableName : tableName ) ;
197+ var tableName = GetTaskMessagesTable ( taskKey ) ;
201198
202199 // build the table transaction
203200 var addEntitiesBatch = messages . Select ( m => new TableTransactionAction (
@@ -211,7 +208,7 @@ public async Task MergePendingMessages(DateTimeOffset flushTime, string taskKey,
211208 } ) ) . ToArray ( ) ;
212209
213210 // create the entry
214- await ExecuteTableOperation ( cancellationToken => tableClient . SubmitTransactionAsync ( addEntitiesBatch , cancellationToken ) , cancellationToken => tableClient . CreateIfNotExistsAsync ( cancellationToken ) , CancellationToken . None ) ;
211+ await SubmitTransactionToTable ( tableName , addEntitiesBatch , CancellationToken . None ) ;
215212 }
216213
217214 internal static int GetNextMessageBatchSize ( string taskKey , IReadOnlyList < string > messages )
@@ -248,20 +245,26 @@ private string GetTablePrefix()
248245 private string GetTableName ( string tableName )
249246 => $ "{ GetTablePrefix ( ) } { tableName } ";
250247
251- private string GetTaskTable ( )
252- => GetTableName ( "Tasks" ) ;
248+ private string GetTableName ( string tableName , string taskKey )
249+ => GetTimePartitionedTableName ( _environmentPrefix , tableName , taskKey ) ;
250+
251+ internal static string GetTimePartitionedTableName ( string environmentPrefix , string tableName , string taskKey )
252+ => $ "{ environmentPrefix } { AzureTableTimebasedKeyBuilder . GetReferenceTime ( taskKey ) : yyyyMM} { tableName } ";
253+
254+ private string GetTaskTable ( string taskKey )
255+ => GetTableName ( "Tasks" , taskKey ) ;
253256
254257 private string GetExternalTaskIdLookupTable ( )
255258 => GetTableName ( "TasksExternalIdLookup" ) ;
256259
257260 private string GetRunningTaskTable ( )
258261 => $ "{ _environmentPrefix } TasksRunning";
259262
260- private string GetFailedTaskTable ( )
261- => GetTableName ( "TasksFailed" ) ;
263+ private string GetFailedTaskTable ( string taskKey )
264+ => GetTableName ( "TasksFailed" , taskKey ) ;
262265
263- private string GetTaskMessagesTable ( )
264- => GetTableName ( "Messages" ) ;
266+ private string GetTaskMessagesTable ( string taskKey )
267+ => GetTableName ( "Messages" , taskKey ) ;
265268
266269 private string BuildNextLogEntryTimestamp ( )
267270 {
@@ -278,15 +281,21 @@ private string BuildNextLogEntryTimestamp()
278281 return string . Format ( "{0}-{1}" , Convert . ToInt64 ( diff . TotalSeconds ) , localEntryCounter . ToString ( "00000000" ) ) ;
279282 }
280283
281- private async Task AddEntityToTable < T > ( string tableName , T entity , CancellationToken cancellationToken = default ) where T : ITableEntity
284+ protected virtual async Task AddEntityToTable < T > ( string tableName , T entity , CancellationToken cancellationToken = default ) where T : ITableEntity
282285 => await ExecuteEntityToTableOperation ( tableName , ( TableClient tc , CancellationToken token ) => tc . AddEntityAsync < T > ( entity , token ) , cancellationToken ) ;
283286
284- private async Task UpdateEntityInTable < T > ( string tableName , T entity , CancellationToken cancellationToken = default ) where T : ITableEntity
287+ protected virtual async Task UpdateEntityInTable < T > ( string tableName , T entity , CancellationToken cancellationToken = default ) where T : ITableEntity
285288 => await ExecuteEntityToTableOperation ( tableName , ( TableClient tc , CancellationToken token ) => tc . UpdateEntityAsync < T > ( entity , Azure . ETag . All , cancellationToken : token ) , cancellationToken ) ;
286289
287- private async Task DeleteEntityByKeys ( string tableName , string pKey , string rowKey , CancellationToken cancellationToken = default )
290+ protected virtual async Task DeleteEntityByKeys ( string tableName , string pKey , string rowKey , CancellationToken cancellationToken = default )
288291 => await ExecuteEntityToTableOperation ( tableName , ( TableClient tc , CancellationToken token ) => tc . DeleteEntityAsync ( pKey , rowKey , Azure . ETag . All , token ) , cancellationToken ) ;
289292
293+ protected virtual async Task SubmitTransactionToTable ( string tableName , IReadOnlyList < TableTransactionAction > actions , CancellationToken cancellationToken )
294+ {
295+ var tableClient = _tableServiceClient . GetTableClient ( tableName : tableName ) ;
296+ await ExecuteTableOperation ( token => tableClient . SubmitTransactionAsync ( actions , token ) , token => tableClient . CreateIfNotExistsAsync ( token ) , cancellationToken ) ;
297+ }
298+
290299 private async Task < T [ ] > QueryEntitiyFromTableByPartitionKey < T > ( string tableName , string partitionKey , CancellationToken cancellationToken = default ) where T : class , ITableEntity
291300 {
292301 var result = new List < T > ( ) ;
0 commit comments