B4J Question Release mode delete file after Sleep

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am using the server lib to create a web server.

I have the web server fully working, except one thing.

I have created a handler:
B4X:
srvr.AddHandler("/events/*", "EventHandle", False)

And when I request a file like: http://ip/events/1.txt
it will display the txt file in the web browser which is what I want. However, I want to delete the file 10 seconds later.

I am using the following code in the EventHandle module:

B4X:
'Class module
Sub Class_Globals
    Private mreq As ServletRequest 'ignore
    Private mresp As ServletResponse 'ignore
    
    Private templates As Map
    
End Sub

Public Sub Initialize
    templates.Initialize
End Sub

Sub Handle(req As ServletRequest, resp As ServletResponse)
    Try
        HandleMainPage (resp,req,req.RequestURI)
        
    Catch
        resp.Status = 404
        resp.ContentType = "text/html"
        Log("Error serving request: " & LastException)
        resp.Write("<h1><b>Error serving request</b></h1><br>Error: " & LastException)
    End Try
    
End Sub

Sub HandleMainPage (Response As ServletResponse, req As ServletRequest, page As String)

    If page = "/events/" Then
        Response.ContentType = SetContentType("txt")
        Response.Write( $"{"Command": "EventLog", "Error": "file missing"}"$)
        Return
    End If
    
    If page.StartsWith("/events/") Then
        page = page.Replace("/events/","")
    End If
    
    If File.Exists(FileLocation,"/appeventlog/" & page) = False Then
        Response.ContentType = SetContentType(page)
        Response.Write( $"{"Command": "EventLog", "Error": "file missing"}"$)
        Return
    End If

    Dim MainPage As String = GetTemplate(page) 'load the template
        
    Response.ContentType = SetContentType(page)
    Response.Write(MainPage)

    Log("The File: " & File.Combine(FileLocation,"/appeventlog/" & page))

    Sleep(10000)

    Log("Delete File: " & File.Combine(FileLocation,"/appeventlog/" & page))
    
    If File.Exists(FileLocation,"/appeventlog/" & page) = True Then
        Log("Deleting file")
        File.Delete(FileLocation,"/appeventlog/" & page)
    End If

End Sub

Sub FileLocation As String
    Dim os As String = GetSystemProperty("os.name", "").ToLowerCase
    If os.Contains("win") Then
        Return File.DirApp
    Else If os.Contains("mac") Then
        Return File.DirApp
    Else
        'linux
        Return "/opt/CloudServer"
    End If
End Sub

Sub GetTemplate(Name As String) As String

    If templates.ContainsKey(Name) Then Return templates.Get(Name)
    Dim temp As String = File.ReadString(FileLocation & "/appeventlog", Name)
    templates.Put(Name, temp)
    Return temp
End Sub

Sub SetContentType(FileName As String) As String 'ignore
    Dim extension, ContentType As String
    Dim m As Matcher = Regex.Matcher("\.([^\.]*)$", FileName) 'find the file extension
    If m.Find Then
        extension = m.Group(1).ToLowerCase
        Select extension
            Case "html", "htm"
                ContentType = "text/html"
            Case "js"
                ContentType = "text/javascript"
            Case "gif", "png"
                ContentType = "image/" & extension
            Case "jpeg", "jpg"
                ContentType = "image/jpeg"
            Case "css", "xml"
                ContentType = "text/" & extension
            Case "ico"
                ContentType = "image/vnd.microsoft.icon"
            Case "txt"
                ContentType = "text/plain"
            Case "logfile"
                ContentType = "application/octet-stream"
                
            Case Else
                ContentType = "application/octet-stream"
        End Select
        Return ContentType
    End If
End Sub

The above is working except it's not deleting the file 10 seconds later.

When I run the above on my Windows 10 PC in debug mode it works fine. If I then run the code in release mode it doesn't delete the file.

The code after sleep(1000) doesn't seem to trigger when running in release mode but does work in debug mode.

Anyone know why the file won't delete when running in release mode ?
 

aaronk

Well-Known Member
Licensed User
Longtime User
Does that last
You declared the Handler as SingleThreaded=False which means it will run its own Thread when run in release mode.
Changing the handle to:
B4X:
srvr.AddHandler("/events/*", "EventHandle", True)

Seem to fix the issue.

Thanks heaps!
 
Upvote 0
Top