codeproject / CodeProject.AI-Server

CodeProject.AI Server is a self contained service that software developers can include in, and distribute with, their applications in order to augment their apps with the power of AI.
Other
636 stars 143 forks source link

Compilations of several .NET project failse because of an error in endtimer.bat #64

Closed matra774 closed 11 months ago

matra774 commented 1 year ago

(I think I submitted this one an hour ago, but is not showing up in a issue list, so I am resubmitting. Feel free to close as a duplciate if original submission shows up) Area of Concern

Describe the bug Compilation of several project fails:

Severity    Code    Description Project File    Line    Suppression State
Error   MSB3073 The command "call ..\..\SDK\Scripts\endtimer.bat Common" exited with code 255.  Common  C:\src\CodeProject.AI-Server\src\API\Common\Common.csproj   52  
Error   MSB3073 The command "call ..\..\..\SDK\Scripts\endtimer.bat Backend" exited with code 255.  Backend C:\src\CodeProject.AI-Server\src\API\Server\Backend\Backend.csproj  55  
Error   MSB3073 The command "call ..\..\..\SDK\Scripts\endtimer.bat Frontend" exited with code 255. Frontend    C:\src\CodeProject.AI-Server\src\API\Server\FrontEnd\Frontend.csproj    162 

Expected behavior Compilation should succeed.

Screenshots If applicable, add screenshots to help explain your problem.

Your System (please complete the following information):

Additional context Looks like an error occurs in line 30 while trying to parse end dates. Modifying the endtime.bat in following way

    @echo ---- Test 1
    @echo %starttime%
    @echo %start%
    @echo %endTime%

    for /F "tokens=1-4 delims=:.," %%a in ("%endTime%") do ( 
        @echo ---- Test 1.1
       IF %endTime% GTR %starttime% set /A "end=((((1%%a %% 100)*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100" 
       IF %endTime% LSS %starttime% set /A "end=(((((1%%a %% 100)+24)*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100" 
    )
    @echo ----test  3

produces ouput

2>    Task Parameter:Command=call ..\..\SDK\Scripts\endtimer.bat Common
2>    call ..\..\SDK\Scripts\endtimer.bat Common
2>    ---- Test 1
2>    10:28:38,90
2>    3771890
2>    10:28:58,82
2>    82 was unexpected at this time.

Minimum script to reproduce:

@echo ---- Test 1
set starttime=10:28:38,90
set start=3771890
set endTime=10:28:58,82
    @echo %starttime%
    @echo %start%
    @echo %endTime%

    for /F "tokens=1-4 delims=:.," %%a in ("%endTime%") do ( 
        @echo ---- Test 1.1
       IF %endTime% GTR %starttime% set /A "end=((((1%%a %% 100)*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100" 
       IF %endTime% LSS %starttime% set /A "end=(((((1%%a %% 100)+24)*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100" 
    )
    @echo ----test  3

Outputs:

C:\Users\joe>set endTime=10:28:58,82
10:28:38,90
3771890
10:28:58,82
82 was unexpected at this time.

*** Possible solution: Adding quotes around variables use din IF statements seems to help:

       IF %endTime% GTR %starttime% set /A "end=((((1%%a %% 100)*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100" 
       IF %endTime% LSS %starttime% set /A "end=(((((1%%a %% 100)+24)*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100" 

Additional observations I have also noticed that endtimer.bat and startimer.bat take long time to execute (around 5-10 seconds). Looks like this can be contributed to the following line in .bat file

powershell -Command "& {[Environment]::SetEnvironmentVariable(\"%projectName%_BuildStartTime\", $null, \"User\")"}

... which also takes a long time when executed directly from cmd. exe.

See the gap in powershell.exe as shown with ProcessMonitor:

10:53:34,6926501    powershell.exe  45124   RegOpenKey  HKCU\Environment    SUCCESS Desired Access: Read/Write
10:53:34,6926763    powershell.exe  45124   RegDeleteValue  HKCU\Environment\%projectName%_BuildStartTime   NAME NOT FOUND  
10:53:34,6927083    powershell.exe  45124   RegCloseKey HKCU\Environment    SUCCESS 
10:53:36,1197080    powershell.exe  45124   Thread Exit     SUCCESS Thread ID: 61752, User Time: 0.0000000, Kernel Time: 0.0000000
**** GAP ***
10:53:44,6601864    powershell.exe  45124   Thread Create       SUCCESS Thread ID: 11824
10:53:44,6607005    powershell.exe  45124   RegQueryKey HKLM    SUCCESS Query: HandleTags, HandleTags: 0x0
10:53:44,6607076    powershell.exe  45124   RegOpenKey  HKLM\Software\Microsoft\Windows NT\CurrentVersion\Diagnostics   NAME NOT FOUND  Desired Access: Read

I am using default PowerShell that comes with windows 11:

PS C:\Users\joe>  $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      22621  1778
ChrisMaunder commented 11 months ago

The fix to the "82 unexpected" is to add

set startTime=!startTime:,=.!
set endTime=!endTime:,=.!

before the lines

if "!startTime!" == "" set startTime=0
if "!endTime!" == ""   set endTime=0

There's no easy fix for the time delay. Using set and setx to store the build start time doesn't work. PowerShell gets around this, but is hideously slow. I'm removing the build timing from the project.

In server.csproj remove these lines:

<Target Name="PreBuild" BeforeTargets="PreBuildEvent" Condition="'$(IsWindows)'=='true'">
    <Exec Command="call ..\SDK\Scripts\starttimer.bat $(ProjectName)" />
</Target>

<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(IsWindows)'=='true'">
    <Exec Command="call ..\SDK\Scripts\endtimer.bat $(ProjectName)" />
</Target>