views:

150

answers:

2

I wrote a plug-in for the jetbrains tool teamcity. It is pretty much just a server listener that listens for a build being triggered and outputs some text files with information about different builds like what triggered it, how many changes there where ect ect. After I finished that I wrote a python script that could input info into teamcity while the server is running and kick of a build. I would like to be able to get the artifacts for that build after the build is ran, but the problem is I don't know how long it takes each build to run. Sometimes it is 30 sec other times 30 minutes.

So I am kicking off the build with this line in python.

    urllib.urlopen('http://'+username+':'+password+'@localhost/httpAuth/action.html?add2Queue='+btid+'&system.name=<btid>&system.value=<'+btid+'>&system.name=<buildNumber>&system.value=<'+buildNumber+'>')

After the build runs (some indetermined amount of time) I would like to use this line to get my text file.

urllib.urlopen('http://'+username+':'+password+'@localhost/httpAuth/action.html?add2Queue='+btid+'&system.name=<btid>&system.value=<'+btid+'>&system.name=<buildNumber>&system.value=<'+buildNumber+'>')

Again the problem is I don't know how long to wait before executing the second line. Usually in Java I would do a second thread of sorts that sleeps for a certain amount of time and waits for the build to be done. I am not sure how to do this in python. So if anyone has an idea of either how to do this in python OR a better way to do this I would appreciate it. If I need to explain myself better please let me know.

Grant-

A: 

Python actually has a threading system that is fairly similar to Java, so you should be able to use that without much trouble.

But if all you need to do is wait for some predetermined amount of time, use

import time
time.sleep(300) # sleep for 300 seconds
David Zaslavsky
This is somewhat what I was thinking, but I just don't know how much time that is. My original thought was to do this same time.sleep() command for so many seconds and try to locate the file. And if it returns the http 404 error then sleep for another so many seconds and try again. This just seems like and ugly approach, and I was hoping for a more elegant solution.
cozmokramer8
Oh and thanks for the link too, I am reading through that now. :)
cozmokramer8
@cozmokramer8: It's called "polling" or "busy waiting" and it's your only choice in this case.
S.Lott
+2  A: 

Unless you get get notified by having the build server contact you, the only way to do it is to poll. You can either spawn a thread as indicated in other comments, you just have your main script sleep and poll.

Something like:

wait=True
while wait:
   url=urllib.urlopen('http://'+username+':'+password+'@localhost/httpAuth/action.html?add2Queue='+btid+'&system.name=<btid>&system.value=<'+btid+'>&system.name=<buildNumber>&system.value=<'+buildNumber+'>')
   if url.getcode()!=404:
     wait=False
   else:
     time.sleep(60)

As an alternative, you could use CherryPy. Then, when the build is done, you could have curl or wget connect to the listening CherryPy server and trigger your app to download the url.

You could also use xmlrpclib to do something similar.

Christopher
I guess I am going to have to poll then. I liked your suggestions for alternatives, but I am somewhat limited because I have to use only what is currently installed on the server. This is for a fairly big company and I don't have the option of even installing extra python libs :( Thanks for the code though. Just curious is this considered bad coding practice to do it this way? It seems ugly to me but apparently it must happen quite often. Thanks again Grant
cozmokramer8
The question of interrupt-driven or poll-based notification is a large one. Sometimes polling is more efficient, sometimes event notifications are more efficient. I generally prefer *not* to poll, but sometimes larger architectural decisions negate the possibility of using notifications. In which case, the only option is to poll. One thing about polling, it is usually best to make the poll interval configurable b/c there is no such thing as the One True Poll Interval.
Christopher
Well thanks again Christopher you have been a big help. Thanks too for the tip about making it configurable, that is a good idea. Also if anyone else is reading this in the future for tips I also made it to where the loop will break after 25 minutes of trying. I did this because there is the chance that someone would start a build and then interrupt it, so not text file would ever output. Then if I ran this script it would be in a continuous loop. Grant-
cozmokramer8