File size: 5,085 Bytes
c4c4d66
8c767a0
c4c4d66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c767a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4c4d66
 
 
 
 
 
 
 
 
 
8c767a0
c4c4d66
 
 
 
8c767a0
 
 
 
 
 
c4c4d66
8c767a0
c4c4d66
 
 
8c767a0
c4c4d66
 
 
8c767a0
c4c4d66
 
 
 
 
8c767a0
 
 
c4c4d66
8c767a0
 
c4c4d66
 
 
 
 
 
 
 
8c767a0
 
 
 
 
 
 
 
 
 
c4c4d66
8c767a0
 
 
 
 
 
 
 
 
c4c4d66
 
 
 
 
8c767a0
c4c4d66
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# UFC Model Update Script for Windows Task Scheduler
# This script safely updates models with resource limits and pushes logs to git

# Configuration
$PROJECT_DIR = "C:\Users\Alvaro\Desktop\ufc"  # Change this to your actual path
$LOG_DIR = "$PROJECT_DIR\logs"
$LOG_FILE = "$LOG_DIR\model_update.log"
$LOCK_FILE = "$LOG_DIR\update.lock"
$MAX_TIMEOUT_MINUTES = 120  # 2 hours timeout

# Create logs directory if it doesn't exist
if (!(Test-Path $LOG_DIR)) {
    New-Item -ItemType Directory -Path $LOG_DIR -Force
}

# Function to log with timestamp
function Write-Log {
    param($Message)
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    "[$timestamp] $Message" | Out-File -FilePath $LOG_FILE -Append
    Write-Host "[$timestamp] $Message"
}

# Function to safely execute git commands
function Invoke-GitCommand {
    param($Command, $Description)
    try {
        Write-Log "Git: $Description"
        $result = Invoke-Expression "git $Command" 2>&1
        if ($LASTEXITCODE -eq 0) {
            Write-Log "Git: $Description completed successfully"
            if ($result) { Write-Log "Git output: $result" }
            return $true
        } else {
            Write-Log "Git: $Description failed with exit code $LASTEXITCODE"
            Write-Log "Git error: $result"
            return $false
        }
    } catch {
        Write-Log "Git: Error executing $Description - $($_.Exception.Message)"
        return $false
    }
}

# Function to commit and push changes
function Push-ChangesToGit {
    Write-Log "Starting git operations..."
    
    # Check if we're in a git repository
    if (!(Test-Path ".git")) {
        Write-Log "Not in a git repository. Skipping git operations."
        return
    }
    
    # Check git status
    $gitStatus = git status --porcelain 2>&1
    if ($LASTEXITCODE -ne 0) {
        Write-Log "Failed to check git status. Skipping git operations."
        return
    }
    
    if ([string]::IsNullOrWhiteSpace($gitStatus)) {
        Write-Log "No changes to commit."
        return
    }
    
    # Add all changes
    if (!(Invoke-GitCommand "add ." "Adding all changes")) {
        return
    }
    
    # Create commit message with timestamp
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    $commitMessage = "Automated model update: $timestamp"
    
    # Commit changes
    if (!(Invoke-GitCommand "commit -m `"$commitMessage`"" "Committing changes")) {
        return
    }
    
    # Push to remote
    if (!(Invoke-GitCommand "push" "Pushing to remote repository")) {
        Write-Log "Failed to push changes. Changes are committed locally."
        return
    }
    
    Write-Log "Successfully pushed all changes to git repository."
}

# Check if another update is already running
if (Test-Path $LOCK_FILE) {
    Write-Log "Another update process is already running. Exiting."
    exit 1
}

# Create lock file
$PID | Out-File -FilePath $LOCK_FILE

try {
    Write-Log "Starting automated model update and git sync..."
    
    # Change to project directory
    Set-Location $PROJECT_DIR
    
    # Verify we're in the right directory
    if (!(Test-Path "src\main.py")) {
        Write-Log "Error: Not in UFC project directory. Expected to find src\main.py"
        exit 1
    }
    
    # Create a job to run the update with timeout
    Write-Log "Running model update pipeline..."
    $job = Start-Job -ScriptBlock {
        param($projectDir)
        Set-Location $projectDir
        python -m src.main --pipeline update 2>&1
    } -ArgumentList $PROJECT_DIR
    
    # Wait for job completion with timeout
    $updateSuccess = $false
    if (Wait-Job $job -Timeout ($MAX_TIMEOUT_MINUTES * 60)) {
        $result = Receive-Job $job
        $exitCode = $job.State
        
        if ($exitCode -eq "Completed") {
            Write-Log "Model update pipeline completed successfully."
            Write-Log "Pipeline output: $result"
            $updateSuccess = $true
        } else {
            Write-Log "Model update pipeline failed. State: $exitCode"
            Write-Log "Pipeline output: $result"
        }
    } else {
        Write-Log "Model update timed out after $MAX_TIMEOUT_MINUTES minutes. Stopping job."
        Stop-Job $job
    }
    
    Remove-Job $job -Force
    
    # Always attempt to push logs and any changes to git
    Write-Log "Syncing changes to git repository..."
    Push-ChangesToGit
    
    if ($updateSuccess) {
        Write-Log "Automated update completed successfully with git sync."
    } else {
        Write-Log "Automated update completed with errors, but logs have been synced to git."
    }
    
} catch {
    Write-Log "Critical error during automated update: $($_.Exception.Message)"
    
    # Try to push error logs to git
    try {
        Set-Location $PROJECT_DIR
        Push-ChangesToGit
    } catch {
        Write-Log "Failed to push error logs to git: $($_.Exception.Message)"
    }
} finally {
    # Cleanup lock file
    if (Test-Path $LOCK_FILE) {
        Remove-Item $LOCK_FILE -Force
    }
    Write-Log "Automated update process finished."
}