public void EpochToDateToEpoch()
        {
            long time      = 15237623489;
            long converted = EpochConverter.ToUnixEpochSeconds(EpochConverter.FromUnixEpochSeconds(time));

            time.ShouldEqual(converted);
        }
        public void DateToEpochToDate()
        {
            DateTime time      = new DateTime(2018, 12, 18, 8, 12, 13, DateTimeKind.Utc);
            DateTime converted = EpochConverter.FromUnixEpochSeconds(EpochConverter.ToUnixEpochSeconds(time));

            time.ShouldEqual(converted);
        }
Esempio n. 3
0
        public bool TryGetMaintenanceDelayTime(out DateTime time)
        {
            string timeFileName = this.GetMaintenanceDelayFilePath();

            if (this.fileSystem.FileExists(timeFileName))
            {
                try
                {
                    string contents = this.fileSystem.ReadAllText(timeFileName);

                    if (long.TryParse(contents, out long seconds))
                    {
                        time = EpochConverter.FromUnixEpochSeconds(seconds);
                        return(true);
                    }
                }
                catch (IOException)
                {
                    // Eat any issue reading this file
                }
            }

            time = DateTime.MinValue;
            return(false);
        }
Esempio n. 4
0
        protected override void PerformMaintenance()
        {
            long   last;
            string error = null;

            if (!this.TryGetMaxGoodPrefetchTimestamp(out last, out error))
            {
                this.Context.Tracer.RelatedError(error);
                return;
            }

            DateTime lastDateTime = EpochConverter.FromUnixEpochSeconds(last);
            DateTime now          = DateTime.UtcNow;

            if (now <= lastDateTime + this.timeBetweenPrefetches)
            {
                this.Context.Tracer.RelatedInfo(this.Area + ": Skipping prefetch since most-recent prefetch ({0}) is too close to now ({1})", lastDateTime, now);
                return;
            }

            this.RunGitCommand(process =>
            {
                this.TryPrefetchCommitsAndTrees(out error, process);
                return(null);
            });

            if (!string.IsNullOrEmpty(error))
            {
                this.Context.Tracer.RelatedWarning(
                    metadata: this.CreateEventMetadata(),
                    message: $"{nameof(this.TryPrefetchCommitsAndTrees)} failed with error '{error}'",
                    keywords: Keywords.Telemetry);
            }
        }
Esempio n. 5
0
        private void BackgroundPrefetch()
        {
            try
            {
                using (ITracer activity = this.tracer.StartActivity(nameof(this.BackgroundPrefetch), EventLevel.Informational))
                {
                    long   last;
                    string error;

                    if (!CommitPrefetcher.TryGetMaxGoodPrefetchTimestamp(activity, this.enlistment, this.fileSystem, this.gitObjects, out last, out error))
                    {
                        activity.RelatedError(error);
                        return;
                    }

                    DateTime lastDateTime = EpochConverter.FromUnixEpochSeconds(last);
                    DateTime now          = DateTime.UtcNow;

                    if (now <= lastDateTime + this.timeBetweenPrefetches)
                    {
                        activity.RelatedInfo(TelemetryKey + ": Skipping prefetch since most-recent prefetch ({0}) is too close to now ({1})", lastDateTime, now);
                        return;
                    }

                    if (!CommitPrefetcher.TryPrefetchCommitsAndTrees(activity, this.enlistment, this.fileSystem, this.gitObjects, out error))
                    {
                        activity.RelatedError($"{TelemetryKey}: {nameof(CommitPrefetcher.TryPrefetchCommitsAndTrees)} failed with error '{error}'");
                    }
                }
            }
            catch (ThreadAbortException)
            {
                this.tracer.RelatedInfo(TelemetryKey + ": Aborting prefetch background thread due to ThreadAbortException");
                return;
            }
            catch (IOException e)
            {
                EventMetadata metadata = new EventMetadata();
                metadata.Add("Method", nameof(this.BackgroundPrefetch));
                metadata.Add("ExceptionMessage", e.Message);
                metadata.Add("StackTrace", e.StackTrace);
                this.tracer.RelatedWarning(
                    metadata: metadata,
                    message: TelemetryKey + ": IOException while running prefetch background thread (non-fatal): " + e.Message,
                    keywords: Keywords.Telemetry);
            }
            catch (Exception e)
            {
                EventMetadata metadata = new EventMetadata();
                metadata.Add("Method", nameof(this.BackgroundPrefetch));
                metadata.Add("ExceptionMessage", e.Message);
                metadata.Add("StackTrace", e.StackTrace);
                this.tracer.RelatedError(
                    metadata: metadata,
                    message: TelemetryKey + ": Unexpected Exception while running prefetch background thread (fatal): " + e.Message,
                    keywords: Keywords.Telemetry);
                Environment.Exit((int)ReturnCode.GenericError);
            }
        }
Esempio n. 6
0
        private bool TryFetchUsingGvfsProtocol(GitProcess gitProcess, out string error)
        {
            if (!this.TryGetMaxGoodPrefetchPackTimestamp(out long last, out error))
            {
                this.Context.Tracer.RelatedError(error);
                return(false);
            }

            TimeSpan timeBetween = this.GitObjects.IsUsingCacheServer()
                                    ? this.timeBetweenFetches
                                    : this.timeBetweenFetchesNoCacheServer;

            DateTime lastDateTime = EpochConverter.FromUnixEpochSeconds(last);
            DateTime now          = DateTime.UtcNow;

            if (!this.forceRun && now <= lastDateTime + timeBetween)
            {
                this.Context.Tracer.RelatedInfo(this.Area + ": Skipping fetch since most-recent fetch ({0}) is too close to now ({1})", lastDateTime, now);
                error = null;
                return(true);
            }

            // We take our own lock here to keep background and foreground fetches
            // (i.e. a user running 'scalar run fetch')
            // from running at the same time.
            using (FileBasedLock fetchLock = ScalarPlatform.Instance.CreateFileBasedLock(
                       this.Context.FileSystem,
                       this.Context.Tracer,
                       Path.Combine(this.Context.Enlistment.GitPackRoot, FetchCommitsAndTreesLock)))
            {
                WaitUntilLockIsAcquired(this.Context.Tracer, fetchLock);

                this.GitObjects.DeleteStaleTempPrefetchPackAndIdxs();
                this.GitObjects.DeleteTemporaryFiles();

                GitProcess.Result result = gitProcess.GvfsHelperPrefetch();

                if (result.ExitCodeIsFailure)
                {
                    error = result.Errors;
                    return(false);
                }

                this.UpdateKeepPacks();
            }

            error = null;
            return(true);
        }
Esempio n. 7
0
        protected bool EnoughTimeBetweenRuns()
        {
            if (!this.Context.FileSystem.FileExists(this.LastRunTimeFilePath))
            {
                return(true);
            }

            string lastRunTime = this.Context.FileSystem.ReadAllText(this.LastRunTimeFilePath);

            if (!long.TryParse(lastRunTime, out long result))
            {
                this.Context.Tracer.RelatedError("Failed to parse long: {0}", lastRunTime);
                return(true);
            }

            if (DateTime.UtcNow.Subtract(EpochConverter.FromUnixEpochSeconds(result)) >= this.TimeBetweenRuns)
            {
                return(true);
            }

            return(false);
        }
        public void FixedDates()
        {
            DateTime[] times = new DateTime[]
            {
                new DateTime(2018, 12, 13, 20, 53, 30, DateTimeKind.Utc),
                new DateTime(2035, 1, 3, 5, 0, 59, DateTimeKind.Utc),
                new DateTime(1989, 12, 31, 23, 59, 59, DateTimeKind.Utc)
            };
            long[] epochs = new long[]
            {
                1544734410,
                2051413259,
                631151999
            };

            for (int i = 0; i < times.Length; i++)
            {
                long epoch = EpochConverter.ToUnixEpochSeconds(times[i]);
                epoch.ShouldEqual(epochs[i]);

                DateTime time = EpochConverter.FromUnixEpochSeconds(epochs[i]);
                time.ShouldEqual(times[i]);
            }
        }