11/*
2- * Copyright 2020-2024 IEXEC BLOCKCHAIN TECH
2+ * Copyright 2020-2025 IEXEC BLOCKCHAIN TECH
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
4141import java .time .temporal .ChronoUnit ;
4242import java .util .HashMap ;
4343import java .util .Map ;
44- import java .util .function .Predicate ;
4544
4645@ Slf4j
4746@ Service
@@ -77,6 +76,15 @@ public ComputeManagerService(
7776 this .resultService = resultService ;
7877 }
7978
79+ /**
80+ * Download OCI image of the application to execute.
81+ * <p>
82+ * The download fails for a bad task description or if a timeout is reached.
83+ * The timeout is computed by calling {@link #computeImagePullTimeout(TaskDescription)}.
84+ *
85+ * @param taskDescription Task description containing application type and download URI
86+ * @return true if download succeeded, false otherwise
87+ */
8088 public boolean downloadApp (TaskDescription taskDescription ) {
8189 if (taskDescription == null || taskDescription .getAppType () == null ) {
8290 return false ;
@@ -88,8 +96,9 @@ public boolean downloadApp(TaskDescription taskDescription) {
8896 }
8997
9098 final long pullTimeout = computeImagePullTimeout (taskDescription );
91- return dockerService .getClient (taskDescription .getAppUri ())
99+ dockerService .getClient (taskDescription .getAppUri ())
92100 .pullImage (taskDescription .getAppUri (), Duration .of (pullTimeout , ChronoUnit .MINUTES ));
101+ return dockerService .getClient (taskDescription .getAppUri ()).isImagePresent (taskDescription .getAppUri ());
93102 }
94103
95104 /**
@@ -139,35 +148,45 @@ public boolean isAppDownloaded(String imageUri) {
139148 }
140149
141150 /**
142- * Standard tasks: download secrets && decrypt dataset (TODO: rewritte or remove)
143- * <p>
151+ * Execute pre-compute stage for standard and TEE tasks.
152+ * <ul>
153+ * <li>Standard tasks: Nothing is executed, an empty result is returned
154+ * <li>TEE tasks: Call {@link PreComputeService#runTeePreCompute(TaskDescription, WorkerpoolAuthorization)}
155+ * </ul>
144156 * TEE tasks: download pre-compute and post-compute images,
145157 * create SCONE secure session, and run pre-compute container.
146158 *
147- * @param taskDescription
148- * @param workerpoolAuth
149- * @return
159+ * @param taskDescription Description of the task
160+ * @param workerpoolAuth Authorization to contribute delivered by the scheduler for the given task
161+ * @return {@code PreComputeResponse} instance
162+ * @see PreComputeService#runTeePreCompute(TaskDescription, WorkerpoolAuthorization)
150163 */
151- public PreComputeResponse runPreCompute (TaskDescription taskDescription ,
152- WorkerpoolAuthorization workerpoolAuth ) {
164+ public PreComputeResponse runPreCompute (final TaskDescription taskDescription ,
165+ final WorkerpoolAuthorization workerpoolAuth ) {
153166 log .info ("Running pre-compute [chainTaskId:{}, isTee:{}]" ,
154- taskDescription .getChainTaskId (),
155- taskDescription .isTeeTask ());
167+ taskDescription .getChainTaskId (), taskDescription .isTeeTask ());
156168
157169 if (taskDescription .isTeeTask ()) {
158- return preComputeService .runTeePreCompute (taskDescription ,
159- workerpoolAuth );
170+ return preComputeService .runTeePreCompute (taskDescription , workerpoolAuth );
160171 }
161172 return PreComputeResponse .builder ().build ();
162173 }
163174
164- public AppComputeResponse runCompute (TaskDescription taskDescription ,
165- TeeSessionGenerationResponse secureSession ) {
166- String chainTaskId = taskDescription .getChainTaskId ();
167- log .info ("Running compute [chainTaskId:{}, isTee:{}]" , chainTaskId ,
168- taskDescription .isTeeTask ());
175+ /**
176+ * Execute application stage for standard and TEE tasks.
177+ *
178+ * @param taskDescription Description of the task
179+ * @param secureSession Session ID and session storage URL for TEE tasks
180+ * @return {@code AppComputeResponse} instance
181+ * @see AppComputeService#runCompute(TaskDescription, TeeSessionGenerationResponse)
182+ */
183+ public AppComputeResponse runCompute (final TaskDescription taskDescription ,
184+ final TeeSessionGenerationResponse secureSession ) {
185+ final String chainTaskId = taskDescription .getChainTaskId ();
186+ log .info ("Running compute [chainTaskId:{}, isTee:{}]" ,
187+ chainTaskId , taskDescription .isTeeTask ());
169188
170- AppComputeResponse appComputeResponse =
189+ final AppComputeResponse appComputeResponse =
171190 appComputeService .runCompute (taskDescription , secureSession );
172191
173192 if (appComputeResponse .isSuccessful ()) {
@@ -179,55 +198,58 @@ public AppComputeResponse runCompute(TaskDescription taskDescription,
179198
180199 private void writeLogs (String chainTaskId , String filename , String logs ) {
181200 if (!logs .isEmpty ()) {
182- String filePath = workerConfigService .getTaskIexecOutDir (chainTaskId ) + File .separator + filename ;
183- File file = FileHelper .createFileWithContent (filePath , logs );
184- log .info ("Saved logs file [path:{}]" ,
185- file .getAbsolutePath ());
201+ final String filePath = workerConfigService .getTaskIexecOutDir (chainTaskId ) + File .separator + filename ;
202+ final File file = FileHelper .createFileWithContent (filePath , logs );
203+ log .info ("Saved logs file [path:{}]" , file .getAbsolutePath ());
186204 //TODO Make sure file is properly written
187205 }
188206 }
189207
190- /*
191- * - Copy computed.json file produced by the compute stage to /output
192- * - Zip iexec_out folder
193- * For TEE tasks, worker-tee-post-compute will do those two steps since
194- * all files in are protected.
208+ /**
209+ * Execute post-compute stage for standard and TEE tasks.
210+ * <p>
211+ * This method calls methods from {@code PostComputeService} depending on the Task type.
195212 *
196- * - Save stdout file
213+ * @param taskDescription Description of the task
214+ * @param secureSession Session ID and session storage URL for TEE tasks
215+ * @return {@code PostComputeResponse} instance
216+ * @see PostComputeService#runStandardPostCompute(TaskDescription)
217+ * @see PostComputeService#runTeePostCompute(TaskDescription, TeeSessionGenerationResponse)
197218 */
198- public PostComputeResponse runPostCompute (TaskDescription taskDescription ,
199- TeeSessionGenerationResponse secureSession ) {
200- String chainTaskId = taskDescription .getChainTaskId ();
219+ public PostComputeResponse runPostCompute (final TaskDescription taskDescription ,
220+ final TeeSessionGenerationResponse secureSession ) {
221+ final String chainTaskId = taskDescription .getChainTaskId ();
201222 log .info ("Running post-compute [chainTaskId:{}, isTee:{}]" ,
202223 chainTaskId , taskDescription .isTeeTask ());
203- PostComputeResponse postComputeResponse = PostComputeResponse .builder ()
204- .exitCause (ReplicateStatusCause .POST_COMPUTE_FAILED_UNKNOWN_ISSUE )
205- .build ();
206224
225+ final PostComputeResponse postComputeResponse ;
207226 if (!taskDescription .isTeeTask ()) {
208227 postComputeResponse = postComputeService .runStandardPostCompute (taskDescription );
209228 } else if (secureSession != null ) {
210229 postComputeResponse = postComputeService .runTeePostCompute (taskDescription , secureSession );
230+ } else {
231+ postComputeResponse = PostComputeResponse .builder ()
232+ .exitCause (ReplicateStatusCause .POST_COMPUTE_FAILED_UNKNOWN_ISSUE )
233+ .build ();
211234 }
212235 if (!postComputeResponse .isSuccessful ()) {
213236 return postComputeResponse ;
214237 }
215- ComputedFile computedFile = resultService .readComputedFile (chainTaskId );
238+ final ComputedFile computedFile = resultService .readComputedFile (chainTaskId );
216239 if (computedFile == null ) {
217240 postComputeResponse .setExitCause (ReplicateStatusCause .POST_COMPUTE_COMPUTED_FILE_NOT_FOUND );
218241 return postComputeResponse ;
219242 }
220- String resultDigest = resultService .computeResultDigest (computedFile );
243+ final String resultDigest = resultService .computeResultDigest (computedFile );
221244 if (resultDigest .isEmpty ()) {
222245 postComputeResponse .setExitCause (ReplicateStatusCause .POST_COMPUTE_RESULT_DIGEST_COMPUTATION_FAILED );
223246 }
224- resultService .saveResultInfo (chainTaskId , taskDescription , computedFile );
247+ resultService .saveResultInfo (taskDescription , computedFile );
225248 return postComputeResponse ;
226249 }
227250
228- public boolean abort (String chainTaskId ) {
229- Predicate <String > containsChainTaskId = name -> name .contains (chainTaskId );
230- long remaining = dockerService .stopRunningContainersWithNamePredicate (containsChainTaskId );
251+ public boolean abort (final String chainTaskId ) {
252+ final long remaining = dockerService .stopRunningContainersWithNameContaining (chainTaskId );
231253 log .info ("Stopped task containers [chainTaskId:{}, remaining:{}]" , chainTaskId , remaining );
232254 return remaining == 0L ;
233255 }
0 commit comments