Beispiel #1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
                int limit = 10;
                int skip  = 0;
                while (true)
                {
                    var fileCids = await _filPanManager.GetFileCids(FileCidStatus.None, skip, limit);

                    if (fileCids.Count() == 0)
                    {
                        break;
                    }
                    foreach (var fileCid in fileCids)
                    {
                        var path = _filPanManager.GetStoragePath(fileCid.Id);
                        if (File.Exists(path))
                        {
                            var result = await _lotusClient.ClientImport(new ClientImportRequest
                            {
                                Path  = path,
                                IsCAR = false
                            });

                            if (result.Success)
                            {
                                await _filPanManager.UpdateFileCid(fileCid.Id, result.Result.Root.Value, FileCidStatus.Success);
                            }
                        }
                    }
                    skip += limit;
                }
                try
                {
                    var imports = await _lotusClient.ClientListImports();

                    if (!imports.Success)
                    {
                        continue;
                    }
                    if (imports.Result.Length > 0)
                    {
                        var rootCids = imports.Result.Where(i => i.Root != null)
                                       .Select(i => i.Root.Value).Distinct().ToDictionary(i => i, i => imports.Result.First(j => j.Root?.Value == i).Key);
                        foreach (var item in imports.Result)
                        {
                            if (item.Root == null)
                            {
                                continue;
                            }
                            if (rootCids[item.Root.Value] != item.Key)
                            {
                                await _lotusClient.ClientRemoveImport(item.Key);

                                _logger.LogWarning("remove  duplicate import {key}", item.Key);
                            }
                        }
                        rootCids = null;

                        imports = await _lotusClient.ClientListImports();

                        if (!imports.Success)
                        {
                            continue;
                        }
                        var minDealDuration = 180 * LotusConstants.EpochsInDay;

                        foreach (var item in imports.Result)
                        {
                            if (item.Root == null)
                            {
                                continue;
                            }
                            var queryOffers = await _lotusClient.ClientFindData(new ClientFindDataRequest { Root = item.Root });

                            if (!queryOffers.Success)
                            {
                                continue;
                            }
                            if (queryOffers.Result.Length > 0)
                            {
                                continue;
                            }
                            if (!File.Exists(item.FilePath))
                            {
                                continue;
                            }
                            var size  = new FileInfo(item.FilePath).Length;
                            var miner = _lotusClientSetting.GetMinerByFileSize(size);
                            if (string.IsNullOrEmpty(miner))
                            {
                                continue;
                            }
                            var minerInfo = await _lotusClient.StateMinerInfo(new StateMinerInfoRequest { Miner = miner });

                            if (!minerInfo.Success)
                            {
                                continue;
                            }
                            var ask = await _lotusClient.ClientQueryAsk(new ClientQueryAskRequest
                            {
                                PeerId = minerInfo.Result.PeerId,
                                Miner  = miner
                            });

                            if (!ask.Success)
                            {
                                continue;
                            }
                            var dealParams = await CreateTTGraphsyncClientStartDealParams(new ClientStartDealRequest
                            {
                                DataCid  = item.Root.Value,
                                Miner    = miner,
                                Price    = ask.Result.Ask.VerifiedPrice,
                                Duration = minDealDuration
                            });

                            if (dealParams == null)
                            {
                                continue;
                            }
                            var dealCid = await _lotusClient.ClientStartDeal(dealParams);

                            if (dealCid.Success)
                            {
                                _logger.LogInformation("ClientStartDeal Result: {datecid}(datecid) - {dealcid}(dealcid)", item.Root.Value, dealCid.Result.Value);
                            }
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    _logger.LogError(0, ex, "Worker error");
                }

                await Task.Delay(1000, stoppingToken);
            }
        }
Beispiel #2
0
        public async Task <ApiResponse <DownloadingFileInfo> > GetFileByCid(string cid)
        {
            using (var scope = GlobalServices.Container.BeginLifetimeScope())
            {
                var repository = scope.Resolve <ICloudSpeedRepository>();
                var fileCid    = await repository.GetFileCidsByCid(cid);

                var downloadingFileInfo = new DownloadingFileInfo();
                if (fileCid != null)
                {
                    var path     = _uploadSetting.GetStoragePath(fileCid.Id);
                    var fileName = await GetFileName(fileCid.Id);

                    var mimeType = fileName.GetMimeType();
                    var fileDeal = await repository.GetFileDealByCid(cid);

                    var uploadLog = await repository.GetUploadLogByDataKey(fileCid.Id);

                    var lsInfo = new LocalStroeInfo()
                    {
                        MimeType  = mimeType,
                        FileName  = fileName,
                        FileSize  = new FileInfo(path).ToFileSize(),
                        Date      = fileCid.Created,
                        Publisher = "ming",
                        Miner     = fileDeal != null ? (fileDeal.Miner + "-" + fileDeal.Status) : string.Empty,
                        LogId     = uploadLog.Id
                    };
                    downloadingFileInfo.LocalStroeInfo = lsInfo;
                    if (fileDeal != null && (fileDeal.Status == FileDealStatus.Success || fileDeal.Status == FileDealStatus.Processing))
                    {
                        var queryOffers = await _lotusClient.ClientFindData(new ClientFindDataRequest { Root = new Cid()
                                                                                                        {
                                                                                                            Value = cid
                                                                                                        } });

                        if (queryOffers.Success)
                        {
                            var orderInfos = new List <RetrievalOrderInfo>();
                            foreach (var item in queryOffers.Result)
                            {
                                if (orderInfos.Any(o => o.Miner == item.Miner))
                                {
                                    continue;
                                }
                                var orderInfo = new RetrievalOrderInfo()
                                {
                                    Miner         = item.Miner,
                                    MinerPeerId   = item.MinerPeer.ID,
                                    OfferMinPrice = item.MinPrice,
                                    OfferSize     = ((long)item.Size).ToFileSize(),
                                    Err           = item.Err
                                };
                                orderInfos.Add(orderInfo);
                            }
                            downloadingFileInfo.RetrievalOrderInfos = orderInfos.ToArray();
                        }
                        else
                        {
                            _logger.LogWarning(queryOffers.Error?.Message);
                        }
                    }
                }
                return(ApiResponse.Ok(downloadingFileInfo));
            }
        }