private void UpdateAnimeGroupsAndTheirContracts(ISessionWrapper session, IReadOnlyCollection <AnimeGroup> groups) { _log.Info("Updating statistics and contracts for AnimeGroups"); var allCreatedGroupUsers = new ConcurrentBag <List <AnimeGroup_User> >(); // Update batches of AnimeGroup contracts in parallel. Each parallel branch requires it's own session since NHibernate sessions aren't thread safe. // The reason we're doing this in parallel is because updating contacts does a reasonable amount of work (including LZ4 compression) Parallel.ForEach(groups.Batch(DefaultBatchSize), new ParallelOptions { MaxDegreeOfParallelism = 4 }, localInit: () => DatabaseFactory.SessionFactory.OpenStatelessSession(), body: (groupBatch, state, localSession) => { var createdGroupUsers = new List <AnimeGroup_User>(groupBatch.Length); // We shouldn't need to keep track of updates to AnimeGroup_Users in the below call, because they should have all been deleted, // therefore they should all be new AnimeGroup.BatchUpdateStats(groupBatch, watchedStats: true, missingEpsStats: true, createdGroupUsers: createdGroupUsers); allCreatedGroupUsers.Add(createdGroupUsers); AnimeGroup.BatchUpdateContracts(localSession.Wrap(), groupBatch, updateStats: true); return(localSession); }, localFinally: localSession => { localSession.Dispose(); }); _animeGroupRepo.UpdateBatch(session, groups); _log.Info("AnimeGroup statistics and contracts have been updated"); _log.Info("Creating AnimeGroup_Users and updating plex/kodi contracts"); List <AnimeGroup_User> animeGroupUsers = allCreatedGroupUsers.SelectMany(groupUsers => groupUsers).ToList(); // Insert the AnimeGroup_Users so that they get assigned a primary key before we update plex/kodi contracts _animeGroupUserRepo.InsertBatch(session, animeGroupUsers); // We need to repopulate caches for AnimeGroup_User and AnimeGroup because we've updated/inserted them // and they need to be up to date for the plex/kodi contract updating to work correctly _animeGroupUserRepo.Populate(session, displayname: false); _animeGroupRepo.Populate(session, displayname: false); // NOTE: There are situations in which UpdatePlexKodiContracts will cause database database writes to occur, so we can't // use Parallel.ForEach for the time being (If it was guaranteed to only read then we'd be ok) foreach (AnimeGroup_User groupUser in animeGroupUsers) { groupUser.UpdatePlexKodiContracts(session); } _animeGroupUserRepo.UpdateBatch(session, animeGroupUsers); _log.Info("AnimeGroup_Users have been created"); }