-
-
Notifications
You must be signed in to change notification settings - Fork 92
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
Accessing a variable set inside a timer from a route #40
Comments
I have an idea on how this could be possible, but i'll need to experiment with it. Basically: since the timers are all running in a separate runspace, the variables defined within the timers will only be within the scope of that runspace - so inaccessible to routes/loggers. My thinking is that runspaces have a defined session state variable, and maybe it could be possible to have a "user defined" hashtable within that state where you could tell pode to set variables against it so they're accessible everywhere. ie: timer 'forever' 5 {
$some_hashtable = @{}
# will add hashtable to user-defined session state, calling it 'earth'
state add 'earth' $some_hashtable
# give it some data
$some_hashtable['hello'] = 'world'
}
route 'get' '/' {
param($session)
# in theory this should write out the text 'world'
$user_state['earth']['hello'] | Out-Default
# or keep to method syntax:
(state get 'earth').hello | Out-Default
} I'm not too sure what's in your hashtable, but for now it could be possible to |
I figured that the variables wouldn't be accessible due to the runspace. I thought about writing to a file and then reading back in the route, but wanted to check in with you if there was a cleaner way of doing this via a runspace state variable or something like that. Thanks for the detailed response. Much appreciated. |
Commit above adds in the |
Tested, and accessing variables I set in a timer from a route is working for me now. I have started to see some exceptions though running with this version. They happen about 10-15 minutes after starting up pode, so things work for a while, and then these exceptions occur. Note that I have some browser-side javascript that is calling api routes served by pode every 1-5 seconds.
another one
|
Real headache this one. I can't seem to get the global error you've put, but I can get the other error plus more. These errors don't seem to happen in master, so seems it definitely caused by this work. It appears to happen when under load, and when you have a route and timer altering the same shared state variable - good old multi threading :/ I've put in some additional error logging, and also locking in the State function. Although the rate of errors has dropped they can still happen. I'll keep digging. |
Yeah, I've had pode under similar load before without any issue, but when I try to make frequent changes to these state variables in timers and read them in routes, I run into issues pretty quick. |
This should now be all right however, you'll need to update anywhere you're using timer 'forever' 2 {
param($session)
$hash = $null
lock $session.Lockable {
if (($hash = (state get 'hash')) -eq $null) {
$hash = (state set 'hash' @{})
$hash['values'] = @()
}
$hash['values'] += (Get-Random -Minimum 0 -Maximum 10)
}
}
route get '/get-array' {
param($session)
lock $session.Lockable {
$hash = (state get 'hash')
json $hash
}
} I've made it so that routes, timers and loggers each have a I've tested it with quite a bit of load:
The latter two I hit 100 times per second. Before the locking, lots of errors, after it there no errors after multiple runs. |
Ok, not sure why, but with this latest commit, I'm getting an exception thrown on the first page load.
Let me know if you want me to send along the code for the server I'm running. Once that error is thrown, I will continue to get the same error until I actually close my powershell window and reopen it:
|
That "Web" error was the one I was getting most frequently until I put locking in place. If it's all right I might need some of the code you're using for the server that reproduces the issue? |
I've cleaned things up a bit and removed everything non-essential. In the attached zip is my server script, a basicindex.pode view file, and the javascript that basicindex.pode file calls that runs the api calls. |
Excellent, thank you. I've added in a bunch of logging, and managed to narrow it down to precisely when it happens. The moment you make a web request at the precise moment a timer is within the Very odd. There are no errors, nothing. Just somehow, it's... null 😕 |
@ssugar Turns out it was some weird behaviour on |
Using your latest commit, the prior exceptions are gone. Seeing this exception after about 20-30 seconds though:
|
I've a feeling that might be caused by something else; is there a chance a route is trying to set a a response twice? That's when that error gets thrown, as the response has already had content set against it. |
Or maybe not! I just got it on your example files. But... it was working yesterday o-o
|
@ssugar Any luck with this one? I've had the scripts you gave me running fine for 2hrs and going. |
@Badgerati - Got it running now. Will keep you up-to-date |
@ssugar Response sizes should be back to normal now |
@Badgerati - Running it now. Response sizes look good again. |
@Badgerati - Stability is good too. Amazing. |
@ssugar Still going strong? |
@Badgerati - Yup, been going strong for the last 12 hours. No exceptions thrown. Looks great. Edit: over 24 hours running smoothly now |
Excellent, sounds like it's all fine now; in which case I'll do some extra testing tonight and then release it |
@Badgerati - Sounds good to me. |
I'd like to have a Server timer running every X seconds that will pull some data and update a hash table. I would then like to read from that hash table when a specific api route is called.
I haven't been able to figure out a way to do that. I looked through the readme and the timer example, but that is more about how to create/set a timer, and not about the logic and variables within the timer. Any guidance would be appreciated.
The text was updated successfully, but these errors were encountered: