Skip to content

Commit

Permalink
Changed the joblog functions to always check for an active connection…
Browse files Browse the repository at this point in the history
… and suppress problems for #13
  • Loading branch information
gitfvb committed Jul 16, 2024
1 parent 4c2557a commit faeb6a4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 34 deletions.
10 changes: 1 addition & 9 deletions AptecoPSFramework/public/jobs/Add-JobLog.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,7 @@ Function Add-JobLog {

Process {

$c = Get-SqlConnection -ConnectionName "JobLog"
If ( $c.State -eq "Open" ) {
# All good!
} else {
Set-JobLogDatabase
}


#Read-DuckDBQueryAsScalar -Name "JobLog" -Query "INSERT INTO joblog (process) values ('$( $Script:processId )'); SELECT last_insert_rowid()"
Set-JobLogDatabase

Invoke-SqlScalar -ConnectionName "JobLog" -Query "INSERT INTO joblog (process) values ('$( $Script:processId )'); SELECT last_insert_rowid()"

Expand Down
7 changes: 6 additions & 1 deletion AptecoPSFramework/public/jobs/Close-JobLogDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ Function Close-JobLogDatabase {

Process {

Close-SqlConnection -ConnectionName "JobLog"
# just try it
try {
Close-SqlConnection -ConnectionName "JobLog" -ErrorAction SilentlyContinue
} catch {

}

}

Expand Down
8 changes: 1 addition & 7 deletions AptecoPSFramework/public/jobs/Get-JobLog.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ Function Get-JobLog {
# CHECK CONNECTION
#-----------------------------------------------

$c = Get-SqlConnection -ConnectionName "JobLog"
If ( $c.State -eq "Open" ) {
# All good!
} else {
Set-JobLogDatabase
}
Set-JobLogDatabase


#-----------------------------------------------
Expand Down Expand Up @@ -134,7 +129,6 @@ Function Get-JobLog {

$job


}

}
35 changes: 24 additions & 11 deletions AptecoPSFramework/public/jobs/Set-JobLogDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ Function Set-JobLogDatabase {

Process {

# Resolve path first
$absolutePath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($script:settings.joblogDB)

<#
# Build Connection string
$connString = "DataSource=$( $absolutePath )"
Expand All @@ -20,16 +17,32 @@ Function Set-JobLogDatabase {
Open-DuckDBConnection -Name "JobLog"
#>

# Open the connection
Open-SQLiteConnection -ConnectionName "JobLog" -DataSource $absolutePath
$connectDatabase = $true
try {
$c = Get-SqlConnection -ConnectionName "JobLog" -ErrorAction SilentlyContinue
If ( $c.State -eq "Open" ) {
$connectDatabase = $false
}
} catch {
# Still connect here
}

If ( $connectDatabase -eq $true ) {

# Resolve path first
$absolutePath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($script:settings.joblogDB)

# Open the connection
Open-SQLiteConnection -ConnectionName "JobLog" -DataSource $absolutePath

# Create the database, if not exists
$joblogCreateStatementPath = Join-Path -Path $Script:moduleRoot -ChildPath "sql/joblog_create.sql"
$joblogCreateStatement = Get-Content -Path $joblogCreateStatementPath -Encoding UTF8 -Raw
#Invoke-DuckDBQueryAsNonExecute -Query $joblogCreateStatement -ConnectionName "JobLog"

$u = Invoke-SqlUpdate -ConnectionName "JobLog" -Query $joblogCreateStatement
# Create the database, if not exists
$joblogCreateStatementPath = Join-Path -Path $Script:moduleRoot -ChildPath "sql/joblog_create.sql"
$joblogCreateStatement = Get-Content -Path $joblogCreateStatementPath -Encoding UTF8 -Raw
#Invoke-DuckDBQueryAsNonExecute -Query $joblogCreateStatement -ConnectionName "JobLog"
$u = Invoke-SqlUpdate -ConnectionName "JobLog" -Query $joblogCreateStatement

}

}

Expand Down
7 changes: 1 addition & 6 deletions AptecoPSFramework/public/jobs/Update-JobLog.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,7 @@ Function Update-JobLog {
# CHECK CONNECTION
#-----------------------------------------------

$c = Get-SqlConnection -ConnectionName "JobLog"
If ( $c.State -eq "Open" ) {
# All good!
} else {
Set-JobLogDatabase
}
Set-JobLogDatabase


#-----------------------------------------------
Expand Down

0 comments on commit faeb6a4

Please sign in to comment.