Scheduled posting (php, mongo, cron, regexp)

Good afternoon.

Recently, I had to write a page for sending posts to the walls of social networks. The task was to post on the wall at a certain time and not immediately.

I wrote a page with fields for choosing social networks, a message text and a field for entering the time of sending. In addition to the usual calendar with the choice of the date and time of sending, I added another field with the input of the sending criteria according to the cron rule.

Template * * * * *

After that, the question arose - how to get only the necessary fields for sending?

Cron is triggered every minute and calls the script for sending posts, and iterating through the entire table of posts prepared for sending every minute and comparing with the current date / time would be an unnecessary operation.

I use the MongoDB database, which can select all fields by regex. Here's what I did:

Take the current date and time and translate into an array

$dateTime = explode(' ', date('i G j n w', time()));

Next, build a regular expression that selects the records that are suitable for sending at the current moment (current minute). sendRule - the name of the field in the database that contains our cron rule. $ dateTime [0] (minutes) can be with zeros at the beginning (for example 05 minutes), so we multiply them by 1 to remove zero. Tested * 1.12.20 7.8.12 * * / 3 (every minute 1.12 and 20 hours on the 7.8.12 days of every month on Wednesday). That's all, then we loop through the resulting array and send everything we need.

$joe_search = new \MongoRegex(

$cursor = $collection->find(array("sendRule" => $joe_search));

Also popular now: