Get Total duration time of succedeed jobs

Hi,

I have this code

                IStorageConnection connection = JobStorage.Current.GetConnection();
                StateData stateData = connection.GetStateData(context.BackgroundJob.Id);
                TimeSpan elapsedTime = new TimeSpan();
                if ((stateData.Data.ContainsKey("PerformanceDuration")) && (stateData.Data.ContainsKey("Latency")))
                {
                    TimeSpan duration = TimeSpan.FromMilliseconds(long.Parse(stateData.Data["PerformanceDuration"]));
                    TimeSpan latency = TimeSpan.FromMilliseconds(long.Parse(stateData.Data["Latency"]));
                    elapsedTime = duration + latency;
                }

But there are case on the stateData.Data is not contains the PerformanceDuration key.

How to get the Total duration time of succedeed jobs ?

Thanks

If you only care about succeeded jobs you can write a filter and when the elected state is SucceededState type, you can cast to it and read the ElapsedDuration field.

Where I work we care about the performance of failed jobs and how long a job was sitting enqueued so we have a filter which writes relevant data as job parameters and we can parse them as the states transition. We used to use the job state history but we have short running jobs that need to retry forever on failure, and that ended up causing performance problems when an upstream gateway went down.

Yes @Andrew_Borland, it is correct.
I write a filter and it working fine.
Thanks, regards

...
        public void OnStateElection(ElectStateContext context)
        {
            var succeededState = context.CandidateState as SucceededState;
            if (succeededState != null)
            {
                double elapsedTimeMillisec = succeededState.PerformanceDuration + succeededState.Latency;
                TimeSpan elapsedTime = TimeSpan.FromMilliseconds(elapsedTimeMillisec);
                var elapsedHumanized = elapsedTime.Humanize(2);
                DateTime succeededAt = succeededState.SucceededAt;
...
                slog.Information("Job {JobName} succeeded. Running time {ElapsedTimeH}");
                return;
            }

            var failedState = context.CandidateState as FailedState;
            if (failedState != null)
            {
              ...
                slog.Fatal(failedState.Exception, "Job {JobId} has been failed due to an exception ");
                return;
            }
        }