Skip to content

Commit

Permalink
Merge 8e607c7 into 80643da
Browse files Browse the repository at this point in the history
  • Loading branch information
Badgerati committed Dec 2, 2019
2 parents 80643da + 8e607c7 commit 9f1ba43
Show file tree
Hide file tree
Showing 16 changed files with 261 additions and 95 deletions.
6 changes: 3 additions & 3 deletions .github/ISSUE_TEMPLATE/enhancement-request.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ assignees: ''

---

### Related Issues
A clear and concise description of what the problem is, or a link/reference to the issue.

### Describe the Change
A clear and concise description of what you want to happen.

### Related Issues
A clear and concise description of what the problem is, or a link/reference to the issue.

### Additional Context
Add any other context or screenshots about the enhancement request here.
6 changes: 3 additions & 3 deletions .github/ISSUE_TEMPLATE/feature-request.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ assignees: ''

---

### Related Issues
A clear and concise description of what the problem is, or a link/reference to the issue.

### Describe the Feature
A clear and concise description of what you want to happen.

### Related Issues
A clear and concise description of what the problem is, or a link/reference to the issue.

### Additional Context
Add any other context or screenshots about the feature request here.
11 changes: 11 additions & 0 deletions .github/ISSUE_TEMPLATE/question.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
name: Question
about: Ask a question about Pode's features, examples, or documentation
title: ''
labels: 'question :grey_question:'
assignees: ''

---

### Question
A clear and concise description of your question.
2 changes: 1 addition & 1 deletion docs/Tutorials/ImportingModules.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Importing Modules/SnapIns

Because Pode runs most things in isolated runspaces, importing and using modules or snap-ins can be quite bothersome. To overcome this, you can use the [`Import-PodeModule`](../../Functions/Utilities/Import-PodeModule) or [`Import-PodeModule`](../../Functions/Utilities/Import-PodeModule) functions to declare paths/names of modules or snap-ins that need to be imported into all of the runspaces.
Because Pode runs most things in isolated runspaces, importing and using modules or snap-ins can be quite bothersome. To overcome this, you can use the [`Import-PodeModule`](../../Functions/Utilities/Import-PodeModule) or [`Import-PodeSnapIn`](../../Functions/Utilities/Import-PodeSnapIn) functions to declare paths/names of modules or snap-ins that need to be imported into all of the runspaces.

!!! important
Snap-ins are only supported in Windows PowerShell.
Expand Down
2 changes: 1 addition & 1 deletion docs/Tutorials/Restarting/Overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ There are 3 ways to restart a running Pode server:
2. [**File Monitoring**](../Types/FileMonitoring): This will watch for file changes, and if enabled will trigger the server to restart.
3. [**Auto-Restarting**](../Types/AutoRestarting): Defined within the `server.psd1` configuration file, you can set schedules for the server to automatically restart.

When the server restarts, it will re-invoke the `scripblock` supplied to the [`Start-PodeServer`](../../../Functions/Core/Start-PodeServer) function. This means the best approach to reload new modules/scripts it to dot-source your scripts into your server, as any changes to the main `scriptblock` will **not** take place.
When the server restarts, it will re-invoke the `-ScriptBlock` supplied to the [`Start-PodeServer`](../../../Functions/Core/Start-PodeServer) function. This means the best approach to reload new modules/scripts it to dot-source/[`Use-PodeScript`](../../../Functions/Utilities/Use-PodeScript) your scripts into your server, as any changes to the main `scriptblock` will **not** take place.
2 changes: 1 addition & 1 deletion docs/Tutorials/SharedState.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Start-PodeServer {
$hash = (Get-PodeState -Name 'hash')
# add a random number
$hash['values'] += (Get-Random -Minimum 0 -Maximum 10)
$hash.values += (Get-Random -Minimum 0 -Maximum 10)
# save the state to file
Save-PodeState -Path './state.json'
Expand Down
13 changes: 13 additions & 0 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Release Notes

## v1.2.1

```plain
### Enhancements
* #415: New functions for invoking Timer and Schedules adhoc
### Bugs
* #416: Fix for using `*/INT` in cron-expressions
### Documentation
* #418: Docs and examples typo fixes
```

## v1.2.0

```plain
Expand Down
2 changes: 1 addition & 1 deletion examples/external-funcs.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Start-PodeServer {
Add-PodeEndpoint -Address * -Port 8085 -Protocol Http

# include the external function module
Use-PodeScript -Path './modules/external-funcs.psm1'
Import-PodeModule -Path './modules/external-funcs.psm1'

# GET request for "localhost:8085/"
Add-PodeRoute -Method Get -Path '/' -ScriptBlock {
Expand Down
5 changes: 5 additions & 0 deletions examples/schedules.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ Start-PodeServer {
# logic
} -EndTime ([DateTime]::Now.AddHours(2))

# adhoc invoke a schedule's logic
Add-PodeRoute -Method Get -Path '/api/run' -ScriptBlock {
Invoke-PodeSchedule -Name 'predefined'
}

}
7 changes: 6 additions & 1 deletion examples/timers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Start-PodeServer {
# runs forever, looping every 5secs
Add-PodeTimer -Name 'forever' -Interval 5 -ScriptBlock {
'Hello, world' | Out-PodeHost
}
} -Limit 5

# runs forever, but skips the first 3 "loops" - is paused for 15secs then loops every 5secs
Add-PodeTimer -Name 'pause-first-3' -Interval 5 -ScriptBlock {
Expand Down Expand Up @@ -44,4 +44,9 @@ Start-PodeServer {
}
}

# adhoc invoke a timer's logic
Add-PodeRoute -Method Get -Path '/api/run' -ScriptBlock {
Invoke-PodeTimer -Name 'forever'
}

}
2 changes: 2 additions & 0 deletions src/Pode.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,13 @@
'Add-PodeSchedule',
'Remove-PodeSchedule',
'Clear-PodeSchedule',
'Invoke-PodeSchedule',

# timers
'Add-PodeTimer',
'Remove-PodeTimer',
'Clear-PodeTimers',
'Invoke-PodeTimer',

# middleware
'Add-PodeMiddleware',
Expand Down
6 changes: 4 additions & 2 deletions src/Private/CronParser.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ function ConvertFrom-PodeCronExpression
}

# replace * with min/max constraint
$_atom = $_atom -ireplace '\*', ($_constraint -join '-')
if ($_atom -ieq '*') {
$_atom = ($_constraint -join '-')
}

# parse the atom for either a literal, range, array, or interval
# literal
Expand Down Expand Up @@ -198,7 +200,7 @@ function ConvertFrom-PodeCronExpression
$interval = '1'
}

if ([string]::IsNullOrWhiteSpace($start) -or $start -ieq '*') {
if ([string]::IsNullOrWhiteSpace($start) -or ($start -ieq '*')) {
$start = '0'
}

Expand Down
149 changes: 83 additions & 66 deletions src/Private/Schedules.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,106 +17,123 @@ function Start-PodeScheduleRunspace
}

$script = {
function Invoke-PodeInternalSchedule($Schedule, $Now)
{
$Schedule.OnStart = $false
$remove = $false

# increment total number of triggers for the schedule
if ($Schedule.Countable) {
$Schedule.Count++
$Schedule.Countable = ($Schedule.Count -lt $Schedule.Limit)
}

# check if we have hit the limit, and remove
if (($Schedule.Limit -ne 0) -and ($Schedule.Count -ge $Schedule.Limit)) {
$remove = $true
}

# trigger the schedules logic
try {
$parameters = @{
Event = @{
Lockable = $PodeContext.Lockable
}
}

foreach ($key in $Schedule.Arguments.Keys) {
$parameters[$key] = $Schedule.Arguments[$key]
}

Add-PodeRunspace -Type Schedules -ScriptBlock (($Schedule.Script).GetNewClosure()) -Parameters $parameters -Forget
}
catch {
$_ | Write-PodeErrorLog
}

# reset the cron if it's random
$Schedule.Crons = Reset-PodeRandomCronExpressions -Expressions $Schedule.Crons
return $remove
}

function Remove-PodeInternalSchedules([string[]] $Schedules)
{
# add any schedules to remove that have exceeded their end time
$Schedules += @($PodeContext.Schedules.Values |
Where-Object { (($null -ne $_.EndTime) -and ($_.EndTime -lt $_now)) }).Name

if (($null -eq $Schedules) -or ($Schedules.Length -eq 0)) {
return
}

# remove any schedules
foreach ($name in $Schedules) {
if ($PodeContext.Schedules.ContainsKey($name)) {
$PodeContext.Schedules.Remove($name) | Out-Null
}
}
}

# select the schedules that trigger on-start
$_remove = @()
$_now = [DateTime]::Now

$PodeContext.Schedules.Values |
Where-Object {
$_.OnStart
} | ForEach-Object {
if (Invoke-PodeInternalSchedule -Schedule $_ -Now $_now) {
$_remove += $_.Name
if (Invoke-PodeInternalSchedule -Schedule $_) {
$_.Completed = $true
}
}

# remove any schedules
Remove-PodeInternalSchedules -Schedules $_remove
Remove-PodeInternalSchedules -Now $_now

# first, sleep for a period of time to get to 00 seconds (start of minute)
Start-Sleep -Seconds (60 - [DateTime]::Now.Second)

while ($true)
{
$_remove = @()
$_now = [DateTime]::Now

# select the schedules that need triggering
$PodeContext.Schedules.Values |
Where-Object {
(($null -eq $_.StartTime) -or ($_.StartTime -le $_now)) -and
(($null -eq $_.EndTime) -or ($_.EndTime -ge $_now)) -and
(Test-PodeCronExpressions -Expressions $_.Crons -DateTime $_now)
(Test-PodeCronExpressions -Expressions $_.Crons -DateTime $_now) -and !$_.Completed
} | ForEach-Object {
if (Invoke-PodeInternalSchedule -Schedule $_ -Now $_now) {
$_remove += $_.Name
if (Invoke-PodeInternalSchedule -Schedule $_) {
$_.Completed = $true
}
}

# remove any schedules
Remove-PodeInternalSchedules -Schedules $_remove
Remove-PodeInternalSchedules -Now $_now

# cron expression only goes down to the minute, so sleep for 1min
Start-Sleep -Seconds (60 - [DateTime]::Now.Second)
}
}

Add-PodeRunspace -Type 'Main' -ScriptBlock $script
}

function Remove-PodeInternalSchedules
{
param(
[Parameter(Mandatory=$true)]
[datetime]
$Now
)

# add any schedules to remove that have exceeded their end time
$Schedules = @($PodeContext.Schedules.Values |
Where-Object { (($null -ne $_.EndTime) -and ($_.EndTime -lt $Now)) })

if (($null -eq $Schedules) -or ($Schedules.Length -eq 0)) {
return
}

# set any expired schedules as being completed
$Schedules | ForEach-Object {
$_.Completed = $true
}
}

function Invoke-PodeInternalSchedule
{
param(
[Parameter(Mandatory=$true)]
$Schedule
)

$Schedule.OnStart = $false
$remove = $false

# increment total number of triggers for the schedule
if ($Schedule.Countable) {
$Schedule.Count++
$Schedule.Countable = ($Schedule.Count -lt $Schedule.Limit)
}

# check if we have hit the limit, and remove
if (($Schedule.Limit -ne 0) -and ($Schedule.Count -ge $Schedule.Limit)) {
$remove = $true
}

# trigger the schedules logic
Invoke-PodeInternalScheduleLogic -Schedule $Schedule

# reset the cron if it's random
$Schedule.Crons = Reset-PodeRandomCronExpressions -Expressions $Schedule.Crons
return $remove
}

function Invoke-PodeInternalScheduleLogic
{
param(
[Parameter(Mandatory=$true)]
$Schedule
)

try {
$parameters = @{
Event = @{
Lockable = $PodeContext.Lockable
}
}

foreach ($key in $Schedule.Arguments.Keys) {
$parameters[$key] = $Schedule.Arguments[$key]
}

Add-PodeRunspace -Type Schedules -ScriptBlock (($Schedule.Script).GetNewClosure()) -Parameters $parameters -Forget
}
catch {
$_ | Write-PodeErrorLog
}
}
Loading

0 comments on commit 9f1ba43

Please sign in to comment.