Esempio n. 1
0
        public IActionResult targets(string id = null)
        {
            var results = _cfg.GetSyncTargets();

            if (!id.IsNullOrEmpty())
            {
                id = id.Trim('"', '\\', '/', ' ');
            }

            if (!id.IsNullOrEmpty() && !results.IsNullOrEmpty())
            {
                results = results.Where(x => x.id == id).ToArray();
            }

            if (results.IsNullOrEmpty())
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, $"No Sync Targets Were Found in '{_cfg.targets ?? "undefined"}' with id's '{(id.IsNullOrEmpty() ? "any" : id.JsonSerialize() )}'."));
            }

            return(StatusCode(StatusCodes.Status200OK, results));
        }
Esempio n. 2
0
        public async Task Process()
        {
            var syncTargets = _cfg.GetSyncTargets();

            if (syncTargets.IsNullOrEmpty())
            {
                return;
            }

            foreach (var st in syncTargets)
            {
                if (st.source.IsNullOrEmpty())
                {
                    throw new Exception("SyncTarget 'source' was not defined");
                }
                if (st.destination.IsNullOrEmpty())
                {
                    throw new Exception("SyncTarget 'destination' was not defined");
                }

                if (st.id.IsNullOrEmpty())
                {
                    st.id = Guid.NewGuid().ToString();
                }
            }

            if (_syncResult == null || _syncResult.Count != syncTargets.Length || syncTargets.Any(x => !_syncResult.ContainsKey(x.id)))
            {
                _syncResult = new ConcurrentDictionary <string, SyncResult>();
                foreach (var st in syncTargets)
                {
                    _syncResult.Add(st.id, null);
                }
            }

            if (_syncInfo == null || _syncResult.Count != syncTargets.Length || syncTargets.Any(x => !_syncInfo.ContainsKey(x.id)))
            {
                _syncInfo = new ConcurrentDictionary <string, SyncInfo>();
                foreach (var st in syncTargets)
                {
                    _syncInfo.Add(st.id, null);
                }
            }

            ++_run;
            await ParallelEx.ForEachAsync(syncTargets, async st => {
                var sw = Stopwatch.StartNew();

                if (st.type == SyncTarget.types.none)
                {
                    return;
                }

                _S3Helper = st.profile.IsNullOrEmpty() ?
                            new S3Helper() :
                            new S3Helper(AWSWrapper.Extensions.Helper.GetAWSCredentials(st.profile));

                SyncResult result;
                if (st.type == SyncTarget.types.awsUpload)
                {
                    /* // debug only
                     * result = await UploadAWS(st);
                     * /*/
                    result = TryProcessUploadAWS(st);
                    //*/
                }
                else if (st.type == SyncTarget.types.awsDownload)
                {
                    /* // debug only
                     * result = await DownloadAWS(st);
                     * /*/
                    result = TryProcessDownloadAWS(st);
                    //*/
                }
                else
                {
                    throw new Exception($"SyncTarget type '{st.type.ToString()}' was not defined");
                }

                result.run         = _run;
                result.duration    = sw.ElapsedMilliseconds / 1000;
                _syncResult[st.id] = result;

                if (st.sleep >= 0)
                {
                    var sleep = st.sleep + 1000;
                    Console.WriteLine($"Sync Task {st.id} was compleated, result: {(result.success ? "success":"failure")}, sleep: {st.sleep} [ms].");
                    await Task.Delay(st.sleep);
                }
            }, maxDegreeOfParallelism : _cfg.parallelism);
        }