|
4 | 4 | import com.zaxxer.hikari.HikariDataSource; |
5 | 5 | import dev.dbos.transact.Constants; |
6 | 6 | import dev.dbos.transact.config.DBOSConfig; |
| 7 | +import dev.dbos.transact.context.DBOSContext; |
| 8 | +import dev.dbos.transact.context.DBOSContextHolder; |
7 | 9 | import dev.dbos.transact.exceptions.*; |
| 10 | +import dev.dbos.transact.json.JSONUtil; |
8 | 11 | import dev.dbos.transact.notifications.GetWorkflowEventContext; |
9 | 12 | import dev.dbos.transact.notifications.NotificationService; |
10 | 13 | import dev.dbos.transact.queue.Queue; |
|
21 | 24 | import javax.sql.DataSource; |
22 | 25 | import java.sql.*; |
23 | 26 | import java.util.*; |
| 27 | +import java.util.function.Supplier; |
24 | 28 |
|
25 | 29 | import static dev.dbos.transact.exceptions.ErrorCode.UNEXPECTED; |
26 | 30 |
|
@@ -302,6 +306,82 @@ public void cancelWorkflow(String workflowId) { |
302 | 306 |
|
303 | 307 | } |
304 | 308 |
|
| 309 | + public void resumeWorkflow(String workflowId) { |
| 310 | + try { |
| 311 | + workflowDAO.resumeWorkflow(workflowId); |
| 312 | + } catch (SQLException s) { |
| 313 | + throw new DBOSException(ErrorCode.RESUME_WORKFLOW_ERROR.getCode(), s.getMessage()) ; |
| 314 | + } |
| 315 | + |
| 316 | + } |
| 317 | + |
| 318 | + |
| 319 | + public <T> T callFunctionAsStep(Supplier<T> fn, String functionName) { |
| 320 | + DBOSContext ctx = DBOSContextHolder.get(); |
| 321 | + |
| 322 | + int nextFuncId = 0 ; |
| 323 | + |
| 324 | + if (ctx != null && ctx.isInWorkflow()) { |
| 325 | + nextFuncId = ctx.getAndIncrementFunctionId() ; |
| 326 | + |
| 327 | + StepResult result = null ; |
| 328 | + |
| 329 | + try (Connection connection = dataSource.getConnection()) { |
| 330 | + result = stepsDAO.checkStepExecutionTxn( |
| 331 | + ctx.getWorkflowId(), nextFuncId, functionName, connection |
| 332 | + ); |
| 333 | + } catch(SQLException e) { |
| 334 | + throw new DBOSException(UNEXPECTED.getCode(), "Function execution failed: " + functionName, e); |
| 335 | + } |
| 336 | + |
| 337 | + if (result != null) { |
| 338 | + return handleExistingResult(result, functionName); |
| 339 | + } |
| 340 | + } |
| 341 | + |
| 342 | + T functionResult; |
| 343 | + try { |
| 344 | + |
| 345 | + try { |
| 346 | + functionResult = fn.get(); |
| 347 | + } catch (Exception e) { |
| 348 | + if (ctx != null && ctx.isInWorkflow()) { |
| 349 | + String jsonError = JSONUtil.serializeError(e); |
| 350 | + StepResult r = new StepResult(ctx.getWorkflowId(), nextFuncId, functionName, null, jsonError); |
| 351 | + stepsDAO.recordStepResultTxn(r); |
| 352 | + } |
| 353 | + throw new DBOSException(UNEXPECTED.getCode(), "Function execution failed: " + functionName, e); |
| 354 | + } |
| 355 | + |
| 356 | + // If we're in a workflow, record the successful result |
| 357 | + if (ctx != null && ctx.isInWorkflow()) { |
| 358 | + String jsonOutput = JSONUtil.serialize(functionResult); |
| 359 | + StepResult o = new StepResult(ctx.getWorkflowId(), nextFuncId, functionName, jsonOutput, null); |
| 360 | + stepsDAO.recordStepResultTxn(o); |
| 361 | + } |
| 362 | + } catch(SQLException sq) { |
| 363 | + throw new DBOSException(UNEXPECTED.getCode(), "Function execution failed: " + functionName, sq); |
| 364 | + } |
| 365 | + |
| 366 | + return functionResult; |
| 367 | + } |
| 368 | + |
| 369 | + @SuppressWarnings("unchecked") |
| 370 | + private <T> T handleExistingResult(StepResult result, String functionName) { |
| 371 | + if (result.getOutput() != null) { |
| 372 | + Object[] resArray = JSONUtil.deserializeToArray(result.getOutput()); |
| 373 | + return resArray == null ? null : (T) resArray[0]; |
| 374 | + } else if (result.getError() != null) { |
| 375 | + Object[] eArray = JSONUtil.deserializeToArray(result.getError()); |
| 376 | + SerializableException se = (SerializableException) eArray[0]; |
| 377 | + throw new DBOSAppException(String.format("Exception of type %s", se.className), se) ; |
| 378 | + } else { |
| 379 | + throw new IllegalStateException( |
| 380 | + String.format("Recorded output and error are both null for %s", functionName) |
| 381 | + ); |
| 382 | + } |
| 383 | + } |
| 384 | + |
305 | 385 | private void createDataSource(String dbName) { |
306 | 386 | HikariConfig hikariConfig = new HikariConfig(); |
307 | 387 |
|
|
0 commit comments