diff --git a/services/migration/migrate-posts.go b/services/migration/migrate-posts.go index be8e247..ebdddc5 100644 --- a/services/migration/migrate-posts.go +++ b/services/migration/migrate-posts.go @@ -313,14 +313,14 @@ func createPost(postId int, migration MigratePosts, wpTagData []wordpress.TagDat func updatePostRelationships(postResults []PostResult, migration MigratePosts) { fmt.Println("Updating post relationships") + totalPostResults := len(postResults) batchSize := 5 postResultWorkInput := make(chan PostResult) doneChannel := make(chan bool) defer close(doneChannel) //Set up a wait group to wait for the input channel to close after execution var wg sync.WaitGroup - wg.Add(1) - wg.Add(len(postResults)) + wg.Add(totalPostResults + 1) //Launch go routines that will read from the input channel, //maxed out at the batch size @@ -330,9 +330,13 @@ func updatePostRelationships(postResults []PostResult, migration MigratePosts) { select { case <-doneChannel: return - case result := <-postResultWorkInput: - updatePostRelationship(result, migration) - wg.Done() + case result, ok := <-postResultWorkInput: + if ok { + updatePostRelationship(result, migration) + wg.Done() + } else { + return + } } } }() @@ -342,7 +346,7 @@ func updatePostRelationships(postResults []PostResult, migration MigratePosts) { //as the workers take work from the channel. This will be blocked while //any task is waiting to be picked up from the channel go func() { - defer close(postResultWorkInput) + //defer close(postResultWorkInput) for _, result := range postResults { select { @@ -351,7 +355,6 @@ func updatePostRelationships(postResults []PostResult, migration MigratePosts) { case postResultWorkInput <- result: } } - wg.Done() }()