Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New File Watcher logic, to run logic on file events #1063

Closed
Badgerati opened this issue Jan 12, 2023 · 0 comments · Fixed by #1064
Closed

New File Watcher logic, to run logic on file events #1063

Badgerati opened this issue Jan 12, 2023 · 0 comments · Fixed by #1064
Assignees
Milestone

Comments

@Badgerati
Copy link
Owner

Badgerati commented Jan 12, 2023

Use Register-ObjectEvent on FileSystemWatcher objects, but do the register in a new runspace - and we only need 1 runspace as well, multiple won't work here.

Need to make sure the FileSystemWatchers are all disposed on server restart and stop.

Add-PodeFileWatcher `
    -Name <string> `
    -Path '<directory>' `
    -ScriptBlock {} `
    [-FilePath <string>] `
    [-ArgumentList @()] `
    [-EventName @('Changed', 'Created', 'Deleted', 'Renamed' | *)] `
    [-NotifyFilter @([System.IO.NotifyFilters]'FileName,DirectoryName,LastWrite,CreationTime')] `
    [-Include '*.*'] `
    [-Exclude @()] `
    [-NoSubdirectories]
  • The 'Error' event is monitored, but is done internal within Pode
  • Default events are Changed, Created, Deleted, and Renamed (ie: *)
  • Default NotifyFilters are FileName, DirectoryName, LastWrite, CreationTime
  • Functions also for: Test, Get, Remove, Clear, Use

Scriptblock parameters:

-ScriptBlock {
    param($args0, ..., $argsX)

    $FileEvent = @{
        Type = <Changed, Created, Deleted, Renamed>
        FullPath = <FullPath>
        Name = <Name>

        # if Renamed event type
        Old = @{
            FullPath = <FullPath>
            Name = <Name>
        }

        DateTime = <Now>
        Etc.
    }
}

Example, to watch a C:\temp folder and all *.txt files - for changed events only:

Add-PodeFileWatcher -Path 'C:\temp' -Include '*.txt' -ScriptBlock {
    "[$($FileEvent.Type)]: $($FileEvent.Name)" | Out-Default
}

We could also support :parameter placeholders in wildcard paths. For example: C:/Websites/:website would monitor files changes at C:/Websites/*, and the "website" parameter would be the name of the site available at $FileEvent.Parameters['website'] - similar to Routes.

@Badgerati Badgerati converted this from a draft issue Jan 12, 2023
@Badgerati Badgerati self-assigned this Jan 12, 2023
@Badgerati Badgerati moved this from Backlog to In Progress in 🚀 Pode Roadmap Jan 12, 2023
@Badgerati Badgerati added this to the 2.8.0 milestone Jan 12, 2023
Badgerati added a commit that referenced this issue Jan 12, 2023
Badgerati added a commit that referenced this issue Jan 12, 2023
…upport for running watchers on their own - without a web server
Badgerati added a commit that referenced this issue Jan 15, 2023
@github-project-automation github-project-automation bot moved this from In Progress to Done in 🚀 Pode Roadmap Jan 15, 2023
@Badgerati Badgerati mentioned this issue Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant