import { Construct } from '@aws-cdk/core' ; import { Choice , Succeed , Map , StateMachine , Condition , TaskInput } from "@aws-cdk/aws-stepfunctions" ; import { LambdaInvoke } from "@aws-cdk/aws-stepfunctions-tasks" ; import { WebCrawlerSteps } from './web-crawler-step-lambdas' ; import { WEB_CRAWLER_STATE_MACHINE_NAME } from './constants' ; const { booleanEquals } = Condition ; export interface WebCrawlerStateMachineProps { steps : WebCrawlerSteps ; } /**
 * Construct to create our web crawler state machine
 */ export default class WebCrawlerStateMachine extends Construct { constructor ( scope , id , props ) { super ( scope , id ) ; const crawlPageAndQueueUrls = new LambdaInvoke ( this , 'CrawlPageAndQueueUrls' , { lambdaFunction : props . steps . crawlPageAndQueueUrls , } ) ; const readQueuedUrls = new LambdaInvoke ( this , 'ReadQueuedUrls' , { lambdaFunction : props . steps . readQueuedUrls , payload : TaskInput . fromContextAt ( '$$.Execution.Input' ) , } ) ; const completeCrawl = new LambdaInvoke ( this , 'CompleteCrawl' , { lambdaFunction : props . steps . completeCrawl , } ) ; const continueExecution = new LambdaInvoke ( this , 'ContinueExecution' , { lambdaFunction : props . steps . continueExecution , } ) ; const definition = readQueuedUrls . addCatch ( readQueuedUrls ) . next ( new Choice ( this , 'QueueContainsUrls?' ) . when ( booleanEquals ( '$.Payload.queueIsNonEmpty' , true ) , new Choice ( this , 'VisitedUrlsExceedsThreshold?' ) . when ( booleanEquals ( '$.Payload.totalUrlCountExceedsThreshold' , true ) , continueExecution . next ( new Succeed ( this , 'ContinuingInAnotherExecution' ) ) ) . otherwise ( new Map ( this , 'ForEachQueuedUrl' , { itemsPath : '$.Payload.queuedPaths' } ) . iterator ( crawlPageAndQueueUrls ) . addCatch ( readQueuedUrls ) . next ( readQueuedUrls ) ) ) . otherwise ( completeCrawl . next ( new Succeed ( this , 'Done' ) ) ) ) ; const webCrawlerStateMachine = new StateMachine ( this , `${ id }-StateMachine` , { stateMachineName : WEB_CRAWLER_STATE_MACHINE_NAME , definition , } ) ; webCrawlerStateMachine . grantStartExecution ( props . steps . startCrawl ) ; props . steps . startCrawl . addEnvironment ( 'WEB_CRAWLER_STATE_MACHINE_ARN' , webCrawlerStateMachine . stateMachineArn ) ; } }