Sign in to follow this  
Followers 0
iceberg

Program as a service

3 posts in this topic

hi all,

would be helpful if someone can enlighten me as to why this code stops working after about 3-4 weeks?

thanks.

#include <Date.au3>
#include <Constants.au3>
#include <ServiceControl.au3>
Opt("TrayAutoPause", 0)
TraySetState()

If @ScriptName <> "ccglogext.exe" Then
    MsgBox(16, "Warning", "Script name must be ccglogext.exe for all functions to perform properly.")
    Exit
EndIf

If Not FileExists(@ScriptDir & "\settings.ini") Then
    $Set = FileInstall("set.ini", @ScriptDir & "\settings.ini", 1)
    If $Set = 0 Then MsgBox(0, "Error", "Unable to load default settings.ini")
    MsgBox(0, "Missing configuration file restored", "Please update the default settings.ini before proceeding.")
    $NP = Run("notepad.exe " & @ScriptDir & "\settings.ini", "", @SW_MAXIMIZE)
    If $NP = 0 Then MsgBox(0, "Error", "Unable to open settings.ini file for modification.")
    Exit
EndIf

Global $nLastEOF = 0
Global $sTextFound = ""
Global $inifile = "settings.ini"
Global $nMonitorSleep = Number((IniRead($inifile, "config", "delay", "delay")) * 60000)
Global $sFileLocationName = IniRead($inifile, "config", "filepath", "filepath") & StringMid(@YEAR & @MON & @MDAY, 3) & ".msg"
HotKeySet("{F7}", "LeaveLoop")
HotKeySet("{F8}", "endall")
Global $ll, $kk = False
Global $work = True
;Global $svc = "{08CECC71-A9B1-417d-AB3A-C57C4F854F53}2231083263"
Global $svc = "CCGLogX"

$sendto = IniRead($inifile, "config", "sendto", "sendto")
If Not FileExists($sendto) Then
    $save = DirCreate(IniRead($inifile, "config", "sendto", "sendto"))
    If $save = 0 Then
        MsgBox(0, "Error", "Unable to create SENDTO folder. Please check access rights and settings.ini")
        Exit
    EndIf
EndIf

While 1
    Sleep(3000)
    If IniRead($inifile, "config", "date", "date") <> StringMid(@YEAR & @MON & @MDAY, 3) Then
        $sFileLocationName = IniRead($inifile, "config", "filepath", "filepath") & StringMid(@YEAR & @MON & @MDAY, 3) & ".msg"
        $ID = IniWrite($inifile, "config", "date", StringMid(@YEAR & @MON & @MDAY, 3))
        If $ID <> 1 Then
            $FW = FileWrite(IniRead($inifile, "config", "sendto", "sendto") & "error.log", "Unable to write new date to settings.ini on " & _Now() & @CRLF)
            If $FW = 0 Then MsgBox(0, "Error", "Unable to write to error.log", 10)
            Exit
        EndIf
        $IL = IniWrite($inifile, "config", "LastEOF", "0")
        If $IL <> 1 Then
            $FW = FileWrite(IniRead($inifile, "config", "sendto", "sendto") & "error.log", "Unable to write new LastEOF to settings.ini on " & _Now() & @CRLF)
            If $FW = 0 Then MsgBox(0, "Error", "Unable to write to error.log", 10)
            Exit
        EndIf
    EndIf
    
    If $work Then
        $work = False
        MonitorChanges()
        _ReduceMemory(@AutoItPID)
        AdlibEnable("setgoing", $nMonitorSleep)
    EndIf

    If $kk Then ExitLoop
    Sleep(100)
WEnd

Func setgoing()
    $work = True
    AdlibDisable()
EndFunc  ;==>setgoing

Func LeaveLoop()
    $ll = True
EndFunc  ;==>LeaveLoop

Func endall()
    $ll = True
    $kk = True
    _StopService(@ComputerName, $svc)
EndFunc  ;==>endall

Func MonitorChanges()
    Dim $array
    $nLastEOF = Number(IniRead($inifile, "config", "LastEOF", ''))
    $ll = False

    If FileOpen($sFileLocationName, 0) = -1 Then
        $FW = FileWrite(IniRead($inifile, "config", "sendto", "sendto") & "error.log", "Unable to access MSG file on " & _Now() & @CRLF)
        If $FW = 0 Then MsgBox(0, "Error", "Unable to write to error.log", 10)
    EndIf

    $sTextFound = _FileMonitorLog($sFileLocationName, $nLastEOF)
    If $sTextFound <> "" Then
        If StringInStr($sTextFound, (IniRead($inifile, "config", "search", "search"))) Then
            $found = 0
            For $x = 0 To UBound($array) - 1
                If $sTextFound == $array[$x] Then
                    $found = 1
                    Exit
                EndIf
            Next
            If Not $found Then
                If IsArray($array) Then
                    ReDim $array[UBound($array) + 1]
                Else
                    Dim $array[1]
                EndIf
                $array[UBound($array) - 1] = $sTextFound
            EndIf
        EndIf
    EndIf
    If $ll Then Exit
    FileClose($sFileLocationName)
    AppendText($array)
    IniWrite($inifile, "config", "LastEOF", $nLastEOF)
EndFunc  ;==>MonitorChanges

Func AppendText($aT)
    For $x = 0 To UBound($aT) - 1
        $ans = StringRegExp($aT[$x], ".*" & (IniRead($inifile, "config", "search", "search")) & ".*", 3)
        For $r = 0 To UBound($ans) - 1
            $data = StringSplit($ans[$r], @CRLF, 1)
            For $i = 1 To $data[0]
                $Line = $data[$i]
                $array = StringSplit($Line, @TAB)
                If $array[0] > 11 And StringInStr($array[12], (IniRead($inifile, "config", "search", "search"))) Then
                    $found = "Yes"
                    $3 = StringTrimLeft($array[3], 1)
                    $13 = StringTrimRight($array[13], 2)
                    $archive = (IniRead($inifile, "config", "sendto", "sendto") & StringMid(@YEAR & @MON & @MDAY, 3) & "\")
                    If Not FileExists($archive) Then
                        $Dir = DirCreate($archive)
                        If $Dir = 0 Then
                            $FW = FileWrite(IniRead($inifile, "config", "sendto", "sendto") & "error.log", "Unable to create SENDTO folder on " & _Now() & @CRLF)
                            If $FW = 0 Then MsgBox(0, "Error", "Unable to write to error.log", 10)
                        EndIf
                    Else
                        $FWL = FileWrite($archive & $3 & ".txt", $array[8] & @TAB & $13 & @CRLF)
                        If $FWL = 0 Then
                            $FW2 = FileWrite(IniRead($inifile, "config", "sendto", "sendto") & "error.log", "Unable to write ARCHIVES to settings.ini on " & _Now() & @CRLF)
                            If $FW2 = 0 Then MsgBox(0, "Error", "Unable to write to error.log", 10)
                        EndIf
                    EndIf
                EndIf
            Next
        Next
    Next
    $Time = _DateAdd('n', (IniRead($inifile, "config", "delay", "delay")), _NowCalc())
    TraySetToolTip("CCG: F8 to stop service. Next search on " & $Time)
EndFunc  ;==>AppendText

Func _FileMonitorLog($sFile, ByRef $nLastEOF)
    Local $aSplit = StringSplit(StringStripCR(FileRead($sFile)), @LF)
    Local $sLog = ""
    If ($aSplit[0] > $nLastEOF) Then
        For $iCC = $nLastEOF + 1 To $aSplit[0]
            $sLog &= $aSplit[$iCC] & @CRLF
        Next
        $nLastEOF = $aSplit[0]
    EndIf
    Return $sLog
EndFunc  ;==>_FileMonitorLog

Func _ReduceMemory($i_PID = -1)
    If $i_PID <> -1 Then
        Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
        DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
    Else
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
    EndIf
    Return $ai_Return[0]
EndFunc  ;==>_ReduceMemory

mouse not found....scroll any mouse to continue.

Share this post


Link to post
Share on other sites



what error did you get after 3-4 weeks? if it is working fine then why did not after some time...


Website: www.cerescode.comForum: www.forum.cerescode.comIRC: irc.freenode.net , Channel: #Ceres--------------------Autoit Wrappers, Great additions to your script (Must See) (By: Valuater)Read It Befor Asking Question Click Here...--------------------Join Monoceres's Forums http://www.monoceres.se--------------------There are three kinds of people: Those who make things happen, those who watch things happen, and those who ask, ‘What happened?’” –Casey Stengel

Share this post


Link to post
Share on other sites

there is no error.

though it shows that the service is running, the program is not.

thanks.


mouse not found....scroll any mouse to continue.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0