Add Laravel Schedule in Controller Action - laravel

I have an web app that is supposed to scan any given site for metrics and send the report as an email. I fetch site url and email id from form and send it to controller action. Then in the action method, I want to run a system command in ubuntu to start the website scan and return the output to the method once done. I want to achieve the running of system command using laravel scheduler as using exec() command doesnt work as expected. If the command takes more time to execute then I get connection reset error. hence I wanted to schedule this task as cron job so that the execution can take its own time to complete and once completed, I can call another controller action to send the results to an email. Can someone help how to achieve this using laravel task scheduler.

Related

Best practice to solve Livewire UI Not updating until after API call

I am using Laravel Livewire, I currently have a form that captures a user's address information, then it needs to submit that to a 3rd party API (it's for billing information).
The issue that I am facing is that the UI doesnt update / livewire doesnt call render until after the entire method has run - which looks on the frontend like the UI is frozen / system isnt doing anything as the API is slow and takes a few seconds (15 - 20) to give a response.
I want to be able to trigger a browser event to display an alert saying the request is processing / show an alert banner on the page that it is processing before making the API request, process the API request, then display the results.
Currently I have identified the following ways to make this happen:
make the updates on the frontend, fire off an event with the data needed. Laravel event listener handles the API call, and emits a result event that the livewire component listens for, then the livewire component updates the page with the results.
make the updates on the frontend, dispatch a job to run in a queue worker, emit an event that the livewire component listens for, update the page with the results.
emit an event from the livewire component to itself, the livewire component then listens for that event and processes the API request, then updates the frontend with the results.
emit an event from the livewire component to itself to update the UI with processing info, while it still runs the API process in the original method call, and then updates the UI once the API gets a response
My thoughts are, what would be best practice here? All the above solutions require a different amount of work to get running, and solution 1 and 2 looks like complete overkill for such a simple process.
Does this come down to preference? Or is there actually a best practice way to do this in livewire?
And additionally, is there another way / simpler way to do this that I havnt mentioned above?
Your feedback is appreciated.
I would probably create a job and then return a response to the user. Then instigate Livewire polling that can check for the job being complete. If the API takes 15-20 seconds then polling once per second should be fine.
You might want to store the state in the database so that the job and the livewire component can communicate with each other. If you don't need to keep the data, using the cache to communicate between job and component would be a good alternative.

Laravel interaction with external program

I would need to interact with an external program as follow:
On submit form the controller launches the external program
The program will send a code by SMS and wait for it for verification
The controller returns a new view with a form to ask for this code
On submit form the controller provides the code to the program
The main issue is this program is running in a blocking mode (it will wait for the code and return only after the verification), therefore I cannot execute it directly from the controller with symfony/process.
Instead I execute it in a queue job (is it the right thing to do?) which is launched on the first form submit, but I can't find out how to send to this running job the code obtained by the second form submit.
Is there a way to send information from a controller to an already running job?
Thank you.
I would change your workflow a little bit.
On submit form, the controller dispatches a job, that launches the external program
The program will send a code by SMS and wait for it for verification
When job with step 2 finishes (verification is done), it will fire off Notification, that will contain link to a new view with the form.
On submit form the controller provides the code to the program
Simple point is to not block the request, let the job handle it, as you pointed out in original question.
Form can be built anyway, implement echo server that will help you deliver message via websocket.
Is there a way to send information from a controller to an already running job?
No.

Is there a way to pause an execution of a workflow?

Is there a way to pauses and resumes a workflow programmatically through the REST API? For instance, I would like to be able to submit a personal time off request from an app, the app creates and triggers a workflow. The workflow sends a notification to the manager for approval and the workflow execution is paused while waiting for the manager's approval. Once the manager approves the request, it calls the workflow API to resumes the execution of the workflow.
From the documentation, there's an option to put the workflow to sleep and I can use it to periodically check for the request's status but this incurs additional cost because there's no idea when the manager is going to approve the request.
https://cloud.google.com/workflows/docs/reference/syntax
Update 2021-10-05:
Waiting for an HTTP callback is now supported:
- create_callback:
call: events.create_callback_endpoint
args:
http_callback_method: POST # can be GET, PUT, ...
result: callback_details
# Send the callback URL somewhere for another service to call
- await_callback:
call: events.await_callback
args:
callback: ${callback_details}
timeout: 1800 # optional, in seconds
result: callback_request
Original response
The ability to wait for an HTTP webhook callback during a workflow execution is planned. (I'm the Workflows PM.)
Until then, you're right that the best solution is to poll for a flag (Firestore works well for this) using sleep.
Not sure about your use-case, but I guess you can only choose to execute the workflow once the manager has approved.
As you know there are 2 parts the first part creates the workflow.
As it is in https://cloud.google.com/workflows/docs/reference/rest/v1beta/projects.locations.workflows/create api for example.
Or you can look at https://cloud.google.com/workflows/docs/creating-updating-workflow.
That does not execute the workflow it just creates it. So it can be executed at a later stage.
To execute the workflow, there is this Api https://cloud.google.com/workflows/docs/reference/executions/rest/v1beta/projects.locations.workflows.executions/create
OR see https://cloud.google.com/workflows/docs/executing-workflow
Also when executing the workflow you can pass in parameters if needed.
Then If I understand the pricing correctly from https://cloud.google.com/workflows#section-9 it seems they only charge you per step executed.
Sorry, I have been through those docs a fair amount I did not see an "option to put the workflow to sleep", Could you link those docs by any chance?
We are talking about two separate workflows here. There is no reason to engineer with pause or not.
Initiate and go up until you fire the approval process.
After approval, trigger a new workflow that does the rest of the steps.

Laravel - Send email on specific event

I am currently developing an API, and each click is counted, with an api consumption.
When 80% of the api is consumed, I would like to send an email alert to the user.
I don't know this whole aspect of Laravel yet, and I'd like to know how you would do that? If possible by detailing a minimum
Thank you in advance,
I assume that this information is available in your database.
If so, you could
make an artisan command which get all user with > 80% and
notified = 0
Loop on those user and send the mail
Set notified to 1 on each user
Use CRON & Laravel scheduler to call your command
Don't forget to reset "notified" if necessary

Dynamics Custom Action Process Session

I am working with a test action that when activated and called via javascript will fire an email message. I used an action because I wanted to pass in some values to be used in the process. In a traditional workflow I could see the history of each time the workflow was fired.
Is there a way to see all of the times my custom action fired?
Traditional workflow are Asynchronous. What you are seeing as Process Sessions are actually Async Execution logs. Those are not available/applicable for Sync jobs like Realtime workflows or Actions. Maybe you can keep the failure logs in Action.
Unfortunately you have to assume from the Emails that sent out is Action execution time.

Resources