11import { IExecDataProtectorDeserializer } from '@iexec/dataprotector-deserializer' ;
22import { promises as fs } from 'fs' ;
3- import path from 'node:path' ;
43import { decryptContent , downloadEncryptedContent } from './decryptContent.js' ;
54import sendTelegram from './telegramService.js' ;
65import {
@@ -34,7 +33,7 @@ async function processProtectedData(
3433 let protectedData ;
3534 try {
3635 const deserializerConfig = datasetFilename
37- ? { protectedDataPath : path . join ( IEXEC_IN , datasetFilename ) }
36+ ? { protectedDataPath : ` ${ IEXEC_IN } / ${ datasetFilename } ` }
3837 : { } ;
3938
4039 const deserializer = new IExecDataProtectorDeserializer ( deserializerConfig ) ;
@@ -66,29 +65,30 @@ async function processProtectedData(
6665 senderName : requesterSecret . senderName ,
6766 } ) ;
6867
69- // Write individual result file
70- const resultFileName = index > 0 ? `${ datasetFilename } .txt` : 'result.txt' ;
71- await writeTaskOutput (
72- path . join ( IEXEC_OUT , resultFileName ) ,
73- JSON . stringify ( response , null , 2 )
74- ) ;
68+ // Write individual result file only for single processing
69+ if ( index === 0 ) {
70+ await writeTaskOutput (
71+ `${ IEXEC_OUT } /result.txt` ,
72+ JSON . stringify ( response , null , 2 )
73+ ) ;
74+ }
7575
76- return { index, response, resultFileName } ;
76+ return { index, response } ;
7777}
7878
7979async function start ( ) {
8080 const {
8181 IEXEC_OUT ,
82- IEXEC_IN ,
8382 IEXEC_APP_DEVELOPER_SECRET ,
8483 IEXEC_REQUESTER_SECRET_1 ,
84+ IEXEC_IN ,
8585 IEXEC_BULK_SLICE_SIZE ,
8686 } = process . env ;
8787
8888 // Check worker env
8989 const workerEnv = validateWorkerEnv ( { IEXEC_OUT } ) ;
9090
91- // Parse the app developer secret
91+ // Parse the app developer secret environment variable
9292 let appDeveloperSecret ;
9393 try {
9494 appDeveloperSecret = JSON . parse ( IEXEC_APP_DEVELOPER_SECRET ) ;
@@ -97,7 +97,7 @@ async function start() {
9797 }
9898 appDeveloperSecret = validateAppSecret ( appDeveloperSecret ) ;
9999
100- // Parse the requester secret
100+ // Parse the requester secret environment variable
101101 let requesterSecret ;
102102 try {
103103 requesterSecret = IEXEC_REQUESTER_SECRET_1
@@ -109,23 +109,36 @@ async function start() {
109109 requesterSecret = validateRequesterSecret ( requesterSecret ) ;
110110
111111 const bulkSize = parseInt ( IEXEC_BULK_SLICE_SIZE ) || 0 ;
112-
113112 const results = [ ] ;
114113
115114 if ( bulkSize > 0 ) {
116115 // Process multiple protected data
117116 for ( let i = 1 ; i <= bulkSize ; i ++ ) {
118117 const datasetFilename = process . env [ `IEXEC_DATASET_${ i } _FILENAME` ] ;
119118
120- const result = await processProtectedData ( i , {
121- IEXEC_IN ,
122- IEXEC_OUT : workerEnv . IEXEC_OUT ,
123- appDeveloperSecret,
124- requesterSecret,
125- datasetFilename,
126- } ) ;
127-
128- results . push ( result ) ;
119+ try {
120+ const result = await processProtectedData ( i , {
121+ IEXEC_IN ,
122+ IEXEC_OUT : workerEnv . IEXEC_OUT ,
123+ appDeveloperSecret,
124+ requesterSecret,
125+ datasetFilename,
126+ } ) ;
127+
128+ results . push ( result ) ;
129+ } catch ( error ) {
130+ // Create an error result for this dataset
131+ results . push ( {
132+ index : i ,
133+ resultFileName : datasetFilename
134+ ? `${ datasetFilename } .txt`
135+ : `dataset-${ i } .txt` ,
136+ response : {
137+ status : 500 ,
138+ message : `Failed to process dataset ${ i } : ${ error . message } ` ,
139+ } ,
140+ } ) ;
141+ }
129142 }
130143 } else {
131144 // Process single protected data
@@ -139,20 +152,45 @@ async function start() {
139152 results . push ( result ) ;
140153 }
141154
142- // Write computed.json with all results
143- const computedOutput = {
144- 'deterministic-output-path' : workerEnv . IEXEC_OUT ,
145- 'bulk-results' : results . map ( ( r ) => ( {
146- index : r . index ,
147- file : r . resultFileName ,
148- status : r . response . status === 200 ? 'success' : 'error' ,
149- } ) ) ,
150- 'total-processed' : results . length ,
151- } ;
155+ // Generate computed.json - same format for both single and bulk
156+
157+ // Create result.txt for bulk processing (similar to single processing)
158+ if ( bulkSize > 0 ) {
159+ const successCount = results . filter (
160+ ( r ) => r . response . status === 200
161+ ) . length ;
162+ const errorCount = results . filter ( ( r ) => r . response . status !== 200 ) . length ;
163+
164+ const bulkResult = {
165+ message : `Bulk processing completed: ${ successCount } successful, ${ errorCount } failed` ,
166+ status : 200 ,
167+ 'total-processed' : results . length ,
168+ 'success-count' : successCount ,
169+ 'error-count' : errorCount ,
170+ 'dataset-results' : results . map ( ( r ) => ( {
171+ index : r . index ,
172+ dataset :
173+ process . env [ `IEXEC_DATASET_${ r . index } _FILENAME` ] ||
174+ `dataset-${ r . index } ` ,
175+ response : r . response ,
176+ } ) ) ,
177+ } ;
178+
179+ await writeTaskOutput (
180+ `${ workerEnv . IEXEC_OUT } /result.txt` ,
181+ JSON . stringify ( bulkResult , null , 2 )
182+ ) ;
183+ }
152184
153185 await writeTaskOutput (
154- path . join ( workerEnv . IEXEC_OUT , 'computed.json' ) ,
155- JSON . stringify ( computedOutput , null , 2 )
186+ `${ workerEnv . IEXEC_OUT } /computed.json` ,
187+ JSON . stringify (
188+ {
189+ 'deterministic-output-path' : `${ workerEnv . IEXEC_OUT } /result.txt` ,
190+ } ,
191+ null ,
192+ 2
193+ )
156194 ) ;
157195}
158196
0 commit comments