public HttpResponseMessage Post(SimpleNotifyJobInput input) { if (input == null || string.IsNullOrEmpty(input.TargetUri) || string.IsNullOrEmpty(input.NotificationMessage)) { return(CreateHttpResponse(HttpStatusCode.BadRequest, "{\"Error\":\"Invalid OutgoingMessagingNotifyInput\"}")); } if (!input.TargetUri.StartsWith("sip", StringComparison.InvariantCultureIgnoreCase)) { return(CreateHttpResponse(HttpStatusCode.BadRequest, "{\"Error\":\"Invalid To\"}")); } string jobId = Guid.NewGuid().ToString("N"); try { //New job and run asyncronizedly var jobConfig = new PlatformServiceSampleJobConfiguration { JobType = JobType.SimpleNotification, SimpleNotifyJobInput = input }; PlatformServiceJobBase job = PlatformServiceClientJobHelper.GetJob(jobId, WebApiApplication.InstanceId, WebApiApplication.AzureApplication, jobConfig); if (job == null) { return(CreateHttpResponse(HttpStatusCode.BadRequest, "{\"Error\":\"Invalid job input or job type\"}")); } job.ExecuteAndRecordAsync(Storage).Observe <Exception>(); return(Request.CreateResponse(HttpStatusCode.Created, job)); } catch (Exception e) { Logger.Instance.Error(e, "Exception happened in schedule job"); return(CreateHttpResponse(HttpStatusCode.InternalServerError, "{\"Error\":\"Unable to start a job run\"}")); } }
/// <summary> /// Implement the interface ExecuteCoreAsync /// </summary> /// <returns></returns> public override async Task ExecuteCoreAsync() { ICommunication communication = AzureApplication.ApplicationEndpoint.Application.Communication; //Construct callbackUrl with callbackContext for callback message routing CallbackContext callbackcontext = new CallbackContext { InstanceId = this.InstanceId, JobId = this.JobId }; string callbackContextJsonString = JsonConvert.SerializeObject(callbackcontext); string CallbackUrl = string.Format(CultureInfo.InvariantCulture, AzureApplication.CallbackUriFormat, HttpUtility.UrlEncode(callbackContextJsonString)); Logger.Instance.Information("[SimpleNotifyJob] start to send messaging invitation"); SimpleNotifyJobInput jobinput = this.JobInput as SimpleNotifyJobInput; if (jobinput == null) { throw new InvalidOperationException("Failed to get SimpleNotifyJobInput instance"); } IMessagingInvitation invite = null; try { //Invite resource will be there when this taskc completes invite = await communication.StartMessagingAsync("Notification", jobinput.TargetUri, CallbackUrl, LoggingContext).ConfigureAwait(false); //Waiting for invite complete await invite.WaitForInviteCompleteAsync().ConfigureAwait(false); IMessagingCall messagingCall = invite.RelatedConversation.MessagingCall; messagingCall.IncomingMessageReceived += OnIncomingMessageReceived; Logger.Instance.Information("[SimpleNotifyJob] sending notification message"); await messagingCall.SendMessageAsync(jobinput.NotificationMessage, LoggingContext).ConfigureAwait(false); Task reseponseReceived = m_responseMessageNotified.Task; try { await reseponseReceived.TimeoutAfterAsync(TimeSpan.FromSeconds(30)).ConfigureAwait(false); } catch (TimeoutException) { Logger.Instance.Warning("[SimpleNotifyJob] Do not get a response with in 30 seconds"); } if (reseponseReceived.IsCompleted) { Logger.Instance.Information("[SimpleNotifyJob] sending thank you message"); await messagingCall.SendMessageAsync("Thank you!", LoggingContext).ConfigureAwait(false); } } catch(CapabilityNotAvailableException ex) { Logger.Instance.Error("[SimpleNotifyJob] Failed!", ex); } catch(RemotePlatformServiceException ex) { Logger.Instance.Error("[SimpleNotifyJob] Failed!", ex); } finally { if (invite?.RelatedConversation != null) { await invite.RelatedConversation.DeleteAsync(LoggingContext).ConfigureAwait(false); } } }
public SimpleNotifyJob(string jobId, string instanceId, AzureBasedApplicationBase azureApplication, SimpleNotifyJobInput input) : base(jobId, instanceId, azureApplication, input, JobType.SimpleNotification) { m_responseMessageNotified = new TaskCompletionSource<string>(); }