public void WorkerLoop() { try { // do work LogManager.GetLogger("SiteLogger").Info("Working on " + mWorkItem); //logger.logEntry(this.ToString(), "Working on " + mWorkItem, LogMsgSeverity.Information, false); //TODO: Write the data to the work table to support restarts //FileTransferService.TransferWorkDataHandler twdh = new FileTransferService.TransferWorkDataHandler(); //FileTransferService.TransferWorkDS twDS = new FileTransferService.TransferWorkDS(); //twDS.TransferWork.AddTransferWorkRow(0, //twdh.SetTransferWork( try { TransferMainService transferMainService = new TransferMainService(); DestinationService destinationsService = new DestinationService(); TransferWorkService transferWorkService = new TransferWorkService(); FTPProcessor ifpx; DoFTPActionStartDelegate dlgt; IAsyncResult ar; // Look up transfer record by ID TransferMain tm = transferMainService.GetByTransferRefId(mWorkRefId); mTransferType = (TransferTypeList)tm.TransferTypeId; // check to make sure the file is complete if (mTransferType == TransferTypeList.FileWatchAndProcess) { while (!TryToOpenInExclusiveMode(mWorkItem)) { System.Threading.Thread.Sleep(5000); } } // Look up transfer record by ID TList<Destination> dests = destinationsService.GetByTransferRefId(mWorkRefId); switch (mTransferType) { case TransferTypeList.FileWatchAndProcess: if (tm.UsePendingFileStucture) SetFilePending(); // Create the delegate. //MDBInterfaceProcessor mdbip = new MDBInterfaceProcessor(); //mdbip.ProcessFileComplete += new ProcessFileCompleteDelegate(OnProcessFileComplete); bool processResult; ProcessTypeService pts = new ProcessTypeService(); ProcessType pt = pts.GetByProcessTypeId((short)tm.ProcessTypeId); string path = new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName; this.ExecuteProcessorAssemblyMethod(mWorkItem, (short)tm.ProcessTypeId, out processResult, System.IO.Path.Combine(path, pt.LoadAssembly), pt.ClassName); //mdbip.ProcessFile(mWorkItem, (InterfaceType)ip.ProcessTypeId , out processResult); break; case TransferTypeList.FileWatchFtpPut: if (tm.UsePendingFileStucture) SetFilePending(); // fire a transfer for every destination you find foreach (Destination dest in dests) { // Create the delegate. ifpx = new FTPProcessor(); dlgt = new DoFTPActionStartDelegate(ifpx.DoFTPAction); /// write transfer to work table TransferWork transferWork = new TransferWork(); transferWork.TransferWorkRefId = Guid.NewGuid(); transferWork.TransferRefId = dest.TransferRefId; transferWork.DestinationRefId = dest.DestinationRefId; transferWork.Filename = mWorkItem; transferWork.SubmitTime = DateTime.Now; //transferWork.CompletionTime = DateTime.MinValue; transferWork.Retires = 0; transferWork.StatusTypeId = (short)StatusTypeList.New; transferWorkService.Save(transferWork); // Initiate the asychronous call. Include an AsyncCallback // delegate representing the callback method, and the data // needed to call EndInvoke. //TODO: Make this a generic call back function ar = dlgt.BeginInvoke(new FTPEventArgs(dest.DestinationRefId, dest.DestServer, dest.SourceDir, dest.DestDir, dest.Username, dest.Password, true, FtpClient.TransferType.PUT, mWorkItem, dest.DeleteAfterProcessing, ftpRetryThreshhold), out processResult, out destinationRefId, new AsyncCallback(OnFTPActionComplete), dlgt); } break; case TransferTypeList.FileWatchFtpPutGet: break; case TransferTypeList.FtpGet: if (tm.UsePendingFileStucture) { FileInfo file = new FileInfo(mWorkItem); if (!file.Exists) file.Create(); // used for gets SetFilePending(); } foreach (Destination dest in dests) { // Create the delegate. ifpx = new FTPProcessor(); dlgt = new DoFTPActionStartDelegate(ifpx.DoFTPAction); /// write transfer to work table TransferWork transferWork = new TransferWork(); transferWork.TransferWorkRefId = Guid.NewGuid(); transferWork.TransferRefId = dest.TransferRefId; transferWork.DestinationRefId = dest.DestinationRefId; transferWork.Filename = mWorkItem; transferWork.SubmitTime = DateTime.Now; transferWork.Retires = 0; transferWork.StatusTypeId = (short)StatusTypeList.New; try { transferWorkService.Save(transferWork); } catch (System.Data.SqlClient.SqlException ex) { LogManager.GetLogger("SiteLogger").Warn("Error Saving Transfer work on " + mWorkItem,ex); //logger.logEntry(this.ToString(), "Error Saving Transfer work on " + mWorkItem + " " + ex.Message, LogMsgSeverity.Warning, false); } // Initiate the asychronous call. Include an AsyncCallback // delegate representing the callback method, and the data // needed to call EndInvoke. //TODO: Make this a generic call back function ar = dlgt.BeginInvoke(new FTPEventArgs(dest.DestinationRefId, dest.SourceServer, dest.DestDir, dest.SourceDir, dest.Username, dest.Password, true, FtpClient.TransferType.GET, mWorkItem, dest.DeleteAfterProcessing, ftpRetryThreshhold), out processResult, out destinationRefId, new AsyncCallback(OnFTPActionComplete), dlgt); } break; case TransferTypeList.FtpGetPut: break; } LogManager.GetLogger("SiteLogger").Debug("Async WorkerLoop(): Started."); //logger.logEntry(this.ToString(), "Async WorkerLoop(): Started.", LogMsgSeverity.Trace, false); // sleep for 5 sec then check result Thread.Sleep(5000); LogManager.GetLogger("SiteLogger").Debug("Async WorkerLoop(): Ended."); //logger.logEntry(this.ToString(), "Async WorkerLoop(): Ended.", LogMsgSeverity.Trace, false); } catch (Exception ex) { LogManager.GetLogger("SiteLogger").Fatal("Async WorkerLoop(): Failed.", ex); //logger.logEntry(this.ToString(), "Async WorkerLoop(): Failed." + ex.Message, LogMsgSeverity.Critical, false); } } catch (Exception ex) { LogManager.GetLogger("SiteLogger").Fatal("Async WorkerLoop(): Failed.", ex); //logger.logEntry(this.ToString(), "Async WorkerLoop(): Failed." + ex.Message, LogMsgSeverity.Critical, false); } }