c# - Azure WorkerRole trigger on queue like WebJob -
i'm used use webjob on azure triggering azure queue. works charm.
static void main(string[] args) { jobhost host = new jobhost(); host.runandblock(); } public static void processqueuemessage([queuetrigger("logqueue")] string logmessage, textwriter logger) { logger.writeline(logmessage); }
what's queuetrigger until process triggered message isnot done, message keep invisible (not delete). if turn off webjob (for webjob update example) message visible (after little timeout) in queue process updated webjob (perfect).
now wanna same thing on worker role. today this.
while (true) { var cloudmessage = await sourceimportationqueue.getmessageasync(); if (cloudmessage != null) sourceimportationqueue.deletemessage(cloudmessage); // process job (few hours) else await task.delay(1000 * 5); }
but if stop worker during job, lost message. how can webjob triggering ?
finally find simple solution. before running job of several hours, launch task keephiddenmessageasync
update message timeout. before end of timeout new update of message done. if problem occur timeout of message reached , message become visible.
private bool jobiscomplete; private void run() { while (true) { jobiscomplete = false; //get message var cloudmessage = await queue.getmessageasync(); if (cloudmessage != null) //run task keep message until end of job , worker role stopping update example var keephiddenmessagetask = keephiddenmessageasync(cloudmessage); // // process job (few hours) // jobiscomplete = true; await keephiddenmessagetask; await _queue.deletemessageasync(cloudmessage); else await task.delay(1000 * 5); } } private async task keephiddenmessageasync(cloudqueuemessage icloudqueuemessage) { while (true) { //update message , hidding during 5 new minutes await _queue.updatemessageasync(icloudqueuemessage, timespan.fromminutes(5), messageupdatefields.visibility); //wait 4 minutes (int = 0; < 60 * 4; i++) { if (jobiscomplete) return; else await task.delay(1000); } } }
Comments
Post a Comment