private bool PollAndHandleMessage(Task[] tasks, int availableIx, Task availableTask) { CloudQueueMessage message; QueueEnvelope envelope = QueueSupport.GetFromDefaultQueue(out message); if (envelope != null) { Task executing = Task.Factory.StartNew(() => WorkerSupport.ProcessMessage(envelope)); tasks[availableIx] = executing; QueueSupport.CurrDefaultQueue.DeleteMessage(message); if (availableTask.Exception != null) { ErrorSupport.ReportException(availableTask.Exception); } return(true); } else { if (message != null) { QueueSupport.CurrDefaultQueue.DeleteMessage(message); ErrorSupport.ReportMessageError(message); } GC.Collect(); return(false); } }
private bool PollAndHandleSubscriptionChain(Task[] tasks, int availableIx, Task availableTask) { var result = SubscribeSupport.GetOwnerChainsInOrderOfSubmission(); if (result.Length == 0) { return(false); } string acquiredEtag = null; var firstLockedOwner = result.FirstOrDefault( lockCandidate => SubscribeSupport.AcquireChainLock(lockCandidate, out acquiredEtag)); if (firstLockedOwner == null) { return(false); } var executing = Task.Factory.StartNew( () => WorkerSupport.ProcessOwnerSubscriptionChains(firstLockedOwner, acquiredEtag, CURRENT_HARDCODED_CONTAINER_NAME)); tasks[availableIx] = executing; if (availableTask.Exception != null) { ErrorSupport.ReportException(availableTask.Exception); } return(true); }
//Validate Inputs Both InputColumns and ExternalColumns private DTSValidationStatus ValidateInputs() { bool cancel; //Component should have a single input if (this.ComponentMetaData.InputCollection.Count != 1) { ErrorSupport.FireErrorWithArgs(HResults.DTS_E_INCORRECTEXACTNUMBEROFINPUTS, out cancel, 1); return(DTSValidationStatus.VS_ISCORRUPT); } //Check input has columns var input = ComponentMetaData.InputCollection[INPUT_NAME]; if (input.ExternalMetadataColumnCollection.Count == 0) { return(DTSValidationStatus.VS_NEEDSNEWMETADATA); } //Check input columns are valid if (!this.ComponentMetaData.AreInputColumnsValid) { input.InputColumnCollection.RemoveAll(); return(DTSValidationStatus.VS_NEEDSNEWMETADATA); } //Input truncation disposition not supported if (input.TruncationRowDisposition != DTSRowDisposition.RD_NotUsed) { ErrorSupport.FireError(HResults.DTS_E_ADODESTINPUTTRUNDISPNOTSUPPORTED, out cancel); return(DTSValidationStatus.VS_ISBROKEN); } return(DTSValidationStatus.VS_ISVALID); }
private static void ProcessErrors(bool useWorker) { if (useWorker) { List <QueueEnvelope> envelopes = new List <QueueEnvelope>(); List <CloudQueueMessage> messages = new List <CloudQueueMessage>(); CloudQueueMessage message = null; QueueEnvelope envelope = ErrorSupport.RetrieveRetryableEnvelope(out message); while (envelope != null) { //WorkerSupport.ProcessMessage(envelope, false); //QueueSupport.CurrErrorQueue.DeleteMessage(message); messages.Add(message); envelope.CurrentRetryCount++; envelopes.Add(envelope); envelope = ErrorSupport.RetrieveRetryableEnvelope(out message); } envelopes.ForEach(QueueSupport.PutToDefaultQueue); messages.ForEach(msg => QueueSupport.CurrErrorQueue.DeleteMessage(msg)); } else { CloudQueueMessage message = null; QueueEnvelope envelope = ErrorSupport.RetrieveRetryableEnvelope(out message); while (envelope != null) { WorkerSupport.ProcessMessage(envelope, false); QueueSupport.CurrErrorQueue.DeleteMessage(message); envelope = ErrorSupport.RetrieveRetryableEnvelope(out message); } } }
//================================================================================================= private ManagementObjectCollection ExecWQL(bool bOnlyMetadata) { if (m_scope == null) { bool bCancel; ErrorSupport.FireError(HResults.DTS_E_CONNECTIONREQUIREDFORMETADATA, out bCancel); throw new PipelineComponentHResultException(HResults.DTS_E_CONNECTIONREQUIREDFORMETADATA); } string wqlQuery = GetWqlString(); try { ObjectQuery objectQuery = new ObjectQuery(wqlQuery); ManagementObjectSearcher searcher = new ManagementObjectSearcher(m_scope, objectQuery); searcher.Options.PrototypeOnly = bOnlyMetadata; ManagementObjectCollection collection = searcher.Get(); try { int count = collection.Count; } catch (ManagementException e) { if (e.ErrorCode == ManagementStatus.InvalidClass || e.ErrorCode == ManagementStatus.NotFound) { throw new Exception("Invalid WQL query"); } else { throw; } } return(collection); } catch (Exception e) { bool bCancel; ComponentMetaData.FireError( HResults.DTS_E_ERROROCCURREDWITHFOLLOWINGMESSAGE, ComponentMetaData.Name, "A exception occurred while executing WQL query \"" + wqlQuery + "\". Exception message : " + e.Message, "", 0, out bCancel); throw new PipelineComponentHResultException(e.Message, HResults.DTS_E_ERROROCCURREDWITHFOLLOWINGMESSAGE); } }
public override void PrimeOutput(int outputs, int[] outputIDs, PipelineBuffer[] buffers) { IDTSOutput100 output = ComponentMetaData.OutputCollection[0]; PipelineBuffer buffer = buffers[0]; try { foreach (ManagementObject row in ExecWQL(false)) { object[] row_values = new object[m_columnInfo.Count]; int i = 0; foreach (ColumnInfo ci in m_columnInfo) { PropertyData wmiColumn = row.Properties[ci.ColumnName]; row_values[i++] = row[wmiColumn.Name]; } // lets unwind wmi row ( unwind array in any column ) // we use the same approach to handle WMI arrays as WMI ODBC driver // for details see msdn : // http://msdn.microsoft.com/en-us/library/aa392328(VS.85).aspx#_hmm_mapping_wmi_arrays_to_odbc foreach (object[] unwinded_row in RowUnwinder.UnwindRow(row_values)) { buffer.AddRow(); i = 0; foreach (ColumnInfo ci in m_columnInfo) { PropertyData wmiColumn = row.Properties[ci.ColumnName]; SetBufferColumn(buffer, row, wmiColumn, ci.BufferColumnIndex, unwinded_row[i++]); } } } // set end of data on all of the buffers buffer.SetEndOfRowset(); } catch (Exception e) { bool bCancel; ErrorSupport.FireErrorWithArgs(HResults.DTS_E_PRIMEOUTPUTFAILED, out bCancel, ComponentMetaData.IdentificationString, e.Message); throw new PipelineComponentHResultException(HResults.DTS_E_PRIMEOUTPUTFAILED); } }
//================================================================================================= public override void ReinitializeMetaData() { // baseclass may have some work to do here base.ReinitializeMetaData(); IDTSOutput100 output = ComponentMetaData.OutputCollection[0]; output.OutputColumnCollection.RemoveAll(); output.ExternalMetadataColumnCollection.RemoveAll(); // Execute the WQL query and retrieve metadata foreach (PropertyData wmiColumn in GetWmiColumns()) { DataType dtsType; int length; int scale; int precision; int codePage; GetWmiColumnProperties(wmiColumn, out dtsType, out length, out scale, out precision, out codePage); string Description = (string)GetQualifiers(wmiColumn, "Description", ""); // create a new column IDTSOutputColumn100 outputcolNew = output.OutputColumnCollection.New(); if (string.IsNullOrEmpty(wmiColumn.Name)) { bool bCancel; ErrorSupport.FireError(HResults.DTS_E_DATASOURCECOLUMNWITHNONAMEFOUND, out bCancel); throw new PipelineComponentHResultException(HResults.DTS_E_DATASOURCECOLUMNWITHNONAMEFOUND); } outputcolNew.Name = wmiColumn.Name; outputcolNew.SetDataTypeProperties(dtsType, length, precision, scale, codePage); outputcolNew.Description = Description; CreateExternalMetaDataColumn(output.ExternalMetadataColumnCollection, outputcolNew); } // Exclusion Group output.ExclusionGroup = 0; // Synchronous Input output.SynchronousInputID = 0; }
public override void AcquireConnections(object transaction) { if (m_scope != null && m_scope.IsConnected) { bool bCancel; ErrorSupport.FireError(HResults.DTS_E_ALREADYCONNECTED, out bCancel); throw new PipelineComponentHResultException(HResults.DTS_E_ALREADYCONNECTED); } IDTSRuntimeConnection100 conn; try { // get the runtime connection conn = ComponentMetaData.RuntimeConnectionCollection[CONNECTION_NAME]; } catch (Exception) { bool bCancel; ErrorSupport.FireErrorWithArgs(HResults.DTS_E_CANNOTTFINDRUNTIMECONNECTIONOBJECT, out bCancel, CONNECTION_NAME); throw new PipelineComponentHResultException(HResults.DTS_E_CANNOTTFINDRUNTIMECONNECTIONOBJECT); } // get the connection manager from the connection IDTSConnectionManager100 conn_mgr = conn.ConnectionManager; if (conn_mgr == null) { bool bCancel; ErrorSupport.FireErrorWithArgs(HResults.DTS_E_CONNECTIONMANANGERNOTASSIGNED, out bCancel, CONNECTION_NAME); throw new PipelineComponentHResultException(HResults.DTS_E_CONNECTIONMANANGERNOTASSIGNED); } m_scope = (conn_mgr.AcquireConnection(transaction)) as ManagementScope; if (m_scope == null) { bool bCancel; ErrorSupport.FireError(HResults.DTS_E_CANNOTACQUIREMANAGEDCONNECTIONFROMCONNECTIONMANAGER, out bCancel); throw new PipelineComponentHResultException(HResults.DTS_E_CANNOTACQUIREMANAGEDCONNECTIONFROMCONNECTIONMANAGER); } }
private void RefreshStockCompanyData() { InformationContext.Current.InitializeCloudStorageAccess(CURRENT_HARDCODED_CONTAINER_NAME); var oldDay = -1; while (true) { var refreshCharts = oldDay != DateTime.Now.DayOfYear || oldDay == -1; oldDay = DateTime.Now.Day; foreach (var groupId in TBRGroupRoot.GetAllGroupIDs()) { var group = TBRGroupRoot.RetrieveFromDefaultLocation(groupId).Group; var titanLock = GetLock(group); if (titanLock.IsLocked && titanLock.LastLocked > DateTime.Now.AddHours(-1) || titanLock.LastLocked > DateTime.Now.AddMinutes(-5)) { continue; } try { titanLock.IsLocked = true; titanLock.LastLocked = DateTime.Now; titanLock.StoreInformation(); RefreshStockCompaniesOfOwner(group); if (refreshCharts) { UpdateChartsOfOwner(group); } } catch (Exception ex) { ErrorSupport.ReportException(ex); } finally { titanLock.IsLocked = false; titanLock.StoreInformation(); } } Thread.Sleep(60000); } }
public override void Run() { GracefullyStopped = false; //ThreadPool.SetMinThreads(3, 3); Task[] tasks = new Task[] { Task.Factory.StartNew(() => { }), Task.Factory.StartNew(() => { }), Task.Factory.StartNew(() => { }), //Task.Factory.StartNew(() => {}), //Task.Factory.StartNew(() => {}), //Task.Factory.StartNew(() => {}), }; QueueSupport.ReportStatistics("Starting worker: " + CurrWorkerID, TimeSpan.FromDays(1)); Task.Factory.StartNew(RefreshStockCompanyData); while (!IsStopped) { try { Task.WaitAny(tasks); if (IsStopped) { break; } int availableIx; Task availableTask = WorkerSupport.GetFirstCompleted(tasks, out availableIx); bool handledSubscriptionChain = PollAndHandleSubscriptionChain(tasks, availableIx, availableTask); if (handledSubscriptionChain) { // TODO: Fix return value check Thread.Sleep(1000); continue; } bool handledMessage = PollAndHandleMessage(tasks, availableIx, availableTask); if (handledMessage) { continue; } Thread.Sleep(1000); } catch (AggregateException ae) { foreach (var e in ae.Flatten().InnerExceptions) { ErrorSupport.ReportException(e); } Thread.Sleep(10000); // or ... // ae.Flatten().Handle((ex) => ex is MyCustomException); } /* * catch (MessagingException e) * { * if (!e.IsTransient) * { * Trace.WriteLine(e.Message); * throw; * } * Thread.Sleep(10000); * }*/ catch (OperationCanceledException e) { if (!IsStopped) { Trace.WriteLine(e.Message); throw; } } catch (Exception ex) { ErrorSupport.ReportException(ex); throw; } } Task.WaitAll(tasks); foreach (var task in tasks.Where(task => task.Exception != null)) { ErrorSupport.ReportException(task.Exception); } QueueSupport.ReportStatistics("Stopped: " + CurrWorkerID, TimeSpan.FromDays(1)); GracefullyStopped = true; }
private static void RunTaskedQueueWorker() { Task[] tasks = new Task[] { Task.Factory.StartNew(() => {}), Task.Factory.StartNew(() => {}), Task.Factory.StartNew(() => {}), //Task.Factory.StartNew(() => {}), //Task.Factory.StartNew(() => {}), }; bool IsStopped = false; int initialCount = 0; while (true) { try { Task.WaitAny(tasks); if (IsStopped) { Task.WaitAll(tasks); foreach (var task in tasks) { if (task.Exception != null) { ErrorSupport.ReportException(task.Exception); } } break; } int availableIx; Task availableTask = WorkerSupport.GetFirstCompleted(tasks, out availableIx); CloudQueueMessage message; QueueEnvelope envelope = QueueSupport.GetFromDefaultQueue(out message); if (envelope != null) { Task executing = Task.Factory.StartNew(() => WorkerSupport.ProcessMessage(envelope)); tasks[availableIx] = executing; QueueSupport.CurrDefaultQueue.DeleteMessage(message); if (availableTask.Exception != null) { ErrorSupport.ReportException(availableTask.Exception); } } else { if (message != null) { QueueSupport.CurrDefaultQueue.DeleteMessage(message); ErrorSupport.ReportMessageError(message); } Thread.Sleep(1000); } } catch (AggregateException ae) { foreach (var e in ae.Flatten().InnerExceptions) { ErrorSupport.ReportException(e); } Thread.Sleep(10000); // or ... // ae.Flatten().Handle((ex) => ex is MyCustomException); } /* * catch (MessagingException e) * { * if (!e.IsTransient) * { * Trace.WriteLine(e.Message); * throw; * } * Thread.Sleep(10000); * }*/ catch (OperationCanceledException e) { if (!IsStopped) { Trace.WriteLine(e.Message); throw; } } catch (Exception ex) { ErrorSupport.ReportException(ex); throw; } } }