forked from RainbowMiner/RainbowMiner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsyncloader.psm1
107 lines (90 loc) · 5.01 KB
/
Asyncloader.psm1
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
function Start-AsyncLoader {
[cmdletbinding()]
Param(
[Parameter(Mandatory = $False)]
[int]$Interval = 60,
[Parameter(Mandatory = $False)]
[bool]$Quickstart = $false
)
if ($Interval -lt 60) {return}
if (-not (Test-Path ".\Cache")) {New-Item "Cache" -ItemType "directory" > $null}
$Global:AsyncLoader = [hashtable]::Synchronized(@{})
$AsyncLoader.Stop = $false
$AsyncLoader.Pause = $true
$AsyncLoader.Jobs = [hashtable]@{}
$AsyncLoader.CycleTime = 10
$AsyncLoader.Interval = $Interval
$AsyncLoader.Quickstart = if ($Quickstart) {0} else {-1}
$AsyncLoader.Verbose = $false
$AsyncLoader.Debug = $Session.LogLevel -eq "Debug"
# Setup runspace to launch the AsyncLoader in a separate thread
$newRunspace = [runspacefactory]::CreateRunspace()
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("AsyncLoader", $AsyncLoader)
$newRunspace.SessionStateProxy.SetVariable("Session", $Session)
$newRunspace.SessionStateProxy.Path.SetLocation($(pwd)) > $null
$AsyncLoader.Loader = [PowerShell]::Create().AddScript({
if ($AsyncLoader.Debug -and -not $psISE -and $Session.LogLevel -ne "Silent") {Start-Transcript ".\Logs\AsyncLoader_$(Get-Date -Format "yyyy-MM-dd_HH-mm-ss").txt"}
$ProgressPreference = "SilentlyContinue"
# Set the starting directory
if ($MyInvocation.MyCommand.Path) {Set-Location (Split-Path $MyInvocation.MyCommand.Path)}
Import-Module ".\Include.psm1"
Set-OsFlags
if ([Net.ServicePointManager]::SecurityProtocol -notmatch [Net.SecurityProtocolType]::Tls12) {
[Net.ServicePointManager]::SecurityProtocol += [Net.SecurityProtocolType]::Tls12
}
$Cycle = -1
$StopWatch = [System.Diagnostics.StopWatch]::New()
while (-not $AsyncLoader.Stop) {
$StopWatch.Restart()
$Cycle++
if (-not $AsyncLoader.Pause) {
foreach ($JobKey in @($AsyncLoader.Jobs.Keys | Sort-Object {$AsyncLoader.Jobs.$_.Index} | Select-Object)) {
$Job = $AsyncLoader.Jobs.$JobKey
if ($Job.CycleTime -le 0) {$Job.CycleTime = $AsyncLoader.Interval}
if (-not $AsyncLoader.Pause -and $Job -and -not $Job.Running -and -not $Job.Paused -and $Job.LastRequest -le (Get-Date).ToUniversalTime().AddSeconds(-$Job.CycleTime)) {
if ($AsyncLoader.Verbose) {
Write-ToFile -FilePath "Logs\errors_$(Get-Date -Format "yyyy-MM-dd").asyncloader.txt" -Message "Start job $JobKey with $($Job.Url) using $($Job.Method)" -Append -Timestamp
}
try {
Invoke-GetUrlAsync -Jobkey $Jobkey -force -quiet
if ($AsyncLoader.Jobs.$Jobkey.Error) {Write-ToFile -FilePath "Logs\errors_$(Get-Date -Format "yyyy-MM-dd").asyncloader.txt" -Message "Error job $JobKey with $($Job.Url) using $($Job.Method): $($AsyncLoader.Jobs.$Jobkey.Error)" -Append -Timestamp}
}
catch {
if ($Error.Count){$Error.RemoveAt(0)}
Write-ToFile -FilePath "Logs\errors_$(Get-Date -Format "yyyy-MM-dd").asyncloader.txt" -Message "Catch error job $JobKey with $($Job.Url) using $($Job.Method): $($_.Exception.Message)" -Append -Timestamp
}
finally {
if ($AsyncLoader.Verbose) {
Write-ToFile -FilePath "Logs\errors_$(Get-Date -Format "yyyy-MM-dd").asyncloader.txt" -Message "Done job $JobKey with $($Job.Url) using $($Job.Method)" -Append -Timestamp
}
}
}
}
}
if ($Error.Count) {if ($Session.LogLevel -ne "Silent") {$Error | Foreach-Object {Write-ToFile -FilePath "Logs\errors_$(Get-Date -Format "yyyy-MM-dd").asyncloader.txt" -Message "$($_.Exception.Message)" -Append -Timestamp}};$Error.Clear()}
$Delta = $AsyncLoader.CycleTime-$StopWatch.Elapsed.TotalSeconds
if ($Delta -gt 0) {Start-Sleep -Milliseconds ($Delta*1000)}
}
if ($AsyncLoader.Debug) {Stop-Transcript}
});
$AsyncLoader.Loader.Runspace = $newRunspace
$AsyncLoader.Handle = $AsyncLoader.Loader.BeginInvoke()
}
function Stop-AsyncLoader {
if (-not (Test-Path Variable:Global:Asyncloader)) {return}
$Global:AsyncLoader.Stop = $true
if ($Global:AsyncLoader.Loader) {$Global:AsyncLoader.Loader.dispose()}
$Global:AsyncLoader.Loader = $null
$Global:AsyncLoader.Handle = $null
Remove-Variable "AsyncLoader" -Scope Global -Force
}
function Stop-AsyncJob {
[cmdletbinding()]
Param(
[Parameter(Mandatory = $True)]
[string]$tag
)
if (-not (Test-Path Variable:Global:Asyncloader)) {return}
foreach ($Jobkey in @($AsyncLoader.Jobs.Keys | Select-Object)) {if ($AsyncLoader.Jobs.$Jobkey.Tag -eq $tag) {$AsyncLoader.Jobs.$Jobkey.Paused=$true}}
}