我正在编写一个服务来下载,然后每个周末删除FTP站点的全部内容。
文件大小从2mb到8mb不等,初始下载大约为150,000+。在此之后,我希望下载范围是每周2000个文件。我打算安排服务在午夜左右运行。
这适用于一家供应商,如果一切正常,那么我们将每周与数百家供应商合作。
我对编写可靠服务的最佳实践感兴趣,能够下载如此大量的文件。
一些问题/疑虑:
我是否应该考虑将任务分配到单独的服务中?
服务#1 解析目录内容并将文件名保存到表中。
服务#2下载并删除文件,如果目录为空,则删除该文件夹。
我只是在那里抛出一些想法;不要求任何人给我写任何东西。只是寻找关于如何构建这样的东西的想法。
Private Function GetStringResponse(ByVal ftp As FtpWebRequest) As String
Dim response As FtpWebResponse = Nothing
Dim datastream As Stream = Nothing
Dim sr As StreamReader = Nothing
Dim result As String = ""
Try
response = DirectCast(ftp.GetResponse, FtpWebResponse)
Dim size As Long = response.ContentLength
datastream = response.GetResponseStream
sr = New StreamReader(datastream)
result = sr.ReadToEnd()
Finally
If Not response Is Nothing Then
response.Close()
End If
If Not datastream Is Nothing Then
datastream.Close()
End If
If Not sr Is Nothing Then
sr.Close()
End If
End Try
Return result
End Function
Private Function GetRequest(ByVal URI As String, Optional p_KeepAlive As Boolean = False) As FtpWebRequest
Dim result As FtpWebRequest = CType(FtpWebRequest.Create(URI), FtpWebRequest)
If m_UserSsl Then
Net.ServicePointManager.ServerCertificateValidationCallback = AddressOf ValidateCertificate
result.EnableSsl = True
End If
result.Credentials = GetCredentials()
result.KeepAlive = p_KeepAlive
Return result
End Function
Public Function GetDirectoryTree(p_Directory As String, Optional p_Mask As String = "") As List(Of String)
Dim files As New List(Of String)
Dim folders As New Queue(Of String)()
Dim _resp As StreamReader = Nothing
Dim _Ftp As Net.FtpWebRequest = Nothing
folders.Enqueue(p_Directory)
While folders.Count > 0
Dim fld As String = folders.Dequeue()
Dim newFiles As List(Of String)
_Ftp = GetRequest(GetDirectory(fld), True)
_Ftp.Method = Net.WebRequestMethods.Ftp.ListDirectory
Dim response As String = GetStringResponse(_Ftp)
If response.Length > 0 Then
newFiles = response.Replace(Convert.ToChar(13) + Convert.ToChar(10), Convert.ToChar(13)).TrimEnd(Convert.ToChar(13)).Split(Convert.ToChar(13)).ToList()
If p_Mask <> String.Empty Then newFiles = newFiles.Where(Function(x) x.ToUpper Like p_Mask.ToUpper).ToList
Else
newFiles = New List(Of String)
End If
_Ftp = GetRequest(GetDirectory(fld), True)
_Ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails
Using resp As New StreamReader(_Ftp.GetResponse().GetResponseStream())
Dim line As String = resp.ReadLine()
While line IsNot Nothing
If line.Trim().ToLower().StartsWith("d") OrElse line.Contains(" <DIR> ") Then
Dim dir As String = newFiles.First(Function(x) line.EndsWith(x))
newFiles.Remove(dir)
folders.Enqueue(fld + dir + "/")
End If
line = resp.ReadLine()
End While
End Using
files.AddRange(newFiles.Select(Function(x) fld + x).ToList())
End While
End Function