@@ -14,45 +14,45 @@ export const processOldDataJob = async (
14
14
) : Promise < void > => {
15
15
const store = new DbStore ( log , dbConn )
16
16
const repo = new IntegrationDataRepository ( store , log )
17
- const service = new IntegrationDataService (
18
- redis ,
19
- streamWorkerEmitter ,
20
- dataSinkWorkerEmitter ,
21
- store ,
22
- log ,
23
- )
24
-
25
- const loadNextBatch = async ( ) : Promise < string [ ] > => {
26
- return await repo . transactionally ( async ( txRepo ) => {
27
- const dataIds = await txRepo . getOldDataToProcess ( 5 )
28
- await txRepo . touchUpdatedAt ( dataIds )
29
- return dataIds
30
- } )
31
- }
32
-
33
- // load 5 oldest apiData and try process them
34
- let dataToProcess = await loadNextBatch ( )
35
17
36
18
let successCount = 0
37
19
let errorCount = 0
38
20
39
- while ( dataToProcess . length > 0 ) {
40
- for ( const dataId of dataToProcess ) {
41
- try {
42
- const result = await service . processData ( dataId )
43
- if ( result ) {
44
- successCount ++
45
- } else {
21
+ while ( true ) {
22
+ const processedSomething = await repo . transactionally ( async ( txRepo ) => {
23
+ const dataIds = await txRepo . getOldDataToProcess ( 5 )
24
+ await txRepo . touchUpdatedAt ( dataIds )
25
+
26
+ const txService = new IntegrationDataService (
27
+ redis ,
28
+ streamWorkerEmitter ,
29
+ dataSinkWorkerEmitter ,
30
+ store ,
31
+ log ,
32
+ txRepo ,
33
+ )
34
+
35
+ for ( const dataId of dataIds ) {
36
+ try {
37
+ const result = await txService . processData ( dataId )
38
+ if ( result ) {
39
+ successCount ++
40
+ } else {
41
+ errorCount ++
42
+ }
43
+ } catch ( err ) {
44
+ log . error ( err , 'Failed to process data!' )
46
45
errorCount ++
47
46
}
48
- } catch ( err ) {
49
- log . error ( err , 'Failed to process data!' )
50
- errorCount ++
51
47
}
52
- }
53
48
54
- log . info ( `Processed ${ successCount } old data successfully and ${ errorCount } with errors.` )
49
+ log . info ( `Processed ${ successCount } old data successfully and ${ errorCount } with errors.` )
55
50
56
- dataToProcess = await loadNextBatch ( )
51
+ return dataIds . length > 0
52
+ } )
53
+
54
+ if ( ! processedSomething ) {
55
+ break
56
+ }
57
57
}
58
58
}
0 commit comments