-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a new mechanism for logging jobs into a database in #13
- Loading branch information
Showing
8 changed files
with
241 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
|
||
<# | ||
If ( $isDuckDBLoaded -eq $true ) | ||
# Resolve path first | ||
$absolutePath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($script:settings.joblogDB) | ||
# Build Connection string | ||
$connString = "DataSource=$( $absolutePath )" | ||
# Add connection to duckdb | ||
Add-DuckDBConnection -Name "JobLog" -ConnectionString $connString | ||
Incremental ID | ||
Guid/ProcessID | ||
Plugin | ||
Debug Yes/No | ||
JobType (Upload, Messages, Lists, Preview) | ||
InputHashtable as JSON | ||
InputRecordsCount | ||
CreateDateTime | ||
UpdateDateTime | ||
FinishDateTime | ||
TotalTime | ||
output is the returned hashtable | ||
Return an ID | ||
CREATE TABLE IF NOT EXISTS joblog ( | ||
id INTEGER PRIMARY KEY | ||
,created TEXT DEFAULT (STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW', 'localtime')) | ||
,updated TEXT | ||
,finished INTEGER | ||
,status TEXT | ||
,process TEXT | ||
,plugin TEXT | ||
,debug INTEGER | ||
,type TEXT | ||
,input TEXT | ||
,inputrecords INTEGER | ||
,successful INTEGER | ||
,failed INTEGER | ||
,totalseconds INTEGER | ||
,output TEXT | ||
) | ||
CREATE TRIGGER IF NOT EXISTS update_joblog_trigger | ||
AFTER UPDATE On joblog | ||
BEGIN | ||
UPDATE joblog SET updated = STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW', 'localtime') WHERE id = NEW.id; | ||
END; | ||
And then allow to execute the job type | ||
Then when creating a new row with | ||
insert into joblog (status) values ('abc'); | ||
select last_insert_rowid() | ||
you get back the id value | ||
https://duckdb.org/docs/extensions/sqlite#writing-data-to-sqlite | ||
INSERT INTO sqlite_db.tbl VALUES (42, 'DuckDB'); | ||
INSTALL sqlite; | ||
LOAD sqlite; | ||
ATTACH 'C:\Users\Florian\Downloads\a pet co\apetco.sqlite' AS apetco (TYPE sqlite); | ||
use apetco; | ||
select * from kontakte; | ||
-- select * from duckdb_extensions(); | ||
#> | ||
|
||
|
||
Function Add-JobLog { | ||
<# | ||
... | ||
#> | ||
[cmdletbinding()] | ||
param( | ||
# [Parameter(Mandatory=$true)][String]$Guid | ||
#,[Parameter(Mandatory=$true)][String]$ConnectionString | ||
# TODO also allow use other connection strings as input parameter? | ||
) | ||
|
||
Process { | ||
|
||
# TODO check if connection is open? | ||
|
||
Read-DuckDBQueryAsScalar -Name "JobLog" -Query "INSERT INTO joblog (process) values ('$( $Script:processId )'); SELECT last_insert_rowid()" | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Delete some records if the log gets too big |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
Function Set-JobLogDatabase { | ||
[cmdletbinding()] | ||
param( | ||
) | ||
|
||
Process { | ||
|
||
# Resolve path first | ||
$absolutePath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($script:settings.joblogDB) | ||
|
||
# Build Connection string | ||
$connString = "DataSource=$( $absolutePath )" | ||
|
||
# Add connection to duckdb | ||
Add-DuckDBConnection -Name "JobLog" -ConnectionString $connString | ||
|
||
# Open the connection | ||
Open-DuckDBConnection -Name "JobLog" | ||
|
||
# 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" | ||
|
||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
Function Update-JobLog { | ||
<# | ||
... | ||
#> | ||
[cmdletbinding()] | ||
param( | ||
|
||
[Parameter(Mandatory=$true)][Int]$JobId # The Job ID that you have already got from the database | ||
|
||
# Values to change | ||
,[Parameter(Mandatory=$false)][Switch]$Finished = $false # Finished like 0 or 1 | ||
,[Parameter(Mandatory=$false)][String]$Status = "" # Status like "Finished" and others | ||
#,[Parameter(Mandatory=$false)][String]$Process = "" # Process ID | ||
,[Parameter(Mandatory=$false)][String]$Plugin = "" # Plugin guid | ||
,[Parameter(Mandatory=$false)][Int]$DebugMode = 0 # Debug mode like 0 or 1 | ||
,[Parameter(Mandatory=$false)][String]$Type = "" # Type like UPLOAD, MESSAGES, LISTS etc. | ||
,[Parameter(Mandatory=$false)][String]$Input = "" # Input hashtable | ||
,[Parameter(Mandatory=$false)][Int]$Inputrecords = 0 # Number of records that have been put in | ||
,[Parameter(Mandatory=$false)][Int]$Successful = 0 # Successful records, only needed on uploads | ||
,[Parameter(Mandatory=$false)][Int]$Failed = 0 # Failed records, only needed on uploads | ||
,[Parameter(Mandatory=$false)][Int]$Totalseconds = 0 # Seconds in total, logged at the end | ||
,[Parameter(Mandatory=$false)][String]$Output = "" # Output hashtable (if it is suitable) | ||
) | ||
|
||
Process { | ||
|
||
$sb = [System.Text.StringBuilder]::new() | ||
$sb.Append("UPDATE joblog SET ") | ||
|
||
$upd = [System.Collections.ArrayList]@() | ||
[void]$upd.Add("finished = $( $Finished )") | ||
[void]$upd.Add("status = '$( $Status )'") | ||
#[void]$upd.Add("process = '$( $Process )'") | ||
[void]$upd.Add("plugin = '$( $Plugin )'") | ||
[void]$upd.Add("debug = $( $DebugMode )") | ||
[void]$upd.Add("type = '$( $Type )'") | ||
[void]$upd.Add("input = '$( $Input )'") | ||
[void]$upd.Add("inputrecords = $( $Inputrecords )") | ||
[void]$upd.Add("successful = $( $Successful )") | ||
[void]$upd.Add("failed = $( $Failed )") | ||
[void]$upd.Add("totalseconds = $( $Totalseconds )") | ||
[void]$upd.Add("output = '$( $Output )'") | ||
$params = $upd -join ", " | ||
$sb.Append($params) | ||
|
||
$sb.Append("WHERE id = $( $JobId )") | ||
|
||
#$query = "update logjob set process = 'abc' where id = $( $JobId )" | ||
Invoke-DuckDBQueryAsNonExecute -Query $sb.ToString() -ConnectionName "JobLog" | ||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
CREATE TABLE IF NOT EXISTS joblog ( | ||
id INTEGER PRIMARY KEY | ||
,created TEXT DEFAULT (STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW', 'localtime')) | ||
,updated TEXT | ||
,finished INTEGER DEFAULT 0 | ||
,status TEXT | ||
,process TEXT | ||
,plugin TEXT | ||
,debug INTEGER | ||
,type TEXT | ||
,input TEXT | ||
,inputrecords INTEGER | ||
,successful INTEGER | ||
,failed INTEGER | ||
,totalseconds INTEGER | ||
,output TEXT | ||
); | ||
|
||
CREATE TRIGGER IF NOT EXISTS update_joblog_trigger | ||
AFTER UPDATE On joblog | ||
BEGIN | ||
UPDATE joblog SET updated = STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW', 'localtime') WHERE id = NEW.id; | ||
END; |