PDA

View Full Version : Set $vbphrase[] from Plugin ?


imported_silkroad
12-05-2009, 07:07 PM
Hi!

I am thinking of some plugin code that does the following:

Runs curl twice and pulls a URL from each site
Compares to see which one is faster (by some significant margin)
Changes the a value in $vbphrase[image_server] based on the results.The reason I want to change $vbphrase[image_server] is that I use it in a number of templates in a URL, for example:

http://$vbphrase[image_server]/images/bigimage.jpg

I want to change the image_server based on the performance of the network.

More importantly, if one server is down, I want to automatically switch $vbphrase[image_server] over to the other one.

Any suggestions?

kh99
12-05-2009, 07:17 PM
It seems to me that if you are doing it in a plugin that will be run every page access, then maybe the easiest thing would be to just create a global variable to hold the answer and change the templates to use it instead of the phrase (or in addition to a phrase). I think you can just add to $vbulletin by making up a unique key, but to be honest I'm not sure if that's the "accepted" practice.

imported_silkroad
12-05-2009, 08:04 PM
It seems to me that if you are doing it in a plugin that will be run every page access, then maybe the easiest thing would be to just create a global variable to hold the answer and change the templates to use it instead of the phrase (or in addition to a phrase). I think you can just add to $vbulletin by making up a unique key, but to be honest I'm not sure if that's the "accepted" practice.

That's what I thought... that someone would advise to use a Global Variable. I agree, but now I think I should not use a plugin (for performance reasons).

I think it is also better to simply run the script in a crontab file every 5 minutes, instead of with every page in a plugin.

In that case, a plug-in might not be the best way to go; better to simply write a script for cron and use a MySQL call to update $vbphrase[image_server] in the dB?


mysql_query("UPDATE phrase SET text='myfastserver.com' WHERE varname='image_server'")
or die(mysql_error());

kh99
12-05-2009, 08:44 PM
Ha - I hate being that predictable :). But if you have an aversion to globals, it's likely that the plugin code and the eval of the template happen in the same php file so it could be local to that file. Making it global I guess just avoids having to worry about cases where something might be happening in a function call.

The cron idea sounds good to me, to be honest I don't know anything about updating the phrase in the database.

imported_silkroad
12-05-2009, 09:26 PM
Ha - The cron idea sounds good to me, to be honest I don't know anything about updating the phrase in the database.

I think that is what I will do...

Run a cronfile that does the network check with curl
Update $vbphrase[image_server] using MySQL and the phrase table.Thanks for helping me brainstorm the approach.

One advantage of this approach is that I can easily change $vbphrase[] when working on a server (and can also easily disable cron when I need to). Also, when I add a third server to the mix, I can easily update the cron file and can avoid doing anything in vB.

--------------- Added 1260092915 at 1260092915 ---------------

Unfortunately, my idea did not work.

Followup post:

UPDATE phrase SET text='www.mybackup.com' WHERE varname='backup_server'; (https://vborg.vbsupport.ru/showthread.php?t=229616)