-
Notifications
You must be signed in to change notification settings - Fork 12
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
Occasional crash without correct error handling while using Magister.ready(callback) #46
Comments
After doing error handling I found out that occasionally an error is produced that causes the problems: |
Is that error correct? |
The error is occasionally (about once every 5-10 times) presented when using this code:
Since I'm using the exact same credentials everytime I call the function, I am sure that the credentials are valid. So the "invalid account" or "wrong username / password" error is not correct. |
Okay that's weird, I will look into it. |
I found out that the error happens when I do a new Magister.ready(callback) before the previous call has fully finished loading data. E.g. loading all grades can take 5 seconds, and in the mean time node.js will already start the next Magister.ready(callback) e.g. to get appointments > the error then happens. |
So I think it has to do with opening multiple sessions to Magister simultaneously |
@lieuwex : off topic: do you know if the student id ( profileInfo().id() ) is unique globally? I mean if a student moves to another school, will he keep the same id? Or is the school in charge of "making up" a unique id only within their own school domain? And I have the same question for the username. (And why is the username not the same as the id?) |
That seems like it, I would not use use
AFAIK the IDs are only unique inside the 'school domain', so two schools could have overlapping IDs. If you want to have unique IDs I advise you to prefix
I guess that has to do with the fact that the student ID is always numerical, but the username doesn't have to be. Most of the times it is, but it could contain letters too AFAIK. |
I will take a look to if I can improve the error when multiple sessions are created to Magister simultaneously |
Mine is completely alphabetical: tkonings |
@lieuwex Thx for the answer on the off-topics! On topic however I think that an improved error or using setTimeout to prevent multiple sessions from occuring is fighting the symptom in stead of fighting the cause of the problem. In my opinion it should always be possible to have multiple simultaneous sessions open. Since this is for Node.JS I never know when an event occurs that will trigger a session. And because of asynchronous execution of the code it is very likely that multiple sessions will be opened at certain times. The use case is as described before:
|
@gruijter Yeah, I will try to fix it. But I wouldn't open multiple sessions to Magister anyway since it will trigger the ratelimit sooner and there isn't really a reason for it in your usecase. |
@lieuwex : Thx! and what is the ratelimit? Is there any documentation I can read to learn about these kind of things? I can only find a (not very comprehensive) document at: http://simplyapps.nl/MagisterJS/docs/index.html |
@lieuwex: any news on handling multiple sessions simultaneously? I am now not able to use the MagisterJS because of the limitation. It causes erratic/unpredictable behavior in the app I wanted to create. |
@gruijter It seems that logging at the same time is just not possible using Magister, so it isn't really an issue with Magister.js. I recommend doing a workaround in your own code. For example creating a wrapper which makes sure new Magister objects are only created after the old one has been done logging in or by caching Magister instances (also adds a performance bonus by not having to login all the time). |
I'm going to second @lieuwex's last sentence. It sounds like you're logging in multiple times with the same credentials, which isn't really how the library is designed. Log in once and cache the Magister object; it will handle any necessary re-logins (because the session might expire for example) by itself, without any intervention from your side. You'll issue far fewer requests, and will have a much lower chance of hitting the ratelimit. And if you do hit it, it's magister that's wanting you to stop, not this library. :) |
ok, thanks for this insight! Could you give an example code on how to cache the entire magister object? I need to get the profileInfo, the currentCourse (classes and grades) and appointments. |
Sorry for the delay, but this is an example of caching the magister object, this requires the lru-cache module. If you only want to cache 1 to 5 magister objects a LRU cache isn't really needed and you could just use an object. var Magister = require('magister.js').Magister;
var LRU = require('lru-cache');
const cache = LRU({
max: 50,
maxAge: 1000 * 60 * 60 * 2, // 2 hours
});
var getMagisterObject = function (schoolurl, username, password) {
var id = schoolurl + '_' + username;
var magister = cache.get(id);
if (magister == null) {
magister = new Magister({
school: {
url: schoolurl,
},
username: username,
password: password,
});
cache.set(id, magister);
}
return magister;
};
// Usage example:
getMagisterObject(
'https://<schoolname>.magister.net',
'<username>',
'<password>'
).ready(function (err) {
if (err != null) {
console.error(err);
return;
}
// code here
}); |
@lieuwex Thx for this! You mention I could also use an object in stead of a LRU cache. I think this is what I'm looking for. How would this work? Now I've based my code on the examples from this page: http://simplyapps.nl/MagisterJS/ In the documentation @: http://simplyapps.nl/MagisterJS/docs/classes/Magister.html Could you help me out with some example code when I don't want to use LRU? I appreciate it! |
Same for me 😀 |
@bwired-nl and all others who are interested: I have published my Magister App for Homey on GitHub: |
@gruijter Every example on the homepage can be seen as different files. In a real app you wouldn't log in everytime you want to fetch anything, but just once.
I created a pull request at your repo which should fix your problem by caching the object. I didn't test this or anything so I'm not sure if I made a mistake somewhere. |
@lieuwex That's great work! I'll try to see how this works out (btw: you forgot to put var in front of getMagister). Being a complete noob I however don't get what is now exactly being cached. Is it all data belonging to a student? I want to poll Magister every 10 minutes to check if there is a new grade or a change in appointments. Would this caching have an effect on that? |
@gruijter Woops you're right. But nope, it's just the session that gets cached, not the data. This has no effect whatsoever apart from no errors and fewer network requests. |
Good news. I now have it implemented in the app and first impression is that it works great. Thx for your help in this. |
The function
Magister.ready(callback)
crashes for unknown reasons. This happens occasionally. Below function is called every 10 minutes, with the exact same credentials, and crashes after an irregular number of calls (usually within 10 calls). see response at the bottom.The text was updated successfully, but these errors were encountered: