@@ -267,7 +267,14 @@ struct ExampleCommand: ParsableCommand {
267267 } else if state == . stopping {
268268 return false
269269 }
270- Thread . sleep ( forTimeInterval: 4 )
270+
271+ // Wait four seconds before trying again.
272+
273+ do {
274+ try await Task . sleep ( for: . seconds( 4 ) )
275+ } catch {
276+ print ( " *** Error pausing the task. " )
277+ }
271278 }
272279 }
273280 // snippet-end:[swift.glue.getCrawlerState]
@@ -459,7 +466,6 @@ struct ExampleCommand: ParsableCommand {
459466 }
460467 // snippet-end:[swift.glue.GetTables]
461468
462- // snippet-start:[swift.glue.BatchDeleteTable]
463469 // snippet-start:[swift.glue.DeleteDatabase]
464470 /// Delete the specified database.
465471 ///
@@ -486,34 +492,56 @@ struct ExampleCommand: ParsableCommand {
486492 tableNames. append ( name)
487493 }
488494
489- // Delete the tables.
490-
491- do {
492- _ = try await glueClient. batchDeleteTable (
493- input: BatchDeleteTableInput (
494- databaseName: databaseName,
495- tablesToDelete: tableNames
495+ // Delete the tables. If there's only one table, use
496+ // `deleteTable()`, otherwise, use `batchDeleteTable()`. You can
497+ // use `batchDeleteTable()` for a single table, but this
498+ // demonstrates the use of `deleteTable()`.
499+
500+ if tableNames. count == 1 {
501+ // snippet-start:[swift.glue.DeleteTable]
502+ do {
503+ print ( " Deleting table... " )
504+ _ = try await glueClient. deleteTable (
505+ input: DeleteTableInput (
506+ databaseName: databaseName,
507+ name: tableNames [ 0 ]
508+ )
496509 )
497- )
498- } catch {
499- print ( " *** Unable to delete the tables. " )
510+ } catch {
511+ print ( " *** Unable to delete the table. " )
512+ }
513+ // snippet-end:[swift.glue.DeleteTable]
514+ } else {
515+ // snippet-start:[swift.glue.BatchDeleteTable]
516+ do {
517+ print ( " Deleting tables... " )
518+ _ = try await glueClient. batchDeleteTable (
519+ input: BatchDeleteTableInput (
520+ databaseName: databaseName,
521+ tablesToDelete: tableNames
522+ )
523+ )
524+ } catch {
525+ print ( " *** Unable to delete the tables. " )
526+ }
527+ // snippet-end:[swift.glue.BatchDeleteTable]
500528 }
501- return true
502529 }
503530
504531 // Delete the database itself.
505532
506533 do {
534+ print ( " Deleting the database itself... " )
507535 _ = try await glueClient. deleteDatabase (
508536 input: DeleteDatabaseInput ( name: databaseName)
509537 )
510538 } catch {
511539 print ( " *** Unable to delete the database. " )
540+ return false
512541 }
513542 return true
514543 }
515544 // snippet-end:[swift.glue.DeleteDatabase]
516- // snippet-end:[swift.glue.BatchDeleteTable]
517545
518546 // snippet-start:[swift.glue.StartJobRun]
519547 /// Start an AWS Glue job run.
@@ -554,6 +582,43 @@ struct ExampleCommand: ParsableCommand {
554582 }
555583 // snippet-end:[swift.glue.StartJobRun]
556584
585+ // snippet-start:[swift.glue.GetJobRuns]
586+ /// Return a list of the job runs for the specified job.
587+ ///
588+ /// - Parameters:
589+ /// - glueClient: The AWS Glue client to use.
590+ /// - jobName: The name of the job for which to return its job runs.
591+ /// - maxResults: The maximum number of job runs to return (default:
592+ /// 1000).
593+ ///
594+ /// - Returns: An array of `GlueClientTypes.JobRun` objects describing
595+ /// each job run.
596+ func getJobRuns( glueClient: GlueClient , name jobName: String , maxResults: Int ? = nil ) async -> [ GlueClientTypes . JobRun ] {
597+ do {
598+ let output = try await glueClient. getJobRuns (
599+ input: GetJobRunsInput (
600+ jobName: jobName,
601+ maxResults: maxResults
602+ )
603+ )
604+
605+ guard let jobRuns = output. jobRuns else {
606+ print ( " *** No job runs found. " )
607+ return [ ]
608+ }
609+
610+ return jobRuns
611+ } catch is EntityNotFoundException {
612+ print ( " *** The specified job name, \( jobName) , doesn't exist. " )
613+ return [ ]
614+ } catch {
615+ print ( " *** Unexpected error getting job runs: " )
616+ dump ( error)
617+ return [ ]
618+ }
619+ }
620+ // snippet-end:[swift.glue.GetJobRuns]
621+
557622 // snippet-start:[swift.glue.GetJobRun]
558623 /// Get information about a specific AWS Glue job run.
559624 ///
@@ -660,6 +725,13 @@ struct ExampleCommand: ParsableCommand {
660725
661726 print ( " Getting the crawler's database... " )
662727 let database = await getDatabase ( glueClient: glueClient, name: databaseName)
728+
729+ guard let database else {
730+ print ( " *** Unable to get the database. " )
731+ return
732+ }
733+ print ( " Database URI: \( database. locationUri ?? " <unknown> " ) " )
734+
663735 let tableList = await getTablesInDatabase ( glueClient: glueClient, databaseName: databaseName)
664736
665737 print ( " Found \( tableList. count) table(s): " )
@@ -765,10 +837,31 @@ struct ExampleCommand: ParsableCommand {
765837 print ( " *** Warning: Job run timed out. " )
766838 jobRunFinished = true
767839 default :
768- Thread . sleep ( forTimeInterval: 0.25 )
840+ do {
841+ try await Task . sleep ( for: . milliseconds( 250 ) )
842+ } catch {
843+ print ( " *** Error pausing the task. " )
844+ }
769845 }
770846 } while jobRunFinished != true
771847
848+ //=====================================================================
849+ // 7.5. List the job runs for this job, showing each job run's ID and
850+ // its execution time.
851+ //=====================================================================
852+
853+ print ( " Getting all job runs for the job \( jobName) : " )
854+ let jobRuns = await getJobRuns ( glueClient: glueClient, name: jobName)
855+
856+ if jobRuns. count == 0 {
857+ print ( " <no job runs found> " )
858+ } else {
859+ print ( " Found \( jobRuns. count) job runs... listing execution times: " )
860+ for jobRun in jobRuns {
861+ print ( " \( jobRun. id ?? " <unnamed> " ) : \( jobRun. executionTime) seconds " )
862+ }
863+ }
864+
772865 //=====================================================================
773866 // 8. List the jobs for the user's account.
774867 //=====================================================================
0 commit comments