User configurable notification frequency in LAMP Web App - php

I am developing a Web Application for businesses to track the status of their repairs & part orders that is running on LAMP (Linux Apache MySQL PHP). I just need some input as to how I should go about allowing users to customize the frequency of email notifications.
Currently, I just have a cron job running every Monday at 6:00AM that runs a php script that sends an email to each user of their un-processed jobs. But I would like to give users the flexibility of not only choosing the time they are sent at, but the days of the week as well.
One idea I had was, some way or another, storing their email notification preferences in a MySQL database, and then writing a php script to notify via email but only if the current date/time fits within the criteria they have set & write in code to prevent it from being sent twice within the same cycle. Then I could just run the cron job every minute or 5 or whatever.
Or would it be better to somehow create individual cron jobs for each user programatically via php?
Any input would be greatly appreciated! :)

No you are right.
Individual crons will consume many resources. Imagine 10k of users with a request to send mail at different times ... this imply 10k of tasks.
The best solution is to create a cron task that will run on your users and take the correct actions.
Iterate on your users, check the date/time set up, detect change and send mail with adding a flag somewhere so said "it's done" (an attribute last_cron_scandate or next_calculated_cron_scandate could be a good solution)

Related

PHP dynamic schedule Email sending out

My application is running in Wordpress as a plugin. The application has many projects with a value of email frenquency, which will be saved on the database and the value will be: weekly, daily and every Tuesday and Thursday.
Now, I'm going to set up a email scheduler that the scheduled emails will be sent to the projects participants weekly, daily or twice a week depends on the frenquency.
My question is how can I set the email scheduler first. As Wordpress cron is based on the page loading, whereas my purpse is not relied on any page load, so I cannot use the Wordpress cron system. I've seen some articles suggest to use server cron itself. Can anyone give me a brief based on my requirements? Is it possible I can get a dynamic value from database using in the cron script? Plus, I didn't use Laravel for this one. I know Laravel has the task scheduling functionality.
The second quesiton is if I use corn with php script, can I set up the schedule to every Tuesday and Thursday?
Thirdly, I also see some libraries like SendGrid which has a schedule functionality, should I use it or keep on Cron?
Code PHP script(s) to handle sending emails according to data retrieved from DB.
Then create Cron to call certain PHP script(s) which runs on daily basis if customers can choose which day they prefer to get the emails.
Yes.
Cron is more secure, stable and independant.

Execute a php function automatically according to a given time without using Cron

is there any way to execute a php function according to a specific given time without using CRON?
because i'm creating a script which sends notifications to the clients about their sales at a given time gaps.
**Please note : time gaps will be selected by the clients so there should be an option for them to setup the time gap **
It could get really messy if you start programmatically setting cron tasks for each user, but I think cron would would be part of the solution.
I think it would be better to store each client's scheduling preferences in a database, then run a script periodically (with cron) to check whether it's time to send their notifications, and then send accordingly.
This would enable them to update their preferences without having to find and update (or remove) individual cron jobs.

what is a proper way of sending email notification when user is not viewing the site

I'm currently working on a email notification of a social media website, and I want to send users email notification whenever the user is not viewing the site(at least for a certain period,30mins,1h,etc)
I'm considering using a cron job for sending those email notification and fire the cron job every 30 minutes.
Let's say user A commented on user B at 2014/8/13 18:39:00, it would have a row in the comment table of the database base like
comment_user user_received comment_send_time view_or_not(y/n) email(y/n)
user_A user_B 2014/8/13 18:39:00 n n
in my corn job php script, I would check if the interval between the current time and the comment_send_time is greater than 30mins, and is view=n email=nand the cron job is going to send user_B an email notification of the new comment, and after the email being sent successfully, updated the email to n, so that it can prevent sending a redundant email notification.
My concern is if I run the cron job every 30 mins, will it harms the server performance, and will a cron job be a proper way to handle this task and what other options would be
Personally, I'd have the job set to run every minute and have the script running a very lightweight sql query to check for new alerts.
If your script is heavy and takes longer to run than 1 minute, then scale it back or performance tweak it.
To answer your question directly, yes, it will affect performance. However, if you have a lightweight script, that hit is negligible. If you're sending out thousands of emails every minute, it's time to scale that gear to it's own server. At that point you should be making money enough to support another server, or you need to rethink your strategy for alerting users. If you need to start scaling, then start looking at message queueing, and task/work queues like RabbitMQ or Laravel

how to trigger email sending automatically from the database

Hello I'm battling with some problem here. I have a message in my mysql database that I want to send based on the scheduled date is there any way to do this : that when is the exact date the server should send the maill by itself, OR is there anyway to make a page load itself everyday without my access to send the email(if no one visit the site). Thanks. I'm using php and mysql db. I have scheduleddate column in my database
You could use a cron job to call a script which checks the database at a set interval (perhaps once per hour) and then have that script pull out the relevant messages and sends them.
The cron for that would look something like:
0 * * * * /path/to/email/script.php
This script could then query the database and send those messages which need sending. Of course you could set this to run every single minute (* * * * *), but I'd be very wary of having any script run every single minute!
This isn't really ideal thought, and instead you should look at using some form of queue management. If you take a look at queues.io you can find lots of info on various popular queue projects projects.
I also found this post on setting up background jobs using resque very helpful!
Of course how you integrate such a system is very much dependent on your codebase and your environment.
Some frameworks have this kinda functionality integrated very well, I have worked with Laravel 4's queue component quite a lot and it really does make it very easy to work with..
In reality many people just end up using a cron/script combo to do the job, but it's important to know there are other (better) ways to achieve this.
The best way to do this without killing your CPU utilization is by scheduling it via a scheduled cron task on your server. Write your code to send the email, then add that page to a daily cron in your server's Admin Control Panel.

How to take an action after certain time (different from user to user)?

I'm developing a web game (js php mysql) in which one clicks a button to start an action that takes time to complete (let's say 10 hours) and when it finishes some points are added to that player's total.. The problem is that I need those points to be added even if the player is not online at the time the action finishes.. for example I need to have the rankings updated, or an email sent to the player..
I thought about a cron job checking constantly for ending actions, but I think that would kill the resources (contantly checking against actions of thousands of players..).
Is there a better solution to this problem?
Thanks for your attention!!
You can just write into your database when it's finished and when the user logs in you add the earned points to his account. You can also check with a cronjob. Even if you have millions of user this will not kill your server.
Cron is perfect for this. You could write your tasks in stored procedures, then have cron run an SQL script to call the stored procedure that would update the records of your players.
Databases are designed to work with thousands and millions of pieces of information efficiently, so I don't think the idea that it will kill system resources is a valid one unless you hosting system is really constrained already.
If you want to be safe against cheating you need to do the checking on the server anyway. If the "waiting" will happen within a Javascript on the client, one could easily decrease the remaing time.
So you need to send the job to the server (which is assumed to be safe against clock modifications) and the server will determine the end timestamp. You could store your jobs in a queue.
If you only need this information for the user himself you can just look at the queue when the user logs in. Otherwise run a cron job every minute (or so). This job will mark all jobs finished when their timestamp is in the past (and remove them from the database).
If you need more precise checking you will need to come up with an alternative server side solution that is doing this more often (e.g. a simple program polling the database every few seconds).

Categories