private void RequestRepository(TransferRepository repository, Reporter reporter)
        {
            var repozipFile = RepoEnv.TempFiles.CreateFile();

            UpdateLock.EnterUpgradeableReadLock();
            try
            {
                Log.Info("Incomming Transfer Request for Repository {Name}", repository.RepoName);
                reporter.Send(RepositoryMessages.GetRepo);

                var data = _repos.AsQueryable().FirstOrDefault(r => r.RepoName == repository.RepoName);
                if (data == null)
                {
                    reporter.Compled(OperationResult.Failure(RepoErrorCodes.DatabaseNoRepoFound));
                    return;
                }

                var commitInfo = _gitHubClient.Repository.Commit.GetSha1(data.RepoId, "HEAD").Result;

                var repozip = repozipFile.Stream;

                if (!(commitInfo != data.LastUpdate && UpdateRepository(data, reporter, repository, commitInfo, repozip)))
                {
                    reporter.Send(RepositoryMessages.GetRepositoryFromDatabase);
                    Log.Info("Downloading Repository {Name} From Server", repository.RepoName);
                    repozip.SetLength(0);
                    _bucket.DownloadToStream(ObjectId.Parse(data.FileName), repozip);
                }

                //_reporter = reporter;

                //repozip.Seek(0, SeekOrigin.Begin);
                //Timers.StartSingleTimer(_reporter, new TransferFailed(string.Empty, FailReason.Timeout, data.RepoName), TimeSpan.FromMinutes(10));
                var request = DataTransferRequest.FromStream(repository.OperationId, repozip, repository.Manager ?? throw new ArgumentNullException("FileManager"), commitInfo);
                request.SendCompletionBack = true;

                _dataTransfer.Request(request);
                _currentTransfers[request.OperationId] = repozipFile;

                reporter.Compled(OperationResult.Success(new FileTransactionId(request.OperationId)));
            }
            finally
            {
                UpdateLock.ExitUpgradeableReadLock();
            }
        }
Example #2
0
        public AppQueryHandler(IMongoCollection <AppData> apps, GridFSBucket files, DataTransferManager dataTransfer, IActorRef changeTracker)
        {
            Receive <QueryChangeSource>(changeTracker.Forward);

            MakeQueryCall <QueryApps, AppList>("QueryApps", (query, _)
                                               => new AppList(apps.AsQueryable().Select(ad => ad.ToInfo()).ToImmutableList()));

            MakeQueryCall <QueryApp, AppInfo>("QueryApp", (query, reporter) =>
            {
                var data = apps.AsQueryable().FirstOrDefault(e => e.Name == query.AppName);

                if (data != null)
                {
                    return(data.ToInfo());
                }

                reporter.Compled(OperationResult.Failure(BuildErrorCodes.QueryAppNotFound));
                return(null);
            });

            MakeQueryCall <QueryBinarys, FileTransactionId>("QueryBinaries", (query, reporter) =>
            {
                if (query.Manager == null)
                {
                    return(null);
                }

                var data = apps.AsQueryable().FirstOrDefault(e => e.Name == query.AppName);

                if (data == null)
                {
                    reporter.Compled(OperationResult.Failure(BuildErrorCodes.QueryAppNotFound));
                    return(null);
                }

                var targetVersion = query.AppVersion != -1 ? query.AppVersion : data.Last;

                var file = data.Versions.FirstOrDefault(f => f.Version == targetVersion);
                if (file == null)
                {
                    reporter.Compled(OperationResult.Failure(BuildErrorCodes.QueryFileNotFound));
                    return(null);
                }

                var request = DataTransferRequest.FromStream(() => files.OpenDownloadStream(file.File), query.Manager, query.AppName);
                dataTransfer.Request(request);

                return(new FileTransactionId(request.OperationId));
            });

            MakeQueryCall <QueryBinaryInfo, BinaryList>("QueryBinaryInfo", (binarys, reporter) =>
            {
                var data = apps.AsQueryable().FirstOrDefault(ad => ad.Name == binarys.AppName);
                if (data != null)
                {
                    return(new BinaryList(data.Versions.Select(i => new AppBinary(data.Name, i.Version, i.CreationTime, i.Deleted, i.Commit, data.Repository)).ToImmutableList()));
                }

                reporter.Compled(OperationResult.Failure(BuildErrorCodes.QueryAppNotFound));
                return(null);
            });
        }