在并发调用脚本后将错误值写入日志文件

Writing wrong values to log files after concurrent calls of a script

此代码示例是 vbscript 脚本的一部分 正在发出 HTTP POST 请求以通过 API 发送短信。对于每条短信,我需要一个唯一的随机 ID。功能说明:

WriteLog(创建日志文件)

MoveLogFiles(归档旧日志文件)

CreateGUID(为每条短信生成 GUID)

这里的问题是我写入日志文件的时候。我尝试 apache 基准测试 来对我的脚本进行压力测试。在日志文件中,smsID 未正确存储。它应该对于每个 http 请求都是唯一的。相反,我在日志文件 的许多条目中看到 相同的 ID。如何保护变量,以便在多次调用脚本时在日志文件中写入适当的值?

Private smsID
smsID = CreateGUID(12)
'HTTP Request & Response 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
WriteLog smsID, "[SMS ID:" & smsID & "] Starting HTTP Request..." 
WriteLog smsID, "[SMS ID:" & smsID & "] JSON to be sent: " & JSONStringNoPIN
WriteLog smsID, "[SMS ID:" & smsID & "] Sending HTTP Request..."
ASPPostJSON(smsID)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
set JSONString = Nothing
set JSONStringNoPIN = Nothing
set JSONRequest = Nothing
logFilesDic.RemoveAll
Set logFilesDic = Nothing
WriteLog smsID, "[SMS ID:" & smsID & "] Finished..." & vbCrLf
Set smsID=Nothing

Function WriteLog(ByVal smsID_, ByVal psMessage)
    Dim fs: Set fs = Server.CreateObject("Scripting.FileSystemObject")

    ' Check if the log file and folder exists
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    dim f, logSize, logDic 
    On Error Resume Next
    Set f = fs.GetFile(logFile)
    Set logDic = CreateObject("Scripting.Dictionary")
    'logDic.Add "1", Now() & vbTab & "[SMS ID:" & smsID_ & "] Opening Log file: " & logFile   

    If Err.Number <> 0 Then
        logDic.Add "1", Now() &  vbTab & "[SMS ID:" & smsID_ & "] An error occured while opening the log file. Error #" & Err.Number & ": " & Err.Description       
        If Err.Number = 53 And Not fs.FileExists(logFile) Then 
            if Not fs.FolderExists Then
                fs.CreateFolder logDirectory
            End if
            if Not fs.FileExists(logFile) Then
                fs.CreateTextFile logFile
                logDic.Add "2", Now() &  vbTab & "[SMS ID:" & smsID_ & "] Log file was created: " & logFile
            End if
            Set f = fs.GetFile(logFile)
        End if  
    End if 
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    ' Check if the log size exceeds the log size limit 
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    logSize = f.Size
    if logSize >= logMaxSize Then
        logDic.Add "3", Now() &  vbTab & "[SMS ID:" & smsID_ & "] Log file is exceeding the maximum file size allowed:" & logMaxSize  & " Bytes. Archiving the current log file: " & logFile
        MoveLogFiles()
        fs.CreateTextFile logFile
        logDic.Add "4", Now() & vbTab & "[SMS ID:" & smsID_ & "] Log file was archived: " & logTemp
    End if
    set f=nothing
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    ' Write to the log file
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Application.Lock
        dim log, logDicItems
        Set log = fs.OpenTextFile(logFile, 8, True) '1=ForReading, 2=ForWriting, 8=ForAppending   

        If logDic.Exists("1") Then log.WriteLine logDic.Item("1")
        If logDic.Exists("2") Then log.WriteLine logDic.Item("2") 
        If logDic.Exists("3") Then log.WriteLine logDic.Item("3") 
        If logDic.Exists("4") Then log.WriteLine logDic.Item("4") 

        log.WriteLine Now() & vbTab & psMessage 
        log.Close 
    Application.UnLock
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Set log = Nothing
    fs.Close() 
    Set fs = Nothing 
    logDic.RemoveAll
    Set logDic = Nothing
    WriteLog = True 
End Function 

Function MoveLogFiles() 
    Dim fs: Set fs = Server.CreateObject("Scripting.FileSystemObject")
    For i = logMaxArchives To 1  Step -1
        if fs.FileExists(logFile & "." & CStr(i)) Then
            if i = logMaxArchives Then 
                fs.DeleteFile logDirectory & logFilesDic.Item(logMaxArchives)
            Else
                fs.MoveFile logDirectory & logFilesDic.Item(i), logDirectory & logFilesDic.Item(i+1)
            End if
        End if
    Next
    fs.MoveFile logDirectory & logFilesDic.Item(0), logDirectory & logFilesDic.Item(1)
    fs.Close()
End Function 


' Generate unique identifier for SMS
Function CreateGUID(tmpLength)
    Application.Lock
        if tmpLength >= 64 or tmpLength < 0 Then 
            WriteLog "Error when generating SMS ID. Maximum length of characters allowed: 64. Value given: " & tmpLength
        Elseif tmpLength < 4 Then
            WriteLog "Error when generating SMS ID. Minimum length of characters allowed: 4. Value given: " & tmpLength
        Else
            Randomize Timer
            Dim tmpCounter,tmpGUID
            Const strValid = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            For tmpCounter = 1 To tmpLength
                tmpGUID = tmpGUID & Mid(strValid, Int(Rnd(1) * Len(strValid)) + 1, 1)
            Next
            CreateGUID = tmpGUID
        End if
    Application.UnLock
End Function

最终问题是由随机化定时器引起的。相反,我使用了这个:

Function CreateGUID()
    Dim TypeLib: Set TypeLib = CreateObject("Scriptlet.TypeLib")
    CreateGUID = TypeLib.Guid
    CreateGUID = Left(CreateGUID, Len(CreateGUID)-3)
    CreateGUID = Mid(CreateGUID,2)
    Response.Write CreateGUID 
End Function