Send API response in AWS after that, run process in background (async) - python-3.x

I do have an endpoint DELETE book/{bookname}. When user request to endpoint, he/she have to get response "This book will be deleted after 10 minute". And after sending response, another function will start and delete book from database after 10 minutes. I have to use async only and Python 3.7. I have succeeded, all of them works fine ,except sending response to user.
Now, User sends request to DELETE /book/{bookname} and waits... (background processes will delete after 10 minutes) . after some seconds due to timeout, user receives "Request Timeout". I just want to send success message to user (it does not matter book will be deleted or not) and run processes background in AWS Lambda. (I must not use invoke and stepfunctions)

Related

Force client to wait for response from azure function

I wrote an azure function with python that do some data processing, when I test on large dataset (150 lines), chrome raise a 502 http error : (tested the azure function on 10 lines and everything was ok)
I think the problem is that chrome browser wait for so long and when no response coming from azure function it automatically raises 502 error. I checked that the logic function is executed till the end but I don't get my json response when code is completed. Here is my http response I should get
return func.HttpResponse(json.dumps({"file" : file.name.split('/')[2]}),
mimetype="application/json",)
expected output :
{"file": "filename.json"}
In production I have to process more then 1500 lines, and within 150 lines the azure function take about 2 minutes to complete.
How to force chrome client or any client who hit the url of my azure function to wait to complete? is there any workaround pls?
For this problem, we are not client so we can't determine timeout value of client.
For your problem of force chrome client to wait the function complete, I'm afraid we can't do this setting. You can refer to this post (also shown as below screenshot).
According to the screenshot above, we can see chrome can't change the timeout setting and we can change it in other browsers.
If the client do not use browser but use code(such as .net) to request the function, the code should be like:
HttpClient httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromMinutes(10);

Dialogflow - Add followupEvent after a given delay

I am trying to add a series of responses inside an intent handler and set a timer of 20 minutes which will trigger(at its end) a followup event.
So here is what I've tried:
agent.add(response_1);
//...
agent.add(response_n);
setTimeout(() => {
console.log("Setting follow up event")
agent.setFollowupEvent('20_MINUTES_PASSED');
}, 1200000);
Even though the log was displayed, my function execution stopped before it. I have checked the logs and I saw the message "Function execution took 26 ms, finished with status code: 200" displayed before "Setting follow up event".
I know that each function has a 3-5 sec timeout and I understand this is why the function finished its execution, but I cannot figure out how to trigger that event after those 20 minutes...
There are two issues with this idea: Cloud functions aren't meant to run for that long, you would have to use either a real server or some scheduling service for this. However, Dialogflow doesn't let you do this anyway, webhook requests time out after a few seconds. If you haven't send a response by then the agent will tell the user that your service is unavailable. You can also not initiate a new session without the users explicit request to do so, presumably because developers would quickly abuse this for spam etc. There is thus no way to trigger an event after 20 minutes.
The closest to what you are looking for are probably push notifications, but they are very limited compared to follow up events.

How to ensure message will be there for 10 minutes in queue

I have two applications App1 Console app and App2 Azure func App
App1 is for enroll new customer
App2 is for activate account for new customer
We need to ensure that each message that App1 sends is stored in a queue for 10 minutes before App2 uses the message
From App1 I'm taking inputs like customer-id , customer-name and send this message to queue.
using simple code -
// Create a new message to send to the queue
string messageBody = $"Message {i}";
var message = new Message(Encoding.UTF8.GetBytes(messageBody));
// Write the body of the message to the console
Console.WriteLine($"Sending message: {messageBody}");
// Send the message to the queue
await queueClient.SendAsync(message);
do i need to add any setting for 10 minutes scenario?
From App2 I need to read this message but need to ensure for 10 minutes before App2 uses the message.
How to achieve this to ensure message there for 10 minutes in queue.
When i search on stackoverflow i found this
If a message is added with a 5 minute timeout it will only be processed after that.
it means while sending message i need to set something ?
It looks like you're looking for Scheduled messages.
You can submit messages to a queue or topic for delayed processing; for example, to schedule a job to become available for processing by a system at a certain time. This capability realizes a reliable distributed time-based scheduler.
Scheduled messages do not materialize in the queue until the defined enqueue time. Before that time, scheduled messages can be canceled. Cancellation deletes the message.
You can schedule messages either by setting the ScheduledEnqueueTimeUtc property when sending a message through the regular send path, or explicitly with the ScheduleMessageAsync API. The latter immediately returns the scheduled message's SequenceNumber, which you can later use to cancel the scheduled message if needed. Scheduled messages and their sequence numbers can also be discovered using message browsing.

Mutithreading and concurrent processing

I have a small question on mutlithreading.
-I want to start to a thread after some successful pre validations.
-If the pre validation fails, there will not be any thread created and the
control is sent back to the user with some error message.
- **If the validation is successful, then i need to send user a success response like a pop with msg "request accepted" and in parallel the thread should be executed.
- Once the thread is executed, each thread returns some response and
the response is sent as an email.
- **The tricky part here is, how can i return a success response back to the user and execute the thread in parallel.
-**FYI, i am using executor framework for submitting the task and retrieving the response in future object and email the data in future object.
Thanks In Advance

SessionCloner.getSessionCloner().getSession() notesagent You are not authorized to perform that operation

Summary:
Domino server 8.5.3 Windows server 2008 FP2.
When calling
NotesAgent.runOnServer(noteid)
from a web browser in a Thread where agent is set to "run as web user", I am getting error "HTTP JVM: You are not authorized to perform that operation".
Detail:
All web requests come through 2 channels into our application, via a notes agent or via an xpage (that acts like an XAgent). We have a back end process that can take up to 20 seconds to complete, it is a call to a remote web service and we have no control over it. Due to requirements we can not queue these documents for a scheduled agent, they need to go immediately...or as immediately as the service will allow! The 2 main problems are: 1..the user has to wait up to 20 seconds, 2..the http thread is not freed up. During a busy time, we have seen the http thread pool saturate. What I have done in my test environment is send the request into the XAgent, this calls our backing bean which starts a separate thread, returns message to user. It's working great, http thread frees up immediately and a timely response for user and submission to web service proceeds "asynchronously".
The logic calling the web service is in LotusScript, converting to java would be a massive job as there are an enormous amount of interconnected processes in LotusScript. In the java thread the username is the server name, effectiveUserName is the authenticated http user, the thread calls a
NotesAgent.runOnServer(noteid)
, which works, except the agent runs with credentials of the user that signed the agent. If we set the agent to "Run as web user", I get the error above. As a test, I moved the code that triggers the NotesAgent.run() into the main "calling" function, which gets it's session via:
JSFUtil.getVariableValue("session")
and this works as expected (user=server, http user=effective user). The thread session is got like this:
this.module = NotesContext.getCurrent().getModule();
this.sessionCloner = SessionCloner.getSessionCloner();
NotesContext context = new NotesContext( this.module );
NotesContext.initThread( context );
session = this.sessionCloner.getSession();
...and as above, the effective User Name is the authenticated http user, the user name is the server name.
If I browse directly to the agent, e.g. .../myapp.nsf/myagent?openagent, the agent will run as the effective http user. I then put my test http user into the highest security group I have on my test server, same error. I then logged in as a server admin user (that has security settings for everything) and got same error.
On my test serrver I have: Domino\jvm\lib\security\java.policy when running the Job from the NSF:
grant {
permission java.security.AllPermission;
};
Since I can trigger the agent via JSFUtil.getVariableValue("session") is there some security difference when getting a session via SessionCloner.getSessionCloner().getSession() ?
Thanks in advance.
Agents and XPages shall not interbreed :-). For the thread I would remove the need to get the web user. Pass a java object to the thread that does not contain any Notes objects. Then go old school and use sInitThread stermThread to get a shiny new session and run the agent from there.

Resources