Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
File based Queue processing
Message
From
19/11/2008 11:32:12
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
 
 
To
All
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Title:
File based Queue processing
Environment versions
Environment:
C# 3.0
OS:
Windows XP SP2
Database:
MS SQL Server
Application:
Desktop
Miscellaneous
Thread ID:
01362837
Message ID:
01362837
Views:
80
Hi All,

I have an application that creates records and saves them to a file based queue for later processing. The files are saved as xml. I have a timer running in another thread that checks this queue directory for files and re-hydrates them back into a record then saves them to the database. After a successful save, they need to be deleted to prevent attempting to save them again. The problem I am having *I think* is they don't always get deleted and then cause a constraint error as they have a primary key. I could deal with this at the error level but I would like to deal with this more proactively.

I have considered many ways to do this like populating a list (saveList) with all the filenames in the directory after checking each one against a deleteList first. Then processing the files based on the list. Once saved moving them to the delete list. After completeing the save list, I could process the delete list to delete them. I am not convinced this is the most efficient way to do this so looking for better ideas or proven ways to deal with a file based queue. You can see my basic code below; for simplification I removed the catch code and some of the event firing code, and other stuff that didn't matter.
Thanks
Tim
public void SaveDataFiles()
{
    string path = AppDesktop.QueueDataPath;
    string filter = "Spot_*.xml";
    string fileName;
    string xmlFile;
    bool llSuccess = true;
    bool llValid = true;

    try
    {
        // Stop the timer while we work.
        if (tmrQChecker != null)
            this.SetTimer(tmrQChecker, Timeout.Infinite, this.QCheckInterval, null);

        TrackSpotEntity oSpotEntity = null;
        while ((fileName = this.PopQueue(path, filter)) != "" && llSuccess)
        {
            xmlFile = path + @"\" + fileName;

            oSpotEntity = null;
            oSpotEntity = oSpot.GetEntityFromQueue(xmlFile);

            if (oSpotEntity != null && oSpotEntity.HasValues)
            {
                if (this.CompanyValid == AccessValidation.Valid)
                {
                    oSpotEntity.CompanyID = this._companyID;
                        if (this.TruckValid == AccessValidation.Valid)
                        {
                            oSpot.Entity.TruckID = this._truckID;

                            // Attempt the save
                            mmSaveDataResult result = oSpot.SaveEntity(oSpotEntity);

                            if (result == mmSaveDataResult.RulesPassed)
                            {
                                // Must have been a success
                                this.DeleteDataFile(xmlFile);
                            }
                            else
                            {
                                if (result == mmSaveDataResult.Exception)
                                {
                                    // Lets deal with this here
                                    string exProblem = oSpot.Exception.ToString();
                                }
                                llSuccess = false;
                                // Cancel all the changes to the entity object
                                oSpot.CancelEntity(oSpotEntity);
                                this.OnDataLoggingError("Data was not saved to Network");
                                break;
                            }
                     }
                    else
                    {
                        // Unable to validate Truck
                        if (TruckValid == AccessValidation.NotValid)
                        {
                            this.OnDataLoggingError("Truck Not Valid");
                            // Truck not valid for data transfer
                            llValid = false;
                        }
                    }
                }
                else
                {
                    // Unable to validate Company
                    if (this.CompanyValid == AccessValidation.NotValid)
                    {
                         this.OnDataLoggingError("Company Not Valid");
                         // Company not valid for data transfer
                         llValid = false;
                    }
                }
            }
            else
            {
                // The returned entity is probably null and probably because of 
               // a constraint problem.  We need to skip this file now.
               this.skipFileList.Add(fileName);
            }
        } // End of While
        this.OnDataLogging("Completed all Network Saves", NetworkAction.CompletedSaveToNetwork);
    }
    // Catch removed for clarity
    finally
    {
        if (tmrQChecker != null)
        {
            // Restart Queue Timer if Company and Truck Are Valid.
            if (llValid)
                this.SetTimer(tmrQChecker, this.QRetryInterval, this.QCheckInterval, null);
            else // Otherwise, dispose of the timer.
            {
                tmrQChecker.Dispose();
                tmrQChecker = null;
            }
        }
    }
}

private string PopQueue(string path, string filter)
{
    string fileName = "";
    try
    {
        DirectoryInfo directory = new DirectoryInfo(path);
        FileInfo[] files = directory.GetFiles(filter);
        this.FilesInQueue = files.Length;
        if (files.Length > 0)
        {
            fileName = directory.GetFiles(filter).OrderBy(XmlReadMode => XmlReadMode.LastWriteTime).First().Name;

            //TODO: I need to verify it isn't one of the files in the skipFileList					
        }
    }
    catch (DirectoryNotFoundException ex)
    {
        // Code Removed
    }
    catch (Exception ex)
    {
        // code Removed
    }

    return fileName;
}
Timothy Bryan
Next
Reply
Map
View

Click here to load this message in the networking platform