Laravel - Trigger queue on form and update db after set time - php

I've been looking through Laravel's queue and schedulers and I'm not sure if it's what I need to do what I want. I'll try explain simply.
First, I hit a submit (basic form) that creates a db row with say a number and a created_at and finished_at. JS then creates a timer on the page that counts down (math) from the created at to the finished time.
I can do all that fine, what I'm struggling to get my head around is how do I make this then change that number value of 0 to 1 say after 10 minutes, or whatever time I wanted to specify? I'm not sure how to go about this. This sort of stuff is new to me.
Any help/pointing me in the right direction would be great! Explanations too. :)
Edit: To add, I looked at things like socket.io but I'm not sure if that's what I want too and if I can even use that with laravel as it's a framework off of node.js

Is the backend actually doing any work besides storage? If not I would skip the complexity of trying to open a web socket to tell the frontend the task is finished.
If all you're trying to accomplish is display something on the frontend to the user after an interval of time I would just use JS 100%.
However if the backend does need to do work and trigger the display change on the frontend you will need to open up a web socket.
Your constraint on Laravel's scheduling is that they are run at predefined times. So if these actions are triggered by the user and not a set time, skip using the scheduler. Instead use Events to broadcast something to node.

Related

How do I check my database with a cronjob in Symfony 4 so I can alter it if necessary?

I am currently working on a web app that runs on Symfony 4.
I have however encountered a problem while doing so.
I have a database that holds awards. They have some properties, but most importantly, they expire. And I want to do something once they did. I thought of doing this with a cronjob. So basically a cronjob checks every 10 minutes or so, whether or not the award has expired and if so, he is supposed to make an API call with the primary key of that award, which handles how I want to treat that award. Can I do that? Or is there something easier in Symfony that does just that, but without cron?
yes. you can do that.
There is no feature in Symfony to do that simpler than cron . you should use cron.
you should declare a public action to check for awards in one of your controllers and call that action from cron.
if you want you can also use this bundle:
https://github.com/Cron/Symfony-Bundle

Laravel process in background

I need to run a specific script/application once a certain button was pressed in my view. Now that this process may take up some time (30 seconds and more) I'd like to inform the user of the progress and whether it is successful or not.
What I have in mind is a simple text/label and a progress bar which are
'somehow' fed with the progression data provided by my Laravel application (the controller, to be more precise).
However, I have no clue where to start or what is best practise for a case as such.
I suggest you take a look at the queue in laravel: https://laravel.com/docs/5.5/queues
For the progress part. On different parts in a job you could dispatch an event. If you want to display them in your frontend, you should use something with push notifications. Laravel also has something for that:https://laravel.com/docs/5.5/broadcasting

Executing function to alter database every 15 minutes

I am currently working on a project that runs online tournaments. Normally admins of the site will generate brackets when it is time for the tournaments to start, but we have run into inconsistent start times, etc. and i am looking into proper ways to automate this process.
I have looked into running cronjobs every x min to check if a bracket needs to be generated but i am worried about issue when it comes to overlapping cronjobs, having to create/manage cronjobs through cpanel etc.
I was thinking about other solutions and thought it would be great if a user could load a page, the backend checks timestamps and determines if the bracket should be generated. An event is then fired/set to begin the auto-generation process elsewhere so it does not impact user load times. I just do not know the best route of going about this.
PS: I just need an idea of the direction i should be looking into so i can learn how to solve this issue i am not looking to copy and paste code. I just haven't been able to find anything. All of my search results provide cronjob examples.
EDIT
After thinking about things could using this work?
$(document).ready(function() {
$.ajax('Full Url Path Here');
})
I don't need to pass user input, or return any data i simply need a way to fire an event, it would be easy to include this only when needed via a helper class. Also i won't necessarily have to worry about users attempting to access i can restrict the route to ajax only requests and since nothing is needed/used on input or returned as output what can happen?
You could do it everytime a user loads a page (idea not tested, but theoretically possible):
1) Create a file and store the timestamp of the last time you updated the database.
2) Everytime a user loads a page, read that timestamp and check if 15 minutes passed.
3) If 15 minutes passed: Run a background script (with shell_exec?) that will do what you want and update the timestamp when it's done executing.
One obvious flaw with this system is that if you have no visitors in let's say a 30 minute frame, you will miss 2 updates. Though I guess that if you have no visitors you also have no point in generating brackets?

Running a function/script per user on the server side

I'm working on a basic lamp(willing to change) website , and I currently need a way to run some function on the server that runs for several hours per user, and every X hours it needs to query the mysql database to see if the value for that user has been updated, if it hasn't it need it to insert a new record in the database...I also should mention that the 'every X hours' can change per user too, and the total runtime of the function per user can also vary.
So basically I need a function that runs continuously on the server for few hours per user. What is the best way to do this? I want the site to be able to support many users (like 10000 +).
I'm willing to try new technologies for every aspect of the site, I'm still in the design phase and I was looking for some input.
I've looked at cron but not really sure how well it would work when dealing with so many users...
edit: Here is a typical scenario of events;
User presses button on the website and closes the browser.
Server starts a timer from when they pressed the button, now
the server will check if that user has pressed a different button within a given time frame (time frame can change per user), say within 30 minutes. If they didn't press the other button then the server needs to automatically insert a new record in the database.
The script will need to continue running, checking every 30 mins for say the next 5 hours.
Thank you!
Cron would work as well as you can code the page it will run. It's not a cron limitation.
The question is ambiguous btw. Maybe explaining your full scenario would help.
Meanwhile, my suggestion would be to set up a scrip that allows you to manually check what you need to check.
You definitely need the DB to be InnoDB optimized with proper indexes to be able to support 1000 plus users.
To alleviate the number of calls to the database, a common practice is to run scripts only on what you are interested (so in the case of users you would only select those who have logged on in say the past 3 hours)
That's achievable in 2 ways, a simple select statement, or by adding entries to a specific table on the login page, and remove them after the automated script has finished running.
All of this is pure theory without understanding exactly what you need to do though.
You are telling what/how you want to do, but not why you want to do it. Maybe letting us know why could lead to a different how ;)
However, what you can do is still use cron (or anything similar). The trick is to have
a last_interaction timestamp column
a maximum_interval column
a daily_runtime column
in your users database. Not optimized but you are in the design phase so you shouldn't pay too much attention to the performance aspect (except is explicitly required).

Browser MMORPG basics ideas

I started making a MMORPG to improve my web design skills. I managed to make users able to move around the screen and get some items with a combination of Javascript, PHP and MySQL.
I want to show all logged users at the same time moving around and push info to the user on what the others I doing but I have no idea how to do it. What are the general ideas/methods to do this?
Thanks
Try using this engine to accomplish what you want: MMO.js... it allows you to build real-time MMORPGs in JavaScript using websockets =)
You basically create a table called "online" with two columns "ID,Time" and whenever a player does some action it updates "Time" that's for the logging part.
And you get a cron job to check the database and if the Time in the database is greater than the current time minus (for example) -5 minutes (timing out time) DELETE the row.

Categories