1919import glob
2020import re
2121import shutil
22+ import threading
23+ from Queue import Queue
2224
2325from flask import Flask , jsonify , request , abort
2426
3234arg_webhook_token = None
3335arg_api_token = None
3436
37+ workQueue = Queue ()
38+
3539def create_symlink (source , linkname ):
3640 try :
3741 os .symlink (source , linkname )
@@ -48,6 +52,16 @@ def req_headers():
4852 "Authorization" : "Bearer %s" % (arg_api_token ,),
4953 }
5054
55+ # Buildkite considers a poke to have failed if it has to wait more than 10s for
56+ # data (any data, not just the initial response) and it normally takes longer than
57+ # that to download an artifact from buildkite. Apparently there is no way in flask
58+ # to finish the response and then keep doing stuff, so instead this has to involve
59+ # threading. Sigh.
60+ def worker_thread ():
61+ while True :
62+ toDeploy = workQueue .get ()
63+ deploy_buildkite_artifact (* toDeploy )
64+
5165@app .route ("/" , methods = ["POST" ])
5266def on_receive_buildkite_poke ():
5367 got_webhook_token = request .headers .get ('X-Buildkite-Token' )
@@ -129,7 +143,16 @@ def on_receive_buildkite_poke():
129143 abort (400 , "Refusing to deploy artifact from URL %s" , artifact_to_deploy ['url' ])
130144 return
131145
132- return deploy_buildkite_artifact (artifact_to_deploy , pipeline_name , build_num )
146+ # there's no point building up a queue of things to deploy, so if there are any pending jobs,
147+ # remove them
148+ while not workQueue .empty ():
149+ try :
150+ workQueue .get (False )
151+ except :
152+ pass
153+ workQueue .put ([artifact_to_deploy , pipeline_name , build_num ])
154+
155+ return jsonify ({})
133156
134157def deploy_buildkite_artifact (artifact , pipeline_name , build_num ):
135158 artifact_response = requests .get (artifact ['url' ], headers = req_headers ())
@@ -152,8 +175,6 @@ def deploy_buildkite_artifact(artifact, pipeline_name, build_num):
152175
153176 create_symlink (source = extracted_dir , linkname = arg_symlink )
154177
155- return jsonify ({})
156-
157178def deploy_tarball (artifact , build_dir ):
158179 """Download a tarball from jenkins and unpack it
159180
@@ -281,4 +302,7 @@ def deploy_tarball(artifact, build_dir):
281302 deployer .symlink_paths ,
282303 )
283304 )
305+ fred = threading .Thread (target = worker_thread )
306+ fred .daemon = True
307+ fred .start ()
284308 app .run (port = args .port , debug = False )
0 commit comments