Exemplo n.º 1
0
        /// <summary>
        /// Both the student and the opp will be locked before processing a test.
        /// If either is already locked, the test will left in the queue for QA to
        /// pick up again the next time it hits the xml repository.  So we will only
        /// process 1 test per student at a time.  If another opp comes in for the
        /// same student, it will be left in the queue.  It's possible that the same opp
        /// can come in for a different student in the case of a reassignment.  This is why
        /// we're locking on both SSID and OppID.  If the student is not locked but the opp is,
        /// the test will be left in the queue.
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        private bool LockOpp(XmlRepositoryItem file)
        {
            bool locked = false;

            lock (oppLock)
            {
                // see if we can lock the student.  If the SSID is empty (can happen with
                //  paper test discreps), then we'll skip locking on the student.
                locked = lockedStudents.Add(file.TesteeKey);
                if (locked)
                {
                    // we were able to lock the student (or the SSID was null/blank).
                    //  Make sure we can also lock the opp.  For a reassignment, the opp could be locked
                    //  by another student.
                    locked = lockedOpps.Add(file.OppID);
                    // if we weren't able to lock the opp, then unlock the student that we just
                    //  locked above.
                    if (!locked)
                    {
                        lockedStudents.Remove(file.TesteeKey);
                    }
                }
            }
            return(locked);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Unlock both the student and the opp.  When we process a test,
 /// both the student and the opp will be locked.  If either one cannot be locked,
 /// the opp will be left in the queue and picked up again on the next pass.  This
 /// will continue until both can be locked.
 /// </summary>
 /// <param name="file"></param>
 public void UnlockOpp(XmlRepositoryItem file)
 {
     lock (oppLock)
     {
         // first unlock the opp
         if (!lockedOpps.Remove(file.OppID))
         {
             throw new ApplicationException(String.Format("Could not unlock opp: {0}.  It's not locked!.", file.OppID));
         }
         // then unlock the student.
         if (!lockedStudents.Remove(file.TesteeKey))
         {
             throw new ApplicationException(String.Format("Could not unlock student: {0}, but was able to unlock opp: {1}!.", file.TesteeKey, file.OppID));
         }
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Checks for generalFiles in a directory and populate the queue,
        /// then assign a file for processing to a worker thread.
        /// If the queue becomes empty, check again and give more work to worker threads.
        /// </summary>
        public void DoWork()
        {
            //@rem Regex regularExpression = new Regex(fileRegexPattern);

            while (!stopSignalled || (generalThreadCounter > 0 && !StopTimeoutElapsed)) // if signalled to stop, let the worker threads finish until we hit a timeout threshold
            {
                // if the settings say to send an email alert for warnings then we are not supposd to send a summary email as well.
                // If we are not supposed to send email alerts for warnings then we are supposed to send only a summary email
                if (!QASystemConfigSettings.Instance.EmailAlertForWarnings)
                { //Check if the current time is within `margin` of `timeToSendWarnings`. If it is, then send an email
                    // with a summary of all warnings since this time yesterday.
                    TimeSpan difference;
                    TimeSpan currentTime = TimeSpan.FromHours(DateTime.Now.Hour) + TimeSpan.FromMinutes(DateTime.Now.Minute) + TimeSpan.FromSeconds(DateTime.Now.Second);
                    if (currentTime <= timeToSendWarnings) // don't want negative values
                    {
                        difference = timeToSendWarnings.Subtract(currentTime);
                    }
                    else
                    {
                        difference = currentTime.Subtract(timeToSendWarnings);
                    }
                    if (difference <= margin && !sentWarnings)
                    {
                        try
                        {
                            Utilities.Logger.SendWarningSummaryEmail(DateTime.Now - TimeSpan.FromDays(1.0));
                        }
                        catch (Exception e)
                        {
                            Utilities.Logger.Log(true, string.Format("Exception occurred while sending warning summary email. Message: {0} {1} StackTrace: {2}", e.Message, Environment.NewLine, e.StackTrace), EventLogEntryType.Error, true, true);
                        }
                        sentWarnings = true;
                    }
                    else if (difference > margin)
                    {
                        sentWarnings = false;
                    }
                }

                if (!stopSignalled)
                {
                    if (XMLRepositoryQ.Count > 0 && generalThreadCounter < numberOfGeneralThreads)
                    {
                        // Get a file to process from the queue...
                        XmlRepositoryItem repositoryItem = XMLRepositoryQ.Dequeue();

                        // try to lock the opp.  If it's already locked, then move
                        //  on to the next file leaving this one in the repository to be
                        //  picked up next time.
                        if (!LockOpp(repositoryItem))
                        {
                            Utilities.Logger.Log(true, String.Format("FileID: {0}, OppID: {1}, TesteeKey: {2} could not be locked.  Will try again later...", repositoryItem.FileID, repositoryItem.OppID, repositoryItem.TesteeKey), EventLogEntryType.Information, false, true);
                            continue;
                        }

                        try
                        {
                            QASystemWorkerThread worker = new QASystemWorkerThread(repositoryItem);
                            Thread workerThread         = new Thread(new ParameterizedThreadStart(worker.ProcessXmlFile));
                            workerThread.Name = "process XML file";
                            workerThread.Start(this);

                            // Increment the active thread count...
                            generalThreadCounter++;
                            activeThreadCount.Increment();
                        }
                        catch (Exception e)
                        {
                            Utilities.Logger.Log(true, "Error starting worker thread: " + e.StackTrace, EventLogEntryType.Error, false, true);
                            continue;
                        }
                    }
                    else if (XMLRepositoryQ.Count == 0) // If the file queues are empty...
                    {
                        try
                        {
                            XMLRepositoryQ = xmlRepositoryBL.GetXMLRepository(ServiceName);
                        }
                        catch (Exception ex)
                        {
                            Utilities.Logger.Log(true, String.Format("Fatal Exception encountered while reading XmlRepository: {0}, Stack Trace: {1}", ex.Message, ex.StackTrace), EventLogEntryType.Error, true, true);
                            throw ex;
                        }

                        if (XMLRepositoryQ.Count == 0)
                        {
                            Thread.Sleep(idleSleepTime);
                        }
                    }
                }
                Thread.Sleep(loopSleepTime);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="xml"></param>
        /// <param name="senderType"></param>
        /// <param name="fileID"></param>
        /// <param name="updateRB"></param>
        /// <param name="updateDoR"></param>
        /// <param name="sendToHandScoring"></param>
        /// <param name="ignoreHandscoringDuplicates"></param>
        /// <param name="emailAlertForWarnings"></param>
        /// <returns></returns>
        public QAResult ReceiveTestResult(XmlDocument xml, XmlRepositoryItem xmlRepoItem)
        {
            DateTime        startDateTime = DateTime.Now;
            QAResult        result        = QAResult.Success;
            ArchiveDB       ar            = new ArchiveDB();
            TestResult      tr            = null;
            bool            isFatal       = false;
            bool            isValid       = false;
            XMLAdapter      adapter;
            SendToModifiers sendToModifiers = null;
            long?           archivedFileID  = null;
            long?           dorRecordId     = null;
            // these config settings may be overridden or modified below for this test.
            ProjectMetaData              projectMetaData   = new ProjectMetaData();
            ITISExtender                 tisExtender       = ServiceLocator.Resolve <ITISExtender>() ?? new NullTISExtender();
            ITISExtenderState            tisExtenderState  = tisExtender.CreateStateContainer();
            ITestResultSerializerFactory serializerFactory = ServiceLocator.Resolve <ITestResultSerializerFactory>();

            if (serializerFactory == null)
            {
                throw new ApplicationException("Must register an ITestResultSerializerFactory with the ServiceLocator.");
            }

            try
            {
                xmlRepositoryBL = new BL.XmlRepository(QASystemConfigSettings.Instance.LongDbCommandTimeout);
                adapter         = serializerFactory.CreateDeserializer(xml);
                tr = adapter.CreateTestResult(_tc, out isValid, true);
                if (tr != null)
                {
                    // set the project metadata based on the current project (set in the parser)
                    projectMetaData.SetProjectMetaData(tr.ProjectID);

                    // if the opp is null, we can't process the file
                    if (tr.Opportunity == null)
                    {
                        result = QAResult.FailValidation;
                        ar.SaveResultExceptions(tr, adapter.ValidationRecords, xmlRepoItem.FileID);
                        return(result);
                    }

                    //merge item scores from outside vendor if necessary
                    bool qaProjectChanged = false;
                    if (projectMetaData.mergeItemScores && adapter.MergeScores(tr, out qaProjectChanged))
                    {
                        SetArchiveStrategy(ArchiveStrategy.ArchiveAndInsert);
                    }

                    // if the project changed, refresh the metadata
                    if (qaProjectChanged)
                    {
                        projectMetaData.SetProjectMetaData(tr.ProjectID);
                    }

                    //PRESCORE:
                    tisExtender.PreScore(this, tr, xmlRepoItem, projectMetaData, tisExtenderState);

                    //SCORE:
                    if (tisExtender.ShouldScore(this, adapter, tr, projectMetaData, tisExtenderState))
                    {
                        if (tr.AddScores(_tc))
                        {
                            SetArchiveStrategy(ArchiveStrategy.ArchiveAndInsert);
                        }
                    }

                    //COMPLETENESS STATUS:
                    tr.Opportunity.Completeness = !tr.IsComplete() ? "Partial" : "Complete";

                    // Validation section. This checks the xml file against several business rules.
                    // If validation fails, the file is moved to a failed files directory.

                    List <ValidationRecord> vrs = new List <ValidationRecord>();

                    tr.ValidationRecords = adapter.ValidationRecords;
                    if (tr.ValidationRecords.Count == 0)
                    {
                        //POSTSCORE:
                        tisExtender.PostScore(this, tr, xmlRepoItem, projectMetaData, tisExtenderState);

                        // if the startdate is null in the file, it will be set to the statusDate.
                        //  Make sure we archive it so that we have a record of the unmodified file.
                        if (tr.Opportunity.OriginalStartDate == DateTime.MinValue)
                        {
                            SetArchiveStrategy(ArchiveStrategy.ArchiveAndInsert);
                        }

                        switch (archiveStrategy)
                        {
                        case ArchiveStrategy.ArchiveAndInsert:
                            //Change the status of old XML file to Arcvhive
                            //insert the new XML file with status as Pprocessing
                            //get the new File id
                            archivedFileID     = xmlRepoItem.FileID;
                            xmlRepoItem.FileID = xmlRepositoryBL.InsertAndArchiveXML(xmlRepoItem.FileID, BL.XmlRepository.Location.processing, tr.ToXml(serializerFactory));
                            break;

                        case ArchiveStrategy.UpdateOriginalFile:
                            // just update the contents of the existing file w/o archiving
                            xmlRepositoryBL.UpdateFileContent(xmlRepoItem.FileID, tr.ToXml(serializerFactory));
                            break;
                        }

                        vrs = tisExtender.Validate(this, tr, xmlRepoItem, projectMetaData, tisExtenderState, out isFatal, out sendToModifiers);

                        tr.Acknowledged = true;

                        if (((tr.Testee.EntityKey < 0 || tr.Opportunity.OpportunityNumber < 0) && !tr.Opportunity.IsDiscrepancy) ||
                            tr.Opportunity.StatusDate == null ||
                            tr.ValidationRecords.Count > 0 ||
                            isFatal)
                        {
                            // Update db with message messages...
                            ar.SaveResultExceptions(tr, tr.ValidationRecords, xmlRepoItem.FileID);
                            // If fatal then save exception and move to failed folder
                            if (isFatal)
                            {
                                result = QAResult.FailValidation;
                                //AM 8/13/2010: changed this; 1 email will be sent at the end with all
                                //  warnings (and errors too if there were any) if emailAlertForWarnings = true
                                //  and there were warnings to send.
                                ar.SaveResultExceptions(tr, vrs, xmlRepoItem.FileID);
                                //ar.SaveResultExceptions(tr, vrs, destinationFile, fileName, emailAlertForWarnings);
                                return(result);
                            }
                        }
                        //AM 8/13/2010: same comment as above
                        ar.SaveResultExceptions(tr, vrs, xmlRepoItem.FileID);
                        //ar.SaveResultExceptions(tr, vrs, destinationFile, fileName, emailAlertForWarnings);
                    }
                    else // If there are some XML validation errors then save exception and move to exception folder
                    {
                        result = QAResult.FailValidation;
                        ar.SaveResultExceptions(tr, adapter.ValidationRecords, xmlRepoItem.FileID);
                        return(result);
                    }
                }
                else // tr == null
                {
                    result = QAResult.FailValidation;
                    ar.SaveResultExceptions(tr, adapter.ValidationRecords, xmlRepoItem.FileID);
                    return(result);
                }

                // Update the test results after validation has been completed.
                try
                {
                    if (isValid)
                    {
                        //PREROUTE:
                        tisExtender.PreRoute(this, adapter, tr, xmlRepoItem, projectMetaData, sendToModifiers, tisExtenderState);

                        // Update XML destinations in case some business rule modified it
                        if (sendToModifiers != null)
                        {
                            foreach (KeyValuePair <SendTo, bool> sendInfo in (SendToModifiersTyped)sendToModifiers)
                            {
                                switch (sendInfo.Key)
                                {
                                case SendTo.DoR:
                                    projectMetaData.updateDoR = sendInfo.Value;
                                    break;

                                case SendTo.Handscoring:
                                    projectMetaData.sendToHandScoring = sendInfo.Value;
                                    break;

                                case SendTo.RB:
                                    projectMetaData.updateRB = sendInfo.Value;
                                    break;
                                }
                            }
                        }

                        if (projectMetaData.updateDoR)
                        {
                            try
                            {
                                List <Target> dorTarget = Target.GetOrderedTargets(tr.ProjectID, Target.TargetClass.DoR);
                                if (dorTarget != null && dorTarget.Count == 1)
                                {
                                    dorTarget[0].Send(tr, delegate(object o) { dorRecordId = (long)o; }, projectMetaData.doRAdminID);
                                }
                            }
                            catch (Exception ex)
                            {
                                throw new QAException(String.Format("DoR update failed for fileId: {0}, Message: {1}", xmlRepoItem.FileID, ex.Message), QAException.ExceptionType.General, ex);
                            }
                        }

                        //send to configured Handscoring targets
                        ItemScoringManager.Instance.Send(tr, sendToModifiers);

                        // send to configured targets in order (incl RB)
                        foreach (Target t in Target.GetOrderedTargets(tr.ProjectID, Target.TargetClass.General))
                        {
                            if (!sendToModifiers.ShouldSend(t.Name))
                            {
                                continue;
                            }
                            ITargetResult targetResult = t.Send(tr);
                            if (targetResult.Sent)
                            {
                                Logger.Log(true, String.Format("Sent data for FileId: {0} to Target: {1} ({2}).", xmlRepoItem.FileID, t.Name, targetResult.ID ?? "<unspecified>"), EventLogEntryType.Information, false, true);
                            }
                        }

                        //POSTROUTE:
                        tisExtender.PostRoute(this, tr, xmlRepoItem, projectMetaData, tisExtenderState);
                    }

                    // Check if this test need to be merged with something else
                    try
                    {
                        TDSQASystemAPI.TestMerge.TestMerge testMerge = TestMergeConfiguration.Instance.GetTestMerge(tr.Name);
                        if (testMerge != null)
                        {
                            testMerge.CreateCombinedTest(_tc, tr, serializerFactory);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new QAException(String.Format("TestMerge operation failed for fileId: {0}, Message: {1}", xmlRepoItem.FileID, ex.Message), QAException.ExceptionType.General, ex);
                    }
                }
                catch (QAException qae)
                {
                    //TODO:
                    result = QAResult.FailUpdate;
                    string message = qae.GetExceptionMessage(true);
                    if (message.StartsWith("DoR"))
                    {
                        ar.SaveResultExceptions(tr, "DoR update failed: ", message, xmlRepoItem.FileID);
                        Logger.Log(true, message, EventLogEntryType.Error, false, true);
                    }
                    else if (message.StartsWith("Handscoring"))
                    {
                        ar.SaveResultExceptions(tr, "Handscoring update failed: ", message, xmlRepoItem.FileID);
                        Logger.Log(true, message, EventLogEntryType.Error, false, true);
                    }
                    else if (message.StartsWith("TestMerge"))
                    {
                        ar.SaveResultExceptions(tr, "Test merge operation failed: ", message, xmlRepoItem.FileID);
                        Logger.Log(true, message, EventLogEntryType.Error, false, true);
                    }
                    else if (message.StartsWith("AutoAppeal"))
                    {
                        ar.SaveResultExceptions(tr, "AutoAppeal failed: ", message, xmlRepoItem.FileID);
                        Logger.Log(true, message, EventLogEntryType.Error, false, true);
                    }
                    else
                    {
                        ar.SaveResultExceptions(tr, "Response Bank update failed: ", message, xmlRepoItem.FileID);
                        Logger.Log(true, message, EventLogEntryType.Error, false, true);
                    }
                    return(result);
                }
                catch (Exception ex)
                {
                    result = QAResult.FailUpdate;
                    string message = ex.GetExceptionMessage(true);
                    ar.SaveResultExceptions(tr, "Response Bank update failed: ", message, xmlRepoItem.FileID);
                    Logger.Log(true, message, EventLogEntryType.Error, false, true);
                    return(result);
                }

                // Log to TestOpportunityStatus, Update the TDS_QC database, and possibly send to TIS
                if (tr != null)
                {
                    try
                    {
                        // Log to TestOpportunityStatus, Update the TDS_QC database
                        ar.SaveResultInfo(tr, xmlRepoItem.FileID, tr.Testee.IsDemo, dorRecordId, archivedFileID, projectMetaData.updateRB);
                    }
                    catch (Exception ex)
                    {
                        result = QAResult.FailUpdate;
                        string message = ex.GetExceptionMessage(true);
                        ar.SaveResultExceptions(tr, "Archive ", ex.Message, xmlRepoItem.FileID);
                        Logger.Log(true, message, EventLogEntryType.Error, false, true);
                        return(result);
                    }

                    //POSTSAVE:
                    tisExtender.PostSave(this, tr, xmlRepoItem, projectMetaData, tisExtenderState);
                }
                result = QAResult.Success;
            }
            catch (Exception ex)
            {
                result = QAResult.Unknown;
                string message = ex.GetExceptionMessage(true);

                if (tr != null)
                {
                    ar.SaveResultExceptions(tr, "QA System Exception:", message, xmlRepoItem.FileID);
                }
                else
                {
                    ar.SaveResultExceptions("QA System Exception:", message, xmlRepoItem.FileID);
                }
                Logger.Log(true, message, EventLogEntryType.Error, false, true);
            }
            finally
            {
                if (!(tr == null || tr.Opportunity == null))
                {
                    // We don't want to call back to TDS for scanned paper tests, since they did not originate from TDS.
                    //  Just skip w/o logging.
                    if (!tr.Mode.Equals("scanned"))
                    {
                        Boolean accepted = true;
                        string  message  = null;
                        if (result == QAResult.FailUpdate || result == QAResult.FailValidation || result == QAResult.Unknown || !(tr.PassedAllValidations()))
                        {
                            accepted = false;
                            if (result == QAResult.FailUpdate)
                            {
                                message = "failed validation";
                            }
                            else if (result == QAResult.FailValidation)
                            {
                                message = "failed while either updating the Response Bank, storing data into the DoR, or invoking the Handscoring webservice";
                            }
                            else if (result == QAResult.Unknown)
                            {
                                message = "An unknown exception occurred";
                            }
                            else if (!(tr.PassedAllValidations()))
                            {
                                message = "Failed rules validation";
                            }
                        }

                        IAcknowledgementTargetFactory ackTargetFactory = AIR.Common.ServiceLocator.Resolve <IAcknowledgementTargetFactory>();
                        if (ackTargetFactory != null) // ok not to acknowledge I suppose
                        {
                            IAcknowledgementTarget ackTarget = ackTargetFactory.SelectTarget(xmlRepoItem);
                            try
                            {
                                if (ackTarget == null ||
                                    !ackTarget.Send(new Message(tr.Opportunity.Key, accepted, message), xmlRepoItem))
                                {
                                    Logger.Log(true, String.Format("Acknowledgement not sent for fileID: {0}", xmlRepoItem.FileID), EventLogEntryType.Information, false, true);
                                }
                            }
                            catch (Exception ex)
                            {
                                // allow these to be treated as warnings or errors.  If TreatAcknowledgementFailureAsError is set to true,
                                //  a failure to send an ACK will result in the file being dumped into the reject bin.
                                //  Default behavior is to treat these as warnings.  We generally don't want to fail a file just because
                                //  we can't send the ACK.  Note also that a combo may already have been created (if applicable).
                                bool treatAckfailureAsError = false;

                                if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["TreatAcknowledgementFailureAsError"]) &&
                                    Convert.ToBoolean(ConfigurationManager.AppSettings["TreatAcknowledgementFailureAsError"]))
                                {
                                    treatAckfailureAsError = true;
                                }

                                // if the file would have otherwise succeeded and there was an exception while attempting to send the ACK and
                                //  we're not treating these as warnings, fail the file.
                                if (treatAckfailureAsError && result == QAResult.Success)
                                {
                                    result = QAResult.Unknown;
                                }

                                Logger.Log(true, String.Format("Could not send acknowledgement for fileID: {0}, Exception: {1}", xmlRepoItem.FileID, ex.GetExceptionMessage(true)),
                                           treatAckfailureAsError ? EventLogEntryType.Error : EventLogEntryType.Warning, false, true);
                            }
                        }
                    }
                }

                //Move file to appropriate folder based on the QAResult and log the status accordingly.
                LogAndCleanup(result, xmlRepoItem.FileID, tr, startDateTime);
            }
            return(result);
        }
Exemplo n.º 5
0
 public void PostSave(QASystem tis, TestResult tr, XmlRepositoryItem xmlRepoItem, ProjectMetaData projectMetaData, ITISExtenderState state)
 {
     // do nothing
 }
Exemplo n.º 6
0
 public void PreRoute(QASystem tis, XMLAdapter adapter, TestResult tr, XmlRepositoryItem xmlRepoItem, ProjectMetaData projectMetaData, SendToModifiers sendToModifiers, ITISExtenderState state)
 {
     // do nothing
 }
Exemplo n.º 7
0
 public List <ValidationRecord> Validate(QASystem tis, TestResult tr, XmlRepositoryItem xmlRepoItem, ProjectMetaData projectMetaData, ITISExtenderState state, out bool isFatal, out SendToModifiers sendToModifiers)
 {
     isFatal         = false;
     sendToModifiers = new SendToModifiers();
     return(new List <ValidationRecord>());
 }
Exemplo n.º 8
0
 /// <summary>
 /// Initialize the file name.
 /// </summary>
 /// <param name="file"></param>
 public QASystemWorkerThread(XmlRepositoryItem xmlRepositoryItem)
 {
     this.xmlRepositoryItem = xmlRepositoryItem;
 }