@@ -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,34 +492,56 @@ 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
)
510
538
} catch {
511
539
print ( " *** Unable to delete the database. " )
540
+ return false
512
541
}
513
542
return true
514
543
}
515
544
// snippet-end:[swift.glue.DeleteDatabase]
516
- // snippet-end:[swift.glue.BatchDeleteTable]
517
545
518
546
// snippet-start:[swift.glue.StartJobRun]
519
547
/// Start an AWS Glue job run.
@@ -554,6 +582,43 @@ struct ExampleCommand: ParsableCommand {
554
582
}
555
583
// snippet-end:[swift.glue.StartJobRun]
556
584
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
+
557
622
// snippet-start:[swift.glue.GetJobRun]
558
623
/// Get information about a specific AWS Glue job run.
559
624
///
@@ -660,6 +725,13 @@ struct ExampleCommand: ParsableCommand {
660
725
661
726
print ( " Getting the crawler's database... " )
662
727
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
+
663
735
let tableList = await getTablesInDatabase ( glueClient: glueClient, databaseName: databaseName)
664
736
665
737
print ( " Found \( tableList. count) table(s): " )
@@ -765,10 +837,31 @@ struct ExampleCommand: ParsableCommand {
765
837
print ( " *** Warning: Job run timed out. " )
766
838
jobRunFinished = true
767
839
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
+ }
769
845
}
770
846
} while jobRunFinished != true
771
847
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
+
772
865
//=====================================================================
773
866
// 8. List the jobs for the user's account.
774
867
//=====================================================================
0 commit comments