@@ -267,7 +267,14 @@ struct ExampleCommand: ParsableCommand {
267
267
} else if state == . stopping {
268
268
return false
269
269
}
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
+ }
271
278
}
272
279
}
273
280
// snippet-end:[swift.glue.getCrawlerState]
@@ -459,7 +466,6 @@ struct ExampleCommand: ParsableCommand {
459
466
}
460
467
// snippet-end:[swift.glue.GetTables]
461
468
462
- // snippet-start:[swift.glue.BatchDeleteTable]
463
469
// snippet-start:[swift.glue.DeleteDatabase]
464
470
/// Delete the specified database.
465
471
///
@@ -486,24 +492,46 @@ struct ExampleCommand: ParsableCommand {
486
492
tableNames. append ( name)
487
493
}
488
494
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
+ )
496
509
)
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]
500
528
}
501
- return true
502
529
}
503
530
504
531
// Delete the database itself.
505
532
506
533
do {
534
+ print ( " Deleting the database itself... " )
507
535
_ = try await glueClient. deleteDatabase (
508
536
input: DeleteDatabaseInput ( name: databaseName)
509
537
)
@@ -513,7 +541,6 @@ struct ExampleCommand: ParsableCommand {
513
541
return true
514
542
}
515
543
// snippet-end:[swift.glue.DeleteDatabase]
516
- // snippet-end:[swift.glue.BatchDeleteTable]
517
544
518
545
// snippet-start:[swift.glue.StartJobRun]
519
546
/// Start an AWS Glue job run.
@@ -554,6 +581,43 @@ struct ExampleCommand: ParsableCommand {
554
581
}
555
582
// snippet-end:[swift.glue.StartJobRun]
556
583
584
+ // snippet-start:[swift.glue.GetJobRuns]
585
+ /// Return a list of the job runs for the specified job.
586
+ ///
587
+ /// - Parameters:
588
+ /// - glueClient: The AWS Glue client to use.
589
+ /// - jobName: The name of the job for which to return its job runs.
590
+ /// - maxResults: The maximum number of job runs to return (default:
591
+ /// 1000).
592
+ ///
593
+ /// - Returns: An array of `GlueClientTypes.JobRun` objects describing
594
+ /// each job run.
595
+ func getJobRuns( glueClient: GlueClient , name jobName: String , maxResults: Int ? = nil ) async -> [ GlueClientTypes . JobRun ] {
596
+ do {
597
+ let output = try await glueClient. getJobRuns (
598
+ input: GetJobRunsInput (
599
+ jobName: jobName,
600
+ maxResults: maxResults
601
+ )
602
+ )
603
+
604
+ guard let jobRuns = output. jobRuns else {
605
+ print ( " *** No job runs found. " )
606
+ return [ ]
607
+ }
608
+
609
+ return jobRuns
610
+ } catch is EntityNotFoundException {
611
+ print ( " *** The specified job name, \( jobName) , doesn't exist. " )
612
+ return [ ]
613
+ } catch {
614
+ print ( " *** Unexpected error getting job runs: " )
615
+ dump ( error)
616
+ return [ ]
617
+ }
618
+ }
619
+ // snippet-end:[swift.glue.GetJobRuns]
620
+
557
621
// snippet-start:[swift.glue.GetJobRun]
558
622
/// Get information about a specific AWS Glue job run.
559
623
///
@@ -660,6 +724,13 @@ struct ExampleCommand: ParsableCommand {
660
724
661
725
print ( " Getting the crawler's database... " )
662
726
let database = await getDatabase ( glueClient: glueClient, name: databaseName)
727
+
728
+ guard let database else {
729
+ print ( " *** Unable to get the database. " )
730
+ return
731
+ }
732
+ print ( " Database URI: \( database. locationUri ?? " <unknown> " ) " )
733
+
663
734
let tableList = await getTablesInDatabase ( glueClient: glueClient, databaseName: databaseName)
664
735
665
736
print ( " Found \( tableList. count) table(s): " )
@@ -765,10 +836,31 @@ struct ExampleCommand: ParsableCommand {
765
836
print ( " *** Warning: Job run timed out. " )
766
837
jobRunFinished = true
767
838
default :
768
- Thread . sleep ( forTimeInterval: 0.25 )
839
+ do {
840
+ try await Task . sleep ( for: . milliseconds( 250 ) )
841
+ } catch {
842
+ print ( " *** Error pausing the task. " )
843
+ }
769
844
}
770
845
} while jobRunFinished != true
771
846
847
+ //=====================================================================
848
+ // 7.5. List the job runs for this job, showing each job run's ID and
849
+ // its execution time.
850
+ //=====================================================================
851
+
852
+ print ( " Getting all job runs for the job \( jobName) : " )
853
+ let jobRuns = await getJobRuns ( glueClient: glueClient, name: jobName)
854
+
855
+ if jobRuns. count == 0 {
856
+ print ( " <no job runs found> " )
857
+ } else {
858
+ print ( " Found \( jobRuns. count) job runs... listing execution times: " )
859
+ for jobRun in jobRuns {
860
+ print ( " \( jobRun. id ?? " <unnamed> " ) : \( jobRun. executionTime) seconds " )
861
+ }
862
+ }
863
+
772
864
//=====================================================================
773
865
// 8. List the jobs for the user's account.
774
866
//=====================================================================
0 commit comments