Пример #1
0
        public IJobBuilder CreateJobBuilder(BeatSyncConfig config)
        {
            //string tempDirectory = "Temp";
            //Directory.CreateDirectory(tempDirectory);
            IDownloadJobFactory downloadJobFactory = new DownloadJobFactory(song =>
            {
                return(new MemoryDownloadContainer());
                //return new FileDownloadContainer(Path.Combine(tempDirectory, (song.Key ?? song.Hash) + ".zip"));
            });
            IJobBuilder jobBuilder = new JobBuilder().SetDownloadJobFactory(downloadJobFactory);

            bool overwriteTarget = false;

            Plugin.log.Info($"Adding target for '{CustomLevelsDirectory}'");
            SongTarget songTarget = new DirectoryTarget(CustomLevelsDirectory, overwriteTarget, songHasher, historyManager, playlistManager);

            //SongTarget songTarget = new MockSongTarget();
            jobBuilder.AddTarget(songTarget);

            JobFinishedAsyncCallback jobFinishedCallback = new JobFinishedAsyncCallback(async(JobResult c) =>
            {
                HistoryEntry entry = c.CreateHistoryEntry();
                if (c.TargetResults != null && c.TargetResults.Length > 0)
                {
                    foreach (TargetResult targetResult in c.TargetResults)
                    {
                        if (targetResult == null)
                        {
                            Plugin.log?.Warn($"TargetResult is null.");
                            continue;
                        }
                        else if (targetResult.Success)
                        {
                            Plugin.log?.Info($"Target transfer successful for {targetResult.Target.DestinationId}|{targetResult.SongState}");
                        }
                        else
                        {
                            Plugin.log?.Warn($"Target transfer unsuccessful for {targetResult.Target.DestinationId}");
                            if (targetResult.Exception != null)
                            {
                                Plugin.log?.Debug(targetResult.Exception);
                            }
                        }
                        // Add entry to history, this should only succeed for jobs that didn't get to the targets.
                        if (targetResult.Target is ITargetWithHistory targetWithHistory && targetWithHistory.HistoryManager != null)
                        {
                            targetWithHistory.HistoryManager.TryAdd(c.Song.Hash, entry);
                        }
                        Plugin.log?.Info($"Song {c.Song} transferred to {targetResult.Target.DestinationId}.");
                    }
                }
                else
                {
                    Plugin.log?.Warn($"{c.Song} has no target results.");
                }
                if (c.Successful)
                {
                    if (c.DownloadResult != null && c.DownloadResult.Status == DownloadResultStatus.Skipped)
                    {
                        Plugin.log?.Info($"      Job skipped: {c.Song} not wanted by any targets.");
                    }
                    else
                    {
                        Plugin.log?.Info($"      Job completed successfully: {c.Song}");
                    }
                }
                else
                {
                    Plugin.log?.Info($"      Job failed: {c.Song}");
                }
            });

            jobBuilder.SetDefaultJobFinishedAsyncCallback(jobFinishedCallback);
            return(jobBuilder);
        }