Advertisement:

Execute a task only once at a certain time

Aloittaja Andy00, tammikuu 28, 2014, 12:01:22 IP

« edellinen - seuraava »

Andy00

Hi, for one board of my forum, if a user does a specific action (no matter what) I want to execute a task after exactly three days. I read this http://www.simplemachines.org/community/index.php?topic=329894.0 but, if i understood it well, these are repeating tasks; instead I want my task to execute only one time.
Any idea about how to do that? Thanks in advance.

margarett

You can use that in combination with that topic you just pointed out.

1 - You create the php file for the action that should be performed. Don't add it to the database! We'll get back to this in a moment.
2 - You identify what is your user action and how it is triggered. When it does, you perform the code that should add it to the database. Before you do this, you need to be sure to calculate the time in 3 days.
2.1 - Add the code to the database to call your task, with your calculated time.
3 - Now, go back to the code created in 1. At the end of that code, you add the database operation to REMOVE that scheduled task. Of course, you will probably need to repeat this task very often, so it might be a good idea to store the task ID or something along those lines. It gets a bit complicated but it should be doable

Good luck
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

LainaaOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Arantor

Yeah, one-off tasks are largely impossible in 2.0.... what I've done in the past is settled for them being in a list and checking once an hour (or once a day, depending on what it's going to do) or whatever as to whether there's anything in the queue.

2.1 introduces background tasks that works in much the fashion you want, though future-dated tasks are not obvious as to their usage (they are doable but it's not *expected* as it were)
Holder of controversial views, all of which my own.


Andy00

I had a look at MySql' event scheduler, it could be a solution but unfortunately my hosting company doesn't prodive it :(
I'll try margarett's solution, otherwise I'll end up using standard scheduling system, checking state once an hour (or every 30 minutes). How would performance be affected if set a 30 minute task? I make a 4 joins query and send some emails.

Arantor

Are you doing that 4 join query every time? Seriously, context is really, really important as I think I've tried to explain before... How about you explain what you're trying to do and I'll tell you if there isn't a better way of doing it?
Holder of controversial views, all of which my own.


Andy00

Thanks Cumber :P
I'm working on a mod for SMF, in particular I've added a new action which is available by some users. Users can activate this action by clicking on a link inside a topic (the first message of the topic).
When a user clicks that link (execute my action), that's the starting point from which I need to count three days.

When the three days wait is finished, I have to do what I 've said in my last post: query with 4 joins and send some (5-15) emails.

Andy00

Another point: I think the query is not so much hard to executes because there are lots of AND and EQUAL conditions

Arantor

Ah, now we get to the real matter: you don't need to do the query with 4 joins and emails every half an hour, you just need to figure out every half an hour if the work needs to be done - which is a much simpler query if you build the queue correctly.

But I could give you so much more useful information if you didn't choose to withhold most of the details. Best of luck to you.
Holder of controversial views, all of which my own.


Andy00

Lainaus käyttäjältä: Sir Cumber-Patcher - tammikuu 29, 2014, 01:44:17 IP
Ah, now we get to the real matter: you don't need to do the query with 4 joins and emails every half an hour, you just need to figure out every half an hour if the work needs to be done - which is a much simpler query if you build the queue correctly.

Maybe I was not clear (my english is not very good:)), I need the 4 joins query exaclty to know if the job has to be done (so I need it every half an hour).
The "real" job consists of other 2 small queries and sending some emails.

The only solution I see is to use smf standard scheduled tasks and check half an hour. If you have other questions or want to propose something else, I'm here :)

PS: thanks to all for you patience.  ::)

Arantor

That's not how I'd do it for performance reasons.

Create a table with two columns, an auto increment id and a timestamp, insert a new row into this table when you start the timer, the timestamp indicates when something has to be done. Check *that* table in the scheduled task. If that timer has counted down, then run your more expensive query (4 table join is not necessarily cheap even if it is ANDs and stuff, indexes depending)... and then remove that entry from the queue table when you're done.

The point is that if you're doing a check every half an hour, you want to do as little work as possible in that time.
Holder of controversial views, all of which my own.


Andy00

I didn't think about that, you are the man :) Thank you very much.

PS: I must tell you Cumber...I've never seen anyone in any forum with 57980 posts! Don't stop posting ;)

Advertisement: