/// <summary> /// This method is the sole method responsible for assigning values to job's Dynamic Properties. /// It does this based on one of the [only] two types of metatags (Environment or RunTime) /// </summary> /// <param name="jobParameters"></param> /// <param name="replaceMode"></param> /// <param name="inputDates"></param> /// <param name="inputArgs"></param> public static bool SetParametersValuesPB(ref PropertyBag jobParameters, ConfigUtils.MetatagMode replaceMode, MetaTagReplacerInputDates inputDates, object[] inputArgs ) { bool retval = true; MetaTagReplacerInputDates inputDateParms = inputDates; if (inputDateParms == null) inputDateParms = new MetaTagReplacerInputDates(DateTime.Now); log.InfoFormat("Input Date used to initialize run-time properties: {0}", inputDateParms.InDate); PropertyBag tempPropBag = new PropertyBag(); foreach (string metaTagKey in jobParameters.Names) { string metaTagValue = GetMetaTagValue(metaTagKey, inputDateParms, replaceMode, inputArgs); if (metaTagValue != null) { tempPropBag[metaTagKey] = metaTagValue; } else { if (IsValidMetatag(metaTagKey, replaceMode)) { log.WarnFormat("Unable to assign property value for {0}", metaTagKey); } else retval = false; //not a valid meta tag - should never happen at this point as illegal MTs should not make it to PB in a first place } } foreach (string metaTagKey in tempPropBag.Names) { jobParameters[metaTagKey] = tempPropBag[metaTagKey]; log.InfoFormat("Assigned property value: {0} = {1}", metaTagKey, jobParameters[metaTagKey]); } return retval; }
private void LaunchJob(Object _jobWrapper) { JobExeWrapper jobWrapper = _jobWrapper as JobExeWrapper; if (jobWrapper == null) { //early return log.ErrorFormat("Tried LaunchJob with null job wrapper."); return; } //ensure that the job is fully initialized before executing if ( !jobWrapper.IsInititalized && jobWrapper.DependencyState.Equals(StateEnums.ExecutionMode.Execution)) { //throw error if not initialized (Timer Service and File Service should prevent non-initialized jobs from being launched //in execute mode) log.ErrorFormat(jobWrapper.Job.KeyHash, "Unable to initialize job for execution. " + "" + "Aborting execution."); foreach( string s in jobWrapper.DependencyStates.Keys) { log.DebugFormat("Module: {0}\t\tState: {1}", s, jobWrapper.DependencyStates[s]); } return; } try { lock (jobWrapper) { // Make a copy of the Job from the JobWrapper's job Job job = new Job(jobWrapper.Job); job.Key = DateTime.Now; job.JobState.CurrentJobCode = job.Key; job.JobState.CurrentJobHash = job.KeyHash; // bubble up key info to jobwrapper global state ((State)jobWrapper.JobGlobalState).CurrentJobCode = job.Key; ((State)jobWrapper.JobGlobalState).CurrentJobHash = job.KeyHash; //update global list of job keys if (jobKeyCodes.ContainsKey(job.KeyString)==false) jobKeyCodes.Add(job.KeyString, job.KeyHash); // Set the values for the Dynamic Parameters (ie the RunTime metatags) MetaTagReplacerInputDates inputDates = new MetaTagReplacerInputDates(DateTime.Now); if (!string.IsNullOrEmpty(job.JobDetails.InDate)) { DateTime jobProcessDate = DateTime.MinValue; string jobInDate = MetaTagReplacer.GetMetaTagValue(job.JobDetails.InDate, inputDates, null); if (jobInDate == null) jobInDate = job.JobDetails.InDate; if (DateTime.TryParse(jobInDate, out jobProcessDate)) inputDates = new MetaTagReplacerInputDates(jobProcessDate); } job.JobState.CurrentParameters.ProcessInputDates = inputDates; PropertyBag jobParameters = job.JobState.CurrentParameters.CurrentParameters; MetaTagReplacer.SetParametersValuesPB(ref jobParameters, ConfigUtils.MetatagMode.All, inputDates, new object[] { job.JobState }); JobImpl.SetModulesRunTimeParameters(job, false); JobImpl.InitializeBusData(job, ((State)jobWrapper.JobGlobalState).StartupBusData); //MetaTagReplacerInputDates inputDates = new MetaTagReplacerInputDates(DateTime.Now); //jobWrapper.Job.JobState.CurrentParameters.ProcessInputDates = inputDates; //PropertyBag jobParameters = jobWrapper.Job.JobState.CurrentParameters.CurrentParameters; //MetaTagReplacer.SetParametersValuesPB(ref jobParameters, ConfigUtils.MetatagMode.All, inputDates); //JobImpl.SetModulesRunTimeParameters(jobWrapper.Job); // add the new copy to the Historical job instances. jobWrappers[jobWrapper.Job.JobName].HistoricalJobs[job.Key] = job; // prepare to run the job instance jobsToResetState.Clear(); string[] jobWrapperStatesKeys = new string[jobWrapper.DependencyStates.Keys.Count]; jobWrapper.DependencyStates.Keys.CopyTo(jobWrapperStatesKeys, 0); foreach (string key in jobWrapperStatesKeys) { lock (jobsWaitingForExecution) { if (jobWrapper.DependencyStates[key] == StateEnums.ExecutionState.WaitState) { // TODO Introduce this when exposing WCF interface //((IModule)jobWrapper.Job.Modules[key]).Inputs = new string[] { ((State)jobWrapper.JobGlobalState).SourceFileName }; if (!jobsWaitingForExecution.ContainsKey(key)) jobsWaitingForExecution.Add(key, StateEnums.ExecutionState.WaitState); } else if (jobWrapper.DependencyStates[key] == StateEnums.ExecutionState.ExecutionSuccessful) jobsToResetState.Add(key); } } foreach (string _jobname in jobsToResetState) jobWrapper.DependencyStates[_jobname] = StateEnums.ExecutionState.PendingExecution; ((State) jobWrapper.JobGlobalState).ExecutionState = StateEnums.ExecutionState.Executing; string[] waitStateJobs = new string[jobsWaitingForExecution.Count]; jobsWaitingForExecution.Keys.CopyTo(waitStateJobs, 0); foreach (string _jobname in waitStateJobs) { if (jobsWaitingForExecution.ContainsKey(_jobname) && jobWrapper.DependencyStates.ContainsKey(_jobname)) { if (jobsWaitingForExecution[_jobname].Equals(StateEnums.ExecutionState.WaitState)) { //skip suspended jobs if (guiJobStatus.ContainsKey(_jobname) && guiJobStatus[_jobname].Equals(StateEnums.Status.Suspended)) continue; //ProcessJobFromRoot(jobWrapper, ((IDependency<IModule>)jobWrapper.Job.Dependencies[_jobname])); //Attempt alotted number of retries should the job failed with Abort return code //ProcessJobFromRoot(jobWrapper, job, ((IDependency<IModule>)jobWrapper.Job.Dependencies[_jobname])); int max_retries = job.JobDetails.MaxRetries; int num_retries = 0; while (num_retries < max_retries) { ErrorCode returnCode = ProcessJobFromRoot(jobWrapper, job, ((IDependency<IModule>)jobWrapper.Job.Dependencies[_jobname])); if (returnCode != ErrorCode.ABORT_JOB) break; else { System.Threading.Thread.Sleep(job.JobDetails.DelayBetweenRetries); if (job.JobDetails.RetryFromTop) { IDependency<IModule> dep = (IDependency<IModule>) jobWrapper.Job.Dependencies[_jobname]; IEnumerator<IDependency<IModule>> iEnumerator = GetDependencyEnumerator(jobWrapper, dep, dep.GetBreadthFirstEnumerator()); while (iEnumerator.MoveNext()) { IDependency<IModule> d = iEnumerator.Current; if (!d.Name.Equals(job.JobName)) jobWrapper.DependencyStates[d.Name] = StateEnums.ExecutionState.PendingExecution; } } num_retries++; if ((max_retries - num_retries) > 0) log.ErrorFormat(job.KeyHash,"Job {0}, ID {1} failed due to critical error. Will try to run {2} more times.", job.JobName, job.KeyHash, (max_retries - num_retries)); else { log.ErrorFormat(job.KeyHash,"Job {0}, ID {1} failed due to critical error and is aborted with no retries left.", job.JobName, job.KeyHash); log.ErrorFormat(job.KeyHash,"Completed executing job {0}, failed due to critical error and is aborted with no retries left.", job.JobName); } } } //((Job)jobWrapper.HistoricalJobs[jobkey]).ResetOutputs(); job.ResetOutputs(); UpdateJobDetailsForGui(job); } } } ((State) jobWrapper.JobGlobalState).ExecutionState = StateEnums.ExecutionState.WaitState; if (jobWrapper.Job.JobDetails.RunOnce && jobWrapper.OkToRemove) { RemoveJob(job.JobName, false); } log.InfoFormat(job.KeyHash,"Completed executing job {0} in {1} mode in {2} secs.", job.JobName, jobWrapper.DependencyState.ToString(), jobWrapper.Job.JobStats.Duration); if (job.JobDetails.RunOnce) ((State)jobWrapper.JobGlobalState).ExecutionState = StateEnums.ExecutionState.Complete; //job.JobState.ExecutionState = StateEnums.ExecutionState.Complete; //ExecuteJobCallback ejb = new ExecuteJobCallback(ProcessJobCallback); //ejb.BeginInvoke(job, null, null); } } catch (Exception ex) { ThreadAbortException tex = ex as ThreadAbortException; if (tex != null) log.Error(tex); else log.Error(ex); } }
/// <summary> /// This should the main single point of getting the meta tag value /// This function fetches individual meta tag value as is, e.g. null if invalid metatag /// </summary> /// <param name="metaTagKey"></param> /// <param name="inDate"></param> /// <returns></returns> public static string GetMetaTagValue(string metaTagKey, MetaTagReplacerInputDates inDateParms, ConfigUtils.MetatagMode? inMode) { return GetMetaTagValue(metaTagKey, inDateParms, inMode, null); }
/// <summary> /// This should the main single point of getting the meta tag value /// This function fetches individual meta tag value as is, e.g. null if invalid metatag /// </summary> /// <param name="metaTagKey"></param> /// <param name="inDate"></param> /// <param name="inArgs"></param> /// <returns></returns> public static string GetMetaTagValue(string metaTagKey, MetaTagReplacerInputDates inDateParms, ConfigUtils.MetatagMode? inMode, object[] inArgs ) { string metaTagValue = null; try { ConfigUtils.MetatagMode mode = ConfigUtils.MetatagMode.All; if (inMode != null) mode = (ConfigUtils.MetatagMode) inMode; if (ConfigUtils.GlobalConfig.GlobalParameters.Contains(metaTagKey) && (mode == ConfigUtils.MetatagMode.Environment || mode == ConfigUtils.MetatagMode.All)) { metaTagValue = ConfigUtils.GlobalConfig.GlobalParameters[metaTagKey] as string; } if (MetaTagReplacerFunctions.ReplacerFunctions.ContainsKey(metaTagKey) && (mode != ConfigUtils.MetatagMode.Environment || mode == ConfigUtils.MetatagMode.All) ) { if(inDateParms == null) throw new ArgumentNullException("Runtime metatag received null inDateParm."); metaTagValue = MetaTagReplacerFunctions.EvaluateMetaTagFunction(metaTagKey, inDateParms, inArgs) as string; } } catch (Exception e) { log.Error(e); } return metaTagValue; }
public static void SetModulesRunTimeParameters(Job job, bool historicalRun) { PropertyBag currentJobParameters = job.JobState.CurrentParameters.CurrentParameters; //This collection is to contain binding between Meta Tag and module to render "ModuleOutputAdded" event ("Donor"); Dictionary<string, object> moduleOutputEventBindings = job.JobState.CurrentParameters.ModuleOutputEventDonors; //new Dictionary<string, object>(); //This collection is to contain EventHandlers that will be fired by Donors and update some Module Propery ("Consumers"); Dictionary<string, EventHandler> moduleOutputEventRecepients = job.JobState.CurrentParameters.ModuleOutputEventConsumers; //New Job launch - erase module level dynamic parameters overrides if (!historicalRun) job.JobState.CurrentParameters.ClearOverrideParameters(); foreach (object moduleName in job.OrderedModuleList) { IModule module = job.Modules[moduleName.ToString()] as IModule; if (module != null) { PropertyBag propertyOriginalValues = job.JobState.CurrentParameters.GetOriginalModulePropertyValuesPB(moduleName.ToString()); PropertyInfo[] pis = module.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); MetaTagReplacerInputDates moduleInDatesOverride = null; // if (historicalRun) { //Use module parameters overrides preserved with job historical instance moduleInDatesOverride = job.JobState.CurrentParameters.GetModuleInDatesOverride(module.Name); } else { //Check for special case of RunDate property which enforces module-level dates override //if (!string.IsNullOrEmpty(module.RunDate)) string runDateOriginalValue = propertyOriginalValues["RunDate"] as string; if (!string.IsNullOrEmpty(runDateOriginalValue)) { //Create a module level meta tag overrides based on module's InDate meta tag DateTime moduleProcessDate = DateTime.MinValue; //string moduleInDate = MetaTagReplacer.GetMetaTagValue(module.RunDate, // job.JobState.CurrentParameters.ProcessInputDates, // null); string moduleInDate = MetaTagReplacer.GetMetaTagValue(runDateOriginalValue, job.JobState.CurrentParameters.ProcessInputDates, null); if (!string.IsNullOrEmpty(moduleInDate)) { if (DateTime.TryParse(moduleInDate, out moduleProcessDate)) { module.RunDate = moduleInDate; moduleInDatesOverride = new MetaTagReplacerInputDates(moduleProcessDate); job.JobState.CurrentParameters.SetModuleInDatesOverride(module.Name, moduleInDatesOverride); log.InfoFormat(job.JobState.CurrentJobHash, "Job {0}, Module {1}: Property RunDate was set to run-time value <{2}> from <{3}> and will be used to compute module-level overrides", job.JobName, module.Name, moduleInDate, runDateOriginalValue); } } } } // if (pis.Length > 0 && propertyOriginalValues.Count() > 0 && currentJobParameters.Count() > 0) { foreach (PropertyInfo pi in pis) { #region Metatag replacement/assignment string propOriginalValue = propertyOriginalValues[pi.Name] as string; if ( propOriginalValue != null && !pi.Name.Equals("RunDate") //Exclude special case of RunDate since it is already populated ) { string propValue = propOriginalValue; PropertyBag modulePropertyPB = currentJobParameters; //PropertyBag modulePropertyPBOverride = new PropertyBag(); bool forceSetPropertyValueToOriginal = false; if (historicalRun) { //Use module parameters overrides preserved with job historical instance //modulePropertyPBOverride = job.JobState.CurrentParameters.GetModulePropertyOverridesPB(module.Name, pi.Name); modulePropertyPB = job.JobState.CurrentParameters.GetModulePropertyOverridesPB(module.Name, pi.Name); if (modulePropertyPB == null) modulePropertyPB = currentJobParameters; } else { if (moduleInDatesOverride != null) { //use this module's RunDate (e.g. process date) parameter to override ALL job-level meta tags modulePropertyPB = MetaTagReplacer.GetJobParametersPB(string.Format("{0}_ {1}_{2}_MetaTagOverride", job.JobName, module.Name, pi.Name), propOriginalValue); if (modulePropertyPB.Count() > 0) { if (MetaTagReplacer.SetParametersValuesPB(ref modulePropertyPB, ConfigUtils.MetatagMode.All, moduleInDatesOverride, new object[] { module })) job.JobState.CurrentParameters.SetModulePropertyOverridesPB(module.Name, pi.Name, modulePropertyPB); } } else { PropertyBag modulePropertyPBOverride = MetaTagReplacer.GetJobParametersPB(string.Format("{0}_ {1}_{2}_MetaTagOverride", job.JobName, module.Name, pi.Name), propOriginalValue); //Remove MetaTags that have not been filled by Job level replacement foreach (string mtKey in modulePropertyPB.Names) { if (modulePropertyPB[mtKey] != null && modulePropertyPBOverride.Contains(mtKey)) modulePropertyPBOverride.Remove(mtKey); } //Fill the remainder of MT values if (modulePropertyPBOverride.Count() > 0) { //If there are meta tags in Original property value always force reset //as otherwise if MT function return 'null' property may contain stale value //if as result of replacement new property value equials original forceSetPropertyValueToOriginal = true; MetaTagReplacer.SetParametersValuesPB(ref modulePropertyPBOverride, ConfigUtils.MetatagMode.All, job.JobState.CurrentParameters.ProcessInputDates, new object[] { module, pi.Name, moduleOutputEventBindings, moduleOutputEventRecepients }); //job.JobState.CurrentParameters.SetModulePropertyOverridesPB(module.Name, pi.Name, modulePropertyPBOverride); //Put these module-property-metatag values back into main job-level PropertyBag //to be potentiall referred by other modules foreach (string mtKey in modulePropertyPBOverride.Names) { if (modulePropertyPBOverride[mtKey] != null && modulePropertyPB.Contains(mtKey)) modulePropertyPB[mtKey] = modulePropertyPBOverride[mtKey]; } } } } //Replace meta tags using finalized property bag propValue = MetaTagReplacer.InlineMetatagSubstitution(propOriginalValue, modulePropertyPB); // if (propValue.CompareTo(propOriginalValue) != 0 || forceSetPropertyValueToOriginal) { if (SetModulePropertyValueFromString(module, pi, propValue)) { log.InfoFormat(job.JobState.CurrentJobHash, "Job {0}, Module {1}: Set property {2} to run-time value <{3}> from original <{4}>", job.JobName, module.Name, pi.Name, propValue, propOriginalValue); } else { log.WarnFormat(job.JobState.CurrentJobHash, "Job {0}, Module {1}: Unable to set Property {2} to run-time value {3}", job.JobName, module.Name, pi.Name, propValue); } } } #endregion #region Folder Override SetModulePropertyValueRunTimeProtections(module, pi); #endregion } } } } }
public static object EvaluateMetaTagFunction(string metatag_name, MetaTagReplacerInputDates inDates, object[] in_arguments) { object result = null; MetaTagReplacerFunction replacer_function = ReplacerFunctions[metatag_name]; //Type[] expected_argument_types = replacer_function.ArgTypes; string[] expected_argument_types = replacer_function.ArgTypes; MetaTagReplacerFuncPtr replacer_delegate = replacer_function.ReplacerFunction; Type return_type = replacer_function.ResultType; //object[] arguments = new object[] { }; object[] arguments = null; //if (expected_argument_types.Length > 0) //{ if (in_arguments == null) arguments = replacer_function.ArgDefaultValues; else arguments = in_arguments; //} if (expected_argument_types.Length == arguments.Length || expected_argument_types.Length == 0) { bool valid_arguments_passed = true; for (int index = 0; index < expected_argument_types.Length; index++) { if (!String.IsNullOrEmpty(expected_argument_types[index])) { if ( !arguments[index].GetType().Name.Equals(expected_argument_types[index]) && !arguments[index].GetType().FullName.Equals(expected_argument_types[index]) ) { valid_arguments_passed = false; break; } } } if (valid_arguments_passed) { result = replacer_delegate.Invoke(inDates, arguments); if (result != null) { if (!result.GetType().Equals(return_type)) { result = null; log.WarnFormat("Return type of MetaTag function {0} does not match expected {1}", metatag_name, return_type.Name); } } } else { log.WarnFormat("Incorrect type of arguments passed into MetaTag function {0}", metatag_name); } } else log.WarnFormat("Incorrect number of arguments passed into MetaTag function {0}", metatag_name); return result; }
public static void LoadReplacerFunctions() { //GOLD dates section ReplacerFunctions.Add("*GOLD_PSDATE*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.GoldPeriodStartDate.ToString("yyyy-MM-dd HH:mm:ss"))); ReplacerFunctions.Add("*GOLD_PEDATE*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.GoldPeriodEndDate.ToString("yyyy-MM-dd HH:mm:ss"))); ReplacerFunctions.Add("*GOLD_KDATE*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.GoldKnowledgeDate.ToString("yyyy-MM-dd HH:mm:ss"))); ReplacerFunctions.Add("*GOLD_PKDATE*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.GoldPriorKnowledgeDate.ToString("yyyy-MM-dd HH:mm:ss"))); //Business Date related ReplacerFunctions.Add("*BUSINESS_DATE*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => String.Format("{0} 12:00:00 AM", inDates.BusinessDate.ToString("M/dd/yyyy")))); //To resolve - which style to use??? //ReplacerFunctions.Add("*LAST_BUSINESS_DATE*", // new MetaTagReplacerFunction(null, null, typeof(string), // (MetaTagReplacerInputDates inDates, object[] in_args) // => inDates.PrevBusinessDate.ToString("yyyy-MM-dd"))); ReplacerFunctions.Add("*LAST_BUSINESS_DATE*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => String.Format("{0} 12:00:00 AM", inDates.PrevBusinessDate.ToString("M/dd/yyyy")))); ReplacerFunctions.Add("*START_LAST_BUSINESS_DATE*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.PrevBusinessDate.ToString("yyyy-MM-dd 00:00:00"))); ReplacerFunctions.Add("*END_LAST_BUSINESS_DATE*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.PrevBusinessDate.ToString("yyyy-MM-dd 23:59:59"))); ReplacerFunctions.Add("*LAST_BUSINESS_DATE_SHORT*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.PrevBusinessDate.ToString("yyyyMMdd"))); ReplacerFunctions.Add("*LAST_BUSINESS_DATE_GPOS*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.PrevBusinessDate.ToString("yyyy-MM-dd 23:59:59"))); ReplacerFunctions.Add("*LAST_BUSINESS_DATE_GXACT*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.PrevBusinessDate.ToString("yyyy-MM-dd 22:00:00"))); ReplacerFunctions.Add("*NEXT_BUSINESS_DATE*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => String.Format("{0} 12:00:00 AM", inDates.NextBusinessDate.ToString("M/dd/yyyy")))); ReplacerFunctions.Add("*NEXT_BUSINESS_DATE_SHORT*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.NextBusinessDate.ToString("M/dd/yyyy"))); //Option Expiration related ReplacerFunctions.Add("*OPTION_DATE_EXPIRATION*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => { string expiryTime = ConfigUtils.GlobalConfig.GlobalParameters["*OPTION_TIME_EXPIRATION*"].ToString(); TimeSpan ts = new TimeSpan(Convert.ToInt32(expiryTime.Substring(0, 2)), Convert.ToInt32(expiryTime.Substring(3, 2)), 0).Subtract(TimeSpan.FromMinutes(1)); return String.Format("{0}:{1}", inDates.InDate.ToString("MM/dd/yyyy"), ts); })); //Current Calendar date related ReplacerFunctions.Add("*TODAY*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.InDate.ToString("yyyy-MM-dd"))); ReplacerFunctions.Add("*TODAY_SHORT*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.InDate.ToString("yyyyMMdd"))); ReplacerFunctions.Add("*YESTERDAY_SHORT*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.InDate.AddDays(-1).ToString("yyyyMMdd"))); ReplacerFunctions.Add("*YESTERDAY*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.InDate.AddDays(-1).ToString("yyyy-MM-dd"))); ReplacerFunctions.Add("*START_YESTERDAY*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.InDate.AddDays(-1).ToString("yyyy-MM-dd 00:00:00"))); ReplacerFunctions.Add("*END_YESTERDAY*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.InDate.AddDays(-1).ToString("yyyy-MM-dd 23:59:59"))); ReplacerFunctions.Add("*TODAY-1*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.InDate.AddDays(-1).ToString("yyyy-MM-dd"))); ReplacerFunctions.Add("*TODAY-2*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.InDate.AddDays(-2).ToString("yyyy-MM-dd"))); ReplacerFunctions.Add("*TODAY-3*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.InDate.AddDays(-3).ToString("yyyy-MM-dd"))); ReplacerFunctions.Add("*TODAY+1*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.InDate.AddDays(1).ToString("yyyy-MM-dd"))); ReplacerFunctions.Add("*TODAY+2*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.InDate.AddDays(2).ToString("yyyy-MM-dd"))); ReplacerFunctions.Add("*TODAY+3*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.InDate.AddDays(3).ToString("yyyy-MM-dd"))); ReplacerFunctions.Add("*END_TODAY*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.InDate.ToString("yyyy-MM-dd 23:59:59"))); ReplacerFunctions.Add("*START_TODAY*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.InDate.ToString("yyyy-MM-dd 00:00:00"))); ReplacerFunctions.Add("*START_OF_MONTH*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.InDate.ToString("yyyy-MM-01 00:00:00"))); ReplacerFunctions.Add("*START_OF_YEAR*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.InDate.ToString("yyyy-01-01 00:00:00"))); ReplacerFunctions.Add("*START_OF_WEEK*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => ConfigUtils.StartOfWeek(inDates.InDate, DayOfWeek.Sunday).ToString("yyyy-MM-dd 00:00:00"))); //This Run Date related ReplacerFunctions.Add("*RUNDATE*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.InDate.ToString("yyyyMMdd"))); //ReplacerFunctions.Add("*RunDate*", // new MetaTagReplacerFunction(null, null, typeof(string), // (MetaTagReplacerInputDates inDates, object[] in_args) // => EvaluateMetaTagFunction("*RUNDATE*", inDates, in_args))); ReplacerFunctions.Add("*RunDate*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.InDate.ToString("yyyyMMdd"))); ReplacerFunctions.Add("*IRUNDATE*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.InDate.ToString("yyyyMMdd"))); ReplacerFunctions.Add("*RUNDATE_SHORT*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.InDate.ToString("yyyy-MM-dd"))); ReplacerFunctions.Add("*RUNDATE_FMT2*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.InDate.ToString("MM/dd/yyyy"))); ReplacerFunctions.Add("*RunID*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => inDates.InDate.ToString("yyyyMMddHHmm"))); ReplacerFunctions.Add("*LRUNDATE*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => String.Format("{0}.{1}", inDates.InDate.ToString("yyyyMMdd"), DateTime.Now.ToString("hhmmss")))); //Next and Previous Run Date related ReplacerFunctions.Add("*T-1*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => ConfigUtils.GetPreviousRunDate(inDates.InDate, -1).ToString("yyyy-MM-dd"))); ReplacerFunctions.Add("*T-2*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => ConfigUtils.GetPreviousRunDate(inDates.InDate, -2).ToString("yyyy-MM-dd"))); ReplacerFunctions.Add("*T-3*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => ConfigUtils.GetPreviousRunDate(inDates.InDate, -3).ToString("yyyy-MM-dd"))); ReplacerFunctions.Add("*T+1*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => ConfigUtils.GetNextRunDate(inDates.InDate, 1).ToString("yyyy-MM-dd"))); ReplacerFunctions.Add("*T+2*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => ConfigUtils.GetNextRunDate(inDates.InDate, 2).ToString("yyyy-MM-dd"))); ReplacerFunctions.Add("*T+3*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => ConfigUtils.GetNextRunDate(inDates.InDate, 3).ToString("yyyy-MM-dd"))); ReplacerFunctions.Add("*RUNDATE-1*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => ConfigUtils.GetPreviousRunDate(inDates.InDate, -1).ToString("yyyyMMdd"))); ReplacerFunctions.Add("*RUNDATE-1_SHORT*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => ConfigUtils.GetPreviousRunDate(inDates.InDate, -1).ToString("yyyy-MM-dd"))); ReplacerFunctions.Add("*IRUNDATE-1*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => ConfigUtils.GetPreviousRunDate(inDates.InDate, -1).ToString("yyyyMMdd"))); //GlobeOp related ReplacerFunctions.Add("*GLOBEOP_DATE*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => String.Format("{0}.{1}", inDates.InDate.ToString("yyyyMMdd"), DateTime.Now.ToString("hhmmss")))); ReplacerFunctions.Add("*GLOBEOP_RUNDATE*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => String.Format("{0}.{1}", inDates.InDate.ToString("yyyyMMdd"), DateTime.Now.ToString("hhmmss")))); //Geneva Dates ReplacerFunctions.Add("*PERIODSTARTDATE*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => String.Format("{0}{1}", inDates.InDate.DayOfWeek.Equals(DayOfWeek.Saturday) || inDates.InDate.DayOfWeek.Equals(DayOfWeek.Monday) ? inDates.InDate.AddDays(-1).ToString("MM/dd/yyyy") : inDates.InDate.ToString("MM/dd/yyyy"), ":00:00:00"))); ReplacerFunctions.Add("*PERIODENDDATE*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => String.Format("{0}{1}", inDates.InDate.DayOfWeek.Equals(DayOfWeek.Sunday) || inDates.InDate.DayOfWeek.Equals(DayOfWeek.Friday) ? inDates.InDate.AddDays(1).ToString("MM/dd/yyyy") : inDates.InDate.ToString("MM/dd/yyyy"), ":23:59:59"))); ReplacerFunctions.Add("*KNOWLEDGEDATE*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => String.Format("{0}{1}", inDates.InDate.DayOfWeek.Equals(DayOfWeek.Sunday) || inDates.InDate.DayOfWeek.Equals(DayOfWeek.Friday) ? inDates.InDate.AddDays(1).ToString("MM/dd/yyyy") : inDates.InDate.ToString("MM/dd/yyyy"), ":22:00:00"))); ReplacerFunctions.Add("*PRIORKNOWLEDGEDATE*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => String.Format("{0}{1}", inDates.InDate.DayOfWeek.Equals(DayOfWeek.Saturday) || inDates.InDate.DayOfWeek.Equals(DayOfWeek.Monday) ? inDates.InDate.AddDays(-2).ToString("MM/dd/yyyy") : inDates.InDate.AddDays(-1).ToString("MM/dd/yyyy"), ":22:00:00"))); ReplacerFunctions.Add("*STAARS_FILENAME*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => String.Format("{0}", inDates.InDate.DayOfWeek.Equals(DayOfWeek.Saturday) ? inDates.InDate.AddDays(-1).ToString("yyyyMMdd") : inDates.InDate.DayOfWeek.Equals(DayOfWeek.Sunday) ? inDates.InDate.AddDays(1).ToString("yyyyMMdd") : inDates.InDate.ToString("yyyyMMdd")) ) ); ReplacerFunctions.Add("*STAARS_FILENAME_LONG*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => String.Format("{0}.{1}", inDates.InDate.DayOfWeek.Equals(DayOfWeek.Saturday) ? inDates.InDate.AddDays(-1).ToString("yyyyMMdd") : inDates.InDate.DayOfWeek.Equals(DayOfWeek.Sunday) ? inDates.InDate.AddDays(1).ToString("yyyyMMdd") : inDates.InDate.ToString("yyyyMMdd") , DateTime.Now.ToString("HHmmss")) ) ); //Third Friday related //set up entry variables ReplacerFunctions.Add("*THIRDFRIDAY*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => ConfigUtils.FindThirdFriday(inDates.InDate).ToString("yyyy-MM-dd"))); ReplacerFunctions.Add("*BACKDAYS*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => (((TimeSpan)ConfigUtils.FindThirdFriday(inDates.InDate).Subtract(inDates.InDate)).Days + 1).ToString())); ReplacerFunctions.Add("*LASTTHIRDFRIDAY*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => ConfigUtils.FindThirdFriday(inDates.InDate).AddMonths(-1).ToString("yyyy-MM-dd"))); ReplacerFunctions.Add("*LASTBACKDAYS*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => (((TimeSpan)ConfigUtils.FindThirdFriday(inDates.InDate).AddMonths(-1).Subtract(inDates.InDate)).Days + 1).ToString())); ReplacerFunctions.Add("*User*", new MetaTagReplacerFunction(null, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => System.Security.Principal.WindowsIdentity.GetCurrent().Name)); // * TEST Standard Replacer Functions ******************************************* *// //if (log.IsDebugEnabled) if (log.IsInfoEnabled) { MetaTagReplacerInputDates input_dates = new MetaTagReplacerInputDates(DateTime.Now); log.DebugFormat("Testing Meta Tag Replacer functions using Input Date {0}:", input_dates.InDate); foreach (string metatagFunctionName in ReplacerFunctions.Keys) { try { string resultTest = MetaTagReplacer.GetMetaTagValue(metatagFunctionName, input_dates, null); if (resultTest == null) resultTest = "<NULL>"; log.DebugFormat("{0}={1}", metatagFunctionName, resultTest); } catch (Exception ex) { log.ErrorFormat("Error while testing meta tag replacer function {0}: {1}", metatagFunctionName, ex.Message); } } } //Add Replacer functions from custom config namespaces //1) Add functions that will replace MetaTags with Job-related objects (Job.JobState or individual Modules) Property values string nameSpace = @"appgroup\METATAGS\JobState"; IList<string> tagNames = ConfigUtils.GlobalConfig.Hierarchy.LevelNames(nameSpace); foreach (string tagName in tagNames) { string tagValue = GetMetaTagConfigValue(nameSpace, tagName); if (!string.IsNullOrEmpty(tagValue)) { ReplacerFunctions.Add(tagName, new MetaTagReplacerFunction(new string[] { "Bits.StateNS.State" }, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => { object result = null; if (in_args != null) result = GetObjectPropertyValue(in_args, tagValue); if (result != null) result = System.Convert.ToString(result); return result; })); log.DebugFormat("Added meta tag function {0} to resolve property {1} of job state", tagName, tagValue); } } //1) Add functions that will replace MetaTags with Job-related objects (Job.JobState or individual Modules) Property values nameSpace = @"appgroup\METATAGS\JobProperty"; tagNames = ConfigUtils.GlobalConfig.Hierarchy.LevelNames(nameSpace); foreach (string tagName in tagNames) { string tagValue = GetMetaTagConfigValue(nameSpace, tagName); ; if (!string.IsNullOrEmpty(tagValue)) { List<string> jobPropertyMTParms = tagValue.Split(';').ToList<string>(); if (jobPropertyMTParms.Count > 1) { string jobPropertyName = jobPropertyMTParms[0]; string jobObjectTypeName = jobPropertyMTParms[1]; int numberedTagsCnt = 1; if (jobPropertyMTParms.Count > 2) { if (!int.TryParse(jobPropertyMTParms[2], out numberedTagsCnt)) numberedTagsCnt = 1; } for (int numberedTagNo = 0; numberedTagNo < numberedTagsCnt; numberedTagNo++) { string tagNameKey = tagName; if (numberedTagNo > 0) { tagNameKey = string.Format("{0}{1}{2}{3}", tagNameKey.Substring(0, tagNameKey.Length-1), "_", numberedTagNo.ToString(), "*"); } ReplacerFunctions.Add(tagNameKey, new MetaTagReplacerFunction(new string[] { jobObjectTypeName, "System.String", "Dictionary`2", "" }, null, typeof(string), (MetaTagReplacerInputDates inDates, object[] in_args) => { object result = null; if (in_args != null) result = GetObjectPropertyValue(in_args, jobPropertyName); if (result != null) result = System.Convert.ToString(result); return result; })); log.DebugFormat("Added meta tag function {0} to resolve property {1} of job object {2}", tagNameKey, jobPropertyName, jobObjectTypeName); } } } } //Bus data events binding and corresponding MT eval lambda //Output DataTable - Row Count nameSpace = @"appgroup\METATAGS\JobModuleOutput"; tagNames = ConfigUtils.GlobalConfig.Hierarchy.LevelNames(nameSpace); // foreach (string tagName in (from tag in tagNames where tag == "*DataTable_RowCount*" select tag).Distinct().ToList<string>() ) { string tagValue = GetMetaTagConfigValue(nameSpace, tagName); ; if (!string.IsNullOrEmpty(tagValue)) { List<string> jobModOut_MTParms = tagValue.Split(';').ToList<string>(); if (jobModOut_MTParms.Count > 0) { int numberedTagsCnt = 1; if (!int.TryParse(jobModOut_MTParms[0], out numberedTagsCnt)) numberedTagsCnt = 1; string moduleOutputDecoratorParm = string.Empty; if (jobModOut_MTParms.Count > 1) moduleOutputDecoratorParm = jobModOut_MTParms[1]; for (int numberedTagNo = 0; numberedTagNo < numberedTagsCnt; numberedTagNo++) { string mtName = tagName; if (numberedTagNo > 0) { mtName = string.Format("{0}{1}{2}{3}", mtName.Substring(0, mtName.Length - 1), "_", numberedTagNo.ToString(), "*"); } ReplacerFunctions.Add(mtName, new MetaTagReplacerFunction(new string[] { "", "System.String", "Dictionary`2", "Dictionary`2" }, null, null, (MetaTagReplacerInputDates inDates, object[] in_args) => { if (in_args != null) { if (in_args.Length == 4) { object moduleToUpdate = in_args[0]; string modulePropertyToUpdate = in_args[1] as string; Dictionary<string, object> moduleEventDonors = in_args[2] as Dictionary<string, object>; Dictionary<string, EventHandler> moduleEventConsumers = in_args[3] as Dictionary<string, EventHandler>; // object moduleEventDonor = null; // if (moduleToUpdate != null && modulePropertyToUpdate != null && moduleEventDonors != null) { if (moduleEventDonors.ContainsKey(mtName)) moduleEventDonor = moduleEventDonors[mtName]; else { moduleEventDonor = moduleToUpdate; moduleEventDonors.Add(mtName, moduleEventDonor); } EventHandler eh = null; string eventConsumerKey = string.Format("{0}_{1}_{2}_{3}", mtName, moduleToUpdate.GetType().Name, moduleToUpdate.GetHashCode().ToString(), modulePropertyToUpdate); if (moduleEventConsumers.ContainsKey(eventConsumerKey)) eh = moduleEventConsumers[eventConsumerKey]; else { string moduleOutputMember = string.Empty; if (!string.IsNullOrEmpty(moduleOutputDecoratorParm)) { Dictionary<string, string> moduleEvenDonorFuncParms = GetMTFuncParmsFromObjectAhcnorProp(moduleEventDonor, "MetaTagsAnchor", mtName); if (moduleEvenDonorFuncParms.ContainsKey(moduleOutputDecoratorParm)) moduleOutputMember = moduleEvenDonorFuncParms[moduleOutputDecoratorParm]; } eh = new EventHandler( (object sender, EventArgs e) => { System.ComponentModel.RefreshEventArgs args = e as System.ComponentModel.RefreshEventArgs; if (args != null) { KeyValuePair<string, int> outputKeyAndIndex = (KeyValuePair<string, int>)args.ComponentChanged; string outputDataKey = outputKeyAndIndex.Key; int outputDataIndex = outputKeyAndIndex.Value; if ( (!string.IsNullOrEmpty(moduleOutputMember) && moduleOutputMember.Equals(outputDataKey)) || (string.IsNullOrEmpty(moduleOutputMember) && outputDataIndex.Equals(1)) ) { System.Collections.Hashtable output_data = GetObjectPropertyValue(new object[] { sender }, "OutputData") as System.Collections.Hashtable; if (output_data.ContainsKey(outputDataKey)) { DataTable output_dt = output_data[outputDataKey] as DataTable; if (output_dt != null) { int rowsCnt = output_dt.Rows.Count; ReplaceMetaTagInObjectPropertyValue(new object[] { moduleToUpdate }, modulePropertyToUpdate, mtName, rowsCnt.ToString()); } } } } }); if (eh != null) moduleEventConsumers.Add(eventConsumerKey, eh); } if (eh != null) { UnsubscribeFromObjectEvent(new object[] { moduleEventDonor }, "ModuleOutputAdded", eh); SubscribeToObjectEvent(new object[] { moduleEventDonor }, "ModuleOutputAdded", eh); } } } } return null; } ) ); } } } } // //Output DataTable - create a delimited list of distinct values from first foreach (string tagName in (from tag in tagNames where tag == "*DataTable_ColumnToDelimList*" select tag).Distinct().ToList<string>() ) { string tagValue = GetMetaTagConfigValue(nameSpace, tagName); ; if (!string.IsNullOrEmpty(tagValue)) { List<string> jobModOut_MTParms = tagValue.Split(';').ToList<string>(); if (jobModOut_MTParms.Count > 0) { int numberedTagsCnt = 1; if (!int.TryParse(jobModOut_MTParms[0], out numberedTagsCnt)) numberedTagsCnt = 1; string moduleOutputMemberNameParm = string.Empty; if (jobModOut_MTParms.Count > 1) moduleOutputMemberNameParm = jobModOut_MTParms[1]; string moduleOutputColumnNameParm = string.Empty; if (jobModOut_MTParms.Count > 2) moduleOutputColumnNameParm = jobModOut_MTParms[2]; string moduleOutputDelimiterParm = string.Empty; if (jobModOut_MTParms.Count > 3) moduleOutputDelimiterParm = jobModOut_MTParms[3]; string moduleOutputWrapperParm = string.Empty; if (jobModOut_MTParms.Count > 4) moduleOutputWrapperParm = jobModOut_MTParms[4]; for (int numberedTagNo = 0; numberedTagNo < numberedTagsCnt; numberedTagNo++) { string mtName = tagName; if (numberedTagNo > 0) { mtName = string.Format("{0}{1}{2}{3}", mtName.Substring(0, mtName.Length - 1), "_", numberedTagNo.ToString(), "*"); } ReplacerFunctions.Add(mtName, new MetaTagReplacerFunction(new string[] { "", "System.String", "Dictionary`2", "Dictionary`2" }, null, null, (MetaTagReplacerInputDates inDates, object[] in_args) => { if (in_args != null) { if (in_args.Length == 4) { object moduleToUpdate = in_args[0]; string modulePropertyToUpdate = in_args[1] as string; Dictionary<string, object> moduleEventDonors = in_args[2] as Dictionary<string, object>; Dictionary<string, EventHandler> moduleEventConsumers = in_args[3] as Dictionary<string, EventHandler>; object moduleEventDonor = null; if (moduleToUpdate != null && modulePropertyToUpdate != null && moduleEventDonors != null) { if (moduleEventDonors.ContainsKey(mtName)) moduleEventDonor = moduleEventDonors[mtName]; else { moduleEventDonor = moduleToUpdate; moduleEventDonors.Add(mtName, moduleEventDonor); } EventHandler eh = null; string eventConsumerKey = string.Format("{0}_{1}_{2}_{3}", mtName, moduleToUpdate.GetType().Name, moduleToUpdate.GetHashCode().ToString(), modulePropertyToUpdate); if (moduleEventConsumers.ContainsKey(eventConsumerKey)) eh = moduleEventConsumers[eventConsumerKey]; else { string moduleOutputMember = string.Empty; string moduleOutputDTColumn = string.Empty; string delim = ","; string wrapWith = string.Empty; Dictionary<string, string> moduleEvenDonorFuncParms = GetMTFuncParmsFromObjectAhcnorProp(moduleEventDonor, "MetaTagsAnchor", mtName); if (!string.IsNullOrEmpty(moduleOutputMemberNameParm)) { if (moduleEvenDonorFuncParms.ContainsKey(moduleOutputMemberNameParm)) moduleOutputMember = moduleEvenDonorFuncParms[moduleOutputMemberNameParm]; } if (!string.IsNullOrEmpty(moduleOutputColumnNameParm)) { if (moduleEvenDonorFuncParms.ContainsKey(moduleOutputColumnNameParm)) moduleOutputDTColumn = moduleEvenDonorFuncParms[moduleOutputColumnNameParm]; } if (!string.IsNullOrEmpty(moduleOutputDelimiterParm)) { if (moduleEvenDonorFuncParms.ContainsKey(moduleOutputDelimiterParm)) delim = moduleEvenDonorFuncParms[moduleOutputDelimiterParm]; } if (!string.IsNullOrEmpty(moduleOutputWrapperParm)) { if (moduleEvenDonorFuncParms.ContainsKey(moduleOutputWrapperParm)) wrapWith = moduleEvenDonorFuncParms[moduleOutputWrapperParm]; } // eh = new EventHandler( (object sender, EventArgs e) => { System.ComponentModel.RefreshEventArgs args = e as System.ComponentModel.RefreshEventArgs; if (args != null) { KeyValuePair<string, int> outputKeyAndIndex = (KeyValuePair<string, int>)args.ComponentChanged; string outputDataKey = outputKeyAndIndex.Key; int outputDataIndex = outputKeyAndIndex.Value; if ( (!string.IsNullOrEmpty(moduleOutputMember) && moduleOutputMember.Equals(outputDataKey)) || (string.IsNullOrEmpty(moduleOutputMember) && outputDataIndex.Equals(1)) ) { System.Collections.Hashtable output_data = GetObjectPropertyValue(new object[] { sender }, "OutputData") as System.Collections.Hashtable; if (output_data.ContainsKey(outputDataKey)) { DataTable output_dt = output_data[outputDataKey] as DataTable; if (output_dt != null) { string colValuesDelimList = (from row in output_dt.AsEnumerable() select new { v = (string.IsNullOrEmpty(moduleOutputDTColumn) ? row[0] : (output_dt.Columns.Contains(moduleOutputDTColumn) ? row[moduleOutputDTColumn] : row[0])) } ).Distinct().Aggregate(new StringBuilder(), (sb, val) => sb.AppendFormat("{0}{1}{0}{2}", wrapWith, val.v, delim), sb => { if (sb.Length > 0) sb.Length--; return sb.ToString(); }); ReplaceMetaTagInObjectPropertyValue(new object[] { moduleToUpdate }, modulePropertyToUpdate, mtName, colValuesDelimList); } } } } }); if (eh != null) moduleEventConsumers.Add(eventConsumerKey, eh); } if (eh != null) { UnsubscribeFromObjectEvent(new object[] { moduleEventDonor }, "ModuleOutputAdded", eh); SubscribeToObjectEvent(new object[] { moduleEventDonor }, "ModuleOutputAdded", eh); } } } } return null; } ) ); } } } } //Output DataTable - create a delimited list of distinct values from first foreach (string tagName in (from tag in tagNames where tag == "*XmlDocument_NodeToDelimList*" select tag).Distinct().ToList<string>() ) { string tagValue = GetMetaTagConfigValue(nameSpace, tagName); ; if (!string.IsNullOrEmpty(tagValue)) { List<string> jobModOut_MTParms = tagValue.Split(';').ToList<string>(); if (jobModOut_MTParms.Count > 0) { int numberedTagsCnt = 1; if (!int.TryParse(jobModOut_MTParms[0], out numberedTagsCnt)) numberedTagsCnt = 1; string moduleOutputMemberNameParm = string.Empty; if (jobModOut_MTParms.Count > 1) moduleOutputMemberNameParm = jobModOut_MTParms[1]; string moduleOutputXmlLevelParm = string.Empty; if (jobModOut_MTParms.Count > 2) moduleOutputXmlLevelParm = jobModOut_MTParms[2]; string moduleOutputXmlNodeNameParm = string.Empty; if (jobModOut_MTParms.Count > 3) moduleOutputXmlNodeNameParm = jobModOut_MTParms[3]; string moduleOutputDelimiterParm = string.Empty; if (jobModOut_MTParms.Count > 4) moduleOutputDelimiterParm = jobModOut_MTParms[4]; string moduleOutputWrapperParm = string.Empty; if (jobModOut_MTParms.Count > 5) moduleOutputWrapperParm = jobModOut_MTParms[5]; for (int numberedTagNo = 0; numberedTagNo < numberedTagsCnt; numberedTagNo++) { string mtName = tagName; if (numberedTagNo > 0) { mtName = string.Format("{0}{1}{2}{3}", mtName.Substring(0, mtName.Length - 1), "_", numberedTagNo.ToString(), "*"); } ReplacerFunctions.Add(mtName, new MetaTagReplacerFunction(new string[] { "", "System.String", "Dictionary`2", "Dictionary`2" }, null, null, (MetaTagReplacerInputDates inDates, object[] in_args) => { if (in_args != null) { if (in_args.Length == 4) { object moduleToUpdate = in_args[0]; string modulePropertyToUpdate = in_args[1] as string; Dictionary<string, object> moduleEventDonors = in_args[2] as Dictionary<string, object>; Dictionary<string, EventHandler> moduleEventConsumers = in_args[3] as Dictionary<string, EventHandler>; object moduleEventDonor = null; if (moduleToUpdate != null && modulePropertyToUpdate != null && moduleEventDonors != null) { if (moduleEventDonors.ContainsKey(mtName)) moduleEventDonor = moduleEventDonors[mtName]; else { moduleEventDonor = moduleToUpdate; moduleEventDonors.Add(mtName, moduleEventDonor); } EventHandler eh = null; string eventConsumerKey = string.Format("{0}_{1}_{2}_{3}", mtName, moduleToUpdate.GetType().Name, moduleToUpdate.GetHashCode().ToString(), modulePropertyToUpdate); if (moduleEventConsumers.ContainsKey(eventConsumerKey)) eh = moduleEventConsumers[eventConsumerKey]; else { string moduleOutputMember = string.Empty; string moduleOutputXmlNode = string.Empty; int moduleOutputXmlDepthLevel = 0; string delim = ","; string wrapWith = string.Empty; Dictionary<string, string> moduleEvenDonorFuncParms = GetMTFuncParmsFromObjectAhcnorProp(moduleEventDonor, "MetaTagsAnchor", mtName); if (!string.IsNullOrEmpty(moduleOutputMemberNameParm)) { if (moduleEvenDonorFuncParms.ContainsKey(moduleOutputMemberNameParm)) moduleOutputMember = moduleEvenDonorFuncParms[moduleOutputMemberNameParm]; } if (!string.IsNullOrEmpty(moduleOutputXmlLevelParm)) { if (moduleEvenDonorFuncParms.ContainsKey(moduleOutputXmlLevelParm)) if (!int.TryParse(moduleEvenDonorFuncParms[moduleOutputXmlLevelParm], out moduleOutputXmlDepthLevel)) moduleOutputXmlDepthLevel = 0; } if (!string.IsNullOrEmpty(moduleOutputXmlNodeNameParm)) { if (moduleEvenDonorFuncParms.ContainsKey(moduleOutputXmlNodeNameParm)) moduleOutputXmlNode = moduleEvenDonorFuncParms[moduleOutputXmlNodeNameParm]; } if (!string.IsNullOrEmpty(moduleOutputDelimiterParm)) { if (moduleEvenDonorFuncParms.ContainsKey(moduleOutputDelimiterParm)) delim = moduleEvenDonorFuncParms[moduleOutputDelimiterParm]; } if (!string.IsNullOrEmpty(moduleOutputWrapperParm)) { if (moduleEvenDonorFuncParms.ContainsKey(moduleOutputWrapperParm)) wrapWith = moduleEvenDonorFuncParms[moduleOutputWrapperParm]; } // eh = new EventHandler( (object sender, EventArgs e) => { System.ComponentModel.RefreshEventArgs args = e as System.ComponentModel.RefreshEventArgs; if (args != null) { KeyValuePair<string, int> outputKeyAndIndex = (KeyValuePair<string, int>)args.ComponentChanged; string outputDataKey = outputKeyAndIndex.Key; int outputDataIndex = outputKeyAndIndex.Value; if ( (!string.IsNullOrEmpty(moduleOutputMember) && moduleOutputMember.Equals(outputDataKey)) || (string.IsNullOrEmpty(moduleOutputMember) && outputDataIndex.Equals(1)) ) { System.Collections.Hashtable output_data = GetObjectPropertyValue(new object[] { sender }, "OutputData") as System.Collections.Hashtable; if (output_data.ContainsKey(outputDataKey)) { System.Xml.XmlDocument docXml = output_data[outputDataKey] as System.Xml.XmlDocument; System.Xml.XmlNode startNode = docXml.FirstChild; for (int i = 0; i < moduleOutputXmlDepthLevel; i++) startNode = startNode.FirstChild; List<System.Xml.XmlNode> xmlNodes = new List<System.Xml.XmlNode>(startNode.ChildNodes.Cast<System.Xml.XmlNode>()); if (docXml != null) { string xmlNodeDelimList = (from node in xmlNodes select new { v = node[moduleOutputXmlNode].InnerText }).Distinct().Aggregate(new StringBuilder(), (sb, val) => sb.AppendFormat("{0}{1}{0}{2}", wrapWith, val.v, delim), sb => { if (sb.Length > 0) sb.Length--; return sb.ToString(); }); ReplaceMetaTagInObjectPropertyValue(new object[] { moduleToUpdate }, modulePropertyToUpdate, mtName, xmlNodeDelimList); } } } } }); if (eh != null) moduleEventConsumers.Add(eventConsumerKey, eh); } if (eh != null) { UnsubscribeFromObjectEvent(new object[] { moduleEventDonor }, "ModuleOutputAdded", eh); SubscribeToObjectEvent(new object[] { moduleEventDonor }, "ModuleOutputAdded", eh); } } } } return null; } ) ); } } } } }
/// <summary> /// Sets module override MetaTagReplacerInputDates /// </summary> /// <param name="module"></param> /// <param name="properties_original_values"></param> public void SetModuleInDatesOverride(string module, MetaTagReplacerInputDates override_in_dates) { if (!_module_override_parameters.ContainsKey(module)) _module_override_parameters.Add(module, new PropertyBag()); if (_module_override_parameters[module].Contains(datesOverridesKey)) _module_override_parameters[module][datesOverridesKey] = override_in_dates; else _module_override_parameters[module].Add(datesOverridesKey, override_in_dates); }
public override StateEnums.Status Execute(Object o, Hashtable inputData) { log.InfoFormat(((State)o).CurrentJobHash, "Module {0} received {1} inputs.", name, inputData != null ? inputData.Count.ToString() : "no"); StateEnums.Status retval = StateEnums.Status.Success; if (IfResetOnEachRun) ResetOutput(); PassAlongOutputs(inputData); PerfTimer hpt = new PerfTimer(); hpt.Start(); int activityIdStarting = UpdateProcessStatus(Enums.ProcessStatus.Starting, "", (State)o); string reportArgs = String.Empty; try { //runDate is required to evaluate meta tags this module explicitely using //derrived from job state or passed from runDateString //if (DateTime.TryParse(RunDate, out runDate)) // runDateParms = new MetaTagReplacerInputDates(runDate); //else // runDateParms = ((State)this.ExecutionEngine.GetJobState(this.job.JobName)).CurrentParameters.ProcessInputDates; runDateParms = ((State)o).CurrentParameters.GetModuleInDatesOverride(this.Name); if (runDateParms == null) runDateParms = ((State)o).CurrentParameters.ProcessInputDates; string mail_subject = string.Format("GENEVA CONNECTION ERROR [{0}])", MetaTagReplacer.GetMetaTagValue("*RUN_INSTANCE*", runDateParms, null)); string mail_distribution = MetaTagReplacer.GetMetaTagValue("*SOAP_ERROR_DISTRIBUTION*", runDateParms, null); dtOut = new DataTable(tableName); Util.InitializeAsciiLookup(); ifRetry = true; bool isContingency = false; string runrepSessionID = String.Empty; reportResultsPortfolioStruct results = null; Service webServ = null; int numtries = 1; DateTime runStartTime = DateTime.Now; string msgMailer = ""; string localArgs = ParseArgs(args); while (ifRetry) { try { webServ = null; log.InfoFormat(((State)o).CurrentJobHash, "Attempting Geneva SOAP RunCallableRunrepRunReport [retry #{3}] on {0} with report args {1} {2}", url, rsl, localArgs, numtries); webServ = new Service(); if (webServ == null) { string msg = "Web service not constructed (null value)."; throw new ArgumentNullException(msg); } log.InfoFormat(((State)o).CurrentJobHash, "Web service constructed successfully"); webServ.Timeout = timeout; log.InfoFormat(((State)o).CurrentJobHash, "Web service timeout assigned: {0}", timeout); /* Old style processing of properties webServ.Url = ConfigUtils.MetaTagReplacer(url); */ webServ.Url = this.Url; log.InfoFormat(((State)o).CurrentJobHash, "Web url assigned: {0}", webServ.Url); if (runrepSessionID.Equals(String.Empty)) { //string flags = String.Format("-n{0} -f empty", DateTime.Now.ToString("mmss")); string flags = String.Format("-f empty"); try { runrepSessionID = webServ.StartCallableRunrep(port, host, uid, pwd, flags); //runrepSessionID = webServ.StartPersistentCallableRunrep(port, host, uid, pwd, flags); log.InfoFormat(((State)o).CurrentJobHash, "Runrep session started successfully with flags \"{0}\" [Runrep ID: {1}]", flags, runrepSessionID); log.DebugFormat(((State)o).CurrentJobHash, "Runrep session {0} started successfully for call {1} {2} {3} {4}", runrepSessionID, webServ.Url, rsl, localArgs, flags ); } catch (Exception ex) { log.DebugFormat(((State)o).CurrentJobHash, "Runrep session {0} NOT started successfully for call {1} {2} {3} {4}", runrepSessionID, webServ.Url, rsl, localArgs, flags); string msg = String.Format("Runrep session NOT started with flags \"{0}\" [Runrep ID: {1}]<br>Error: {2}", flags, runrepSessionID, ex); throw new ApplicationException(msg); } } try { webServ.RunCallableRunrepReadFile(runrepSessionID, rsl); log.InfoFormat(((State)o).CurrentJobHash, "Runrep read report file {0} was successful [Runrep ID: {1}]", rsl, runrepSessionID); } catch (Exception ex) { string msg = String.Format("Runrep read report file {0} was NOT successful [Runrep ID: {1}]<br>Error: {2}", rsl, runrepSessionID, ex); throw new ApplicationException(msg); } try { results = webServ.RunCallableRunrepRunReport(runrepSessionID, rsl, localArgs); log.InfoFormat(((State)o).CurrentJobHash, "Runrep run report {0} {1} was successful [Runrep ID: {2}]", rsl, localArgs, runrepSessionID); } catch (Exception ex) { string msg = String.Format("Runrep run report {0} {1} was successful [Runrep ID: {2}]<br>Error: {3}", rsl, localArgs, runrepSessionID, ex); throw new ApplicationException(msg); } try { webServ.ShutdownCallableSession(runrepSessionID); log.InfoFormat(((State)o).CurrentJobHash, "Shut down SOAP session OK. [Runrep ID: {0}]", runrepSessionID); } catch (Exception ex) { log.WarnFormat(((State)o).CurrentJobHash, "Error shutting down SOAP session: {0} [Runrep ID: {1}]", ex, runrepSessionID); } if (isContingency) { string msg = "Geneva SOAP call succeeded."; msgMailer += String.Format("{0}<br><br>", msg); ///TO DO Replace this code with more central approach //string subject = ConfigUtils.MetaTagReplacer("GENEVA CONNECTION ERROR [*RUN_INSTANCE*])"); //SendMailMessage(((State)o).CurrentJobHash, ConfigUtils.MetaTagReplacer("*SOAP_ERROR_DISTRIBUTION*"), "BITS,[email protected]", subject, msgMailer); SendMailMessage(((State)o).CurrentJobHash, mail_distribution, "BITS,[email protected]", mail_subject, msgMailer); } ifRetry = false; isContingency = false; } catch (Exception ex) { string msg = ex.ToString(); log.ErrorFormat(((State)o).CurrentJobHash, "{0}", msg); msgMailer += String.Format("{0}<br><br>", msg); try { webServ.ShutdownCallableSession(runrepSessionID); log.InfoFormat(((State)o).CurrentJobHash, "Shut down SOAP session OK {0}.", runrepSessionID); } catch (Exception exx) { log.WarnFormat(((State)o).CurrentJobHash, "Error shutting down SOAP session: {0} [Runrep ID: {1}]", exx, runrepSessionID); } try { if (isContingency == false) { isContingency = true; runStartTime = DateTime.Now; string m = String.Format("***** Contingency Condition ***** <br>Contingency mode started at: {0}", runStartTime); msgMailer += String.Format("{0}<br><br>", m); log.ErrorFormat(((State)o).CurrentJobHash, m); } ifRetry = ifOverrideSoapErrorRetry ? false : true; ifRetry = ++numtries > numberOfRetryConnection ? false : true; if (ifRetry) { runrepSessionID = String.Empty; string m = "<br><br>Retrying in one minute.<br><br>"; msgMailer += String.Format("{0}<br><br>", m); log.ErrorFormat(((State)o).CurrentJobHash, m); } else { string m = "Allowable number of attempts exceeded. Giving up now."; msgMailer += String.Format("{0}<br><br>", m); log.ErrorFormat(((State)o).CurrentJobHash, m); retval = StateEnums.Status.Warnings; } ///TO DO Replace this code with more central approach //string subject = ConfigUtils.MetaTagReplacer("GENEVA CONNECTION ERROR [*RUN_INSTANCE*])"); //SendMailMessage(((State)o).CurrentJobHash, ConfigUtils.MetaTagReplacer("*SOAP_ERROR_DISTRIBUTION*"), "BITS,[email protected]", subject, msgMailer); SendMailMessage(((State)o).CurrentJobHash, mail_distribution, "BITS,[email protected]", mail_subject, msgMailer); Thread.Sleep(60000); } catch (Exception exfinal) { log.ErrorFormat(((State)o).CurrentJobHash, "Fatal error: {0}", exfinal); retval = StateEnums.Status.Error; } } } if (results != null) { if (!ifUseAddendumErrorsOnly) dtOut = ConvertToDataTable(tableName, results); else dtOut = getAddendumErrors(tableName, results); } if (dtOut != null && dtOut.Rows.Count > 0) log.InfoFormat(((State)o).CurrentJobHash, "Obtained {0} records from SOAP service call.", dtOut.Rows.Count); else log.InfoFormat(((State)o).CurrentJobHash, "Obtained no records from SOAP service call."); if (dtOut != null) { AddToOutputs(this.Name, dtOut); AddToOutputs("RunDate", RunDate); } } catch (Exception ex) { log.Error(((State)o).CurrentJobHash, ex); retval = StateEnums.Status.Error; } hpt.Stop(); int activityIdEnding = UpdateProcessStatus(Enums.ProcessStatus.Success, "", (State)o); log.InfoFormat(((State)o).CurrentJobHash, "GenevaSoapReport [{0}] completed in {1} seconds.", Name, hpt.Duration); return retval; }