News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

add a new function to scheduled tasks

Started by busterone, December 17, 2011, 06:34:07 PM

Previous topic - Next topic

busterone

SMF 2.0.1
This would be simple to do a cron for but for this particular forum, cron is not available. 

What I want to do is add a function in /Sources/ScheduledTasks.php which will simply access the db and update the smf_messages table
with this sql command - UPDATE `smf_messages` SET `body` = '.'     every 6 hours

It may seem like an odd request, but this is a spambot trap forum that is cloaked to humans, so only bots register and post.  I usually do this once a day in phpmyadmin, but would rather automate it and forget it.  I do not want the spammers all over my live sites, so I have a separate cheap package just for the bot traps, hence, no cron. 

I thought about just editing an already existing function that is not used in this particular environment, but they are all rather detailed, so I wondered if just adding a new function would be the better way to go.  I am not much a coder myself. 


Ricky.

Or you may go other way, write a phpscript, include this sql command and run it through OS cron every 6 hours.

Illori

Quote from: busterone on December 17, 2011, 06:34:07 PM
SMF 2.0.1
This would be simple to do a cron for but for this particular forum, cron is not available. 

Ricky.

hmm.. I overlooked.. :) .. 

So new answer would go like ..

In that case, make it publicly accessible and call cron from other server, I have done that in many case.

and if that is not acceptable, you may see this topic : http://www.simplemachines.org/community/index.php?topic=329894.0

busterone

Yep, I am concerned about the file being publicly accessible, even though it is only a bottrap. I have already written a  standalone php file that does what I want it to do, unfortunately, it is publicly accessible and the mysql connection credentials are in it.  I could write the cron on my other server to do it auto, but I don't think I will leave the file public for security sake. I will just alter it to use SSI and require admin login to be able to run it, but I still have to run it manually.

I looked through arantor's topic you linked to, but truthfully, most of that is a bit over my abilities. I found the topic informative, but I need to study a bit more before I attempt to tackle adding it in as a scheduled task. Perhaps I can simply alter one of the existing tasks to run the file I have already created.

For now, I will leave it alone and come back to it after I have studied arantor's topic some more. Thanks for the link, I somehow missed it in my search.   :)

Kays

Adding a scheduled task is relatively simple. The main requirements are the row in the database which defines the task. Arantor provides a query which you should be able to use in phpMyAdmin. And a function with the task name in ManageScheduledTasks.php.

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

busterone

Yep, I see that, I think.  :D
I am still trying to wrap my head around it all.
What I am leaning towards is to simply add this in to the scheduled_daily_maintenance function
I can live with it being just once a day.  I have this so far, but am not even sure if it is correct. I haven't added it in yet to test it.
$smcFunc['db_query']('', '
UPDATE {db_prefix}messages
SET body = {some_variable_here},
)
      );


Kays

If that's what you want to run, then you need to wrap that with a function and add that to ManageScheduledTasks.php. Something like:


function my_update()
{
  global $smFunc;

  $smcFunc['db_query']('', '
UPDATE {db_prefix}messages
SET body = {some_variable_here},
)
      );
}


With "my_update" being the name of the function. That gets added to the "task" column for that task in the scheduled_tasks table.

I hope that's not the query you intend to run. As it is, it'll replace the body of all messages.


If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

busterone

#8
LOL, yes it is the task I want. It is for a spambot trap, and all I am collecting for reporting is the ip,email address, and username.  I do not allow any bbc so the bots post what would be a live link in a normal forum, but isn't live on my trap. I replace the body of them all in phpmyadmin with the query from the first post at least once a day. The bots never return to even look at their posts, they simply know that their bot-ware says that it was a success when they post.  I do not allow any search engines to index the site either, but prefer to not have all that pseudo-spam sitting there in the topics. This way, they think that there is a lot of activity and successful spamming, not realizing what they are actually doing.   ;)

I will give your code a shot a bit later tonight. Thanks.  :)
edit---
There is an issue here that I can't see, probably right in front of me though. I added this at the end of ManageScheduledTasks.php
and am getting this error when I attempt to access scheduled tasks in admin

Parse error: syntax error, unexpected T_STRING in /xxx/xxx/xxx/htdocs/xxxxxxx/Sources/ManageScheduledTasks.php on line 564
558 function my_update()
559 {
560  global $smFunc;
561
562 $smcFunc['db_query']('', '
563 UPDATE {db_prefix}messages
564 SET body = {'x'},
565          )
566     );
567 }
568 ?>


Matthew K.

It should be...
SET body = {string:body_str}',
array(
'body_str' => 'x'
)

busterone

ahh, that took care of the parse error. Thanks.  :)

I still need to insert the db row, but will post back when finished.

busterone

DB info added and it appears to be set up correctly. I changed the frequency so it will run before I retire for the night so I can verify that it works.
Thanks everyone. I think I have the concept down finally.  :)

Kays

I just hope that you're doing this on a test forum. Or have made a database backup first. Since that will replace the text in all messages.

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

busterone

See my earlier post. I want it to do that deliberately.  There are no humans posting, nor any bots' post content worth keeping. :)
I do have it on a test forum first though. Once I verify that it is working as I want it, I will implement it on the bot trap forum.

Kays

Ok, just want to make sure. :)

You can run a scheduled task manually if you just want to check if it works.

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

busterone

There must be something in the $smcFunc query that is not quite right.
I just ran it manually twice, and both times the screen returned with All selected tasks were completed , but when I looked at the posts, nothing was done to them. There are no errors in the error log either.
Here is the final function from ManageScheduledTasks.php
function my_update()
{
  global $smFunc;

  $smcFunc['db_query']('', '
UPDATE {db_prefix}messages
SET body = {string:body_str}',
array(
'body_str' => 'x'
)
   );
}

Kays

Off hand, I can't see anything wrong with that query.

Something to try. Add the following to your function and run the task. If your function is properly called then you should see a message in the error log


log_error('My update was successfully run');

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

busterone

Odd, nothing in the error log, also nothing in the task log for that task either. I ran the optimize database task, and it reflects that it ran successfully in the task log.

Kays

So it didn't run then. Is the task name in the database the same as the function name?

Did you add the language strings. I don't think they matter, but...

I'm off to bed so good luck with it. :)


If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

busterone

I figured it out. Simple- the function name was the issue. I changed it from function my_update()  to function scheduled_my_update()

worked as it should then.

Thanks for the help. Have a good night sleep. :)
Marking this one solved.

Advertisement: