MyBB Community Forums

Full Version: Task isn't Auto Executing
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi,
I have made a Task file which should execute every month,

here is its Code:
<?php
//Task File poststats_month.php

function task_poststats_month($task)
{
	global $mybb, $db;

			function poststats_publish_post($tid, $message, $uid) {
				global $db;
					require_once MYBB_ROOT . 'inc/datahandlers/post.php';
						$posthandler = new PostDataHandler('insert');
						$posthandler->action = 'do_newreply';
					$new_post = array(
										"tid" => $tid,
										"uid" => $uid,
										"message" => $message,
										"ipaddress" => '127.0.0.1',
										"signature" => "0",
										"subscriptionmethod" => "0",
										"disablesmilies" => "0"

									);
			    $posthandler->set_data($new_post);
			    $valid_post = $posthandler->validate_post();
			    if($valid_post) {
					$posthandler->insert_post();
					$db->query("UPDATE ".TABLE_PREFIX."threads SET closed = 1 WHERE tid = '$tid'");
			    }
			}

			$usergroup = $mybb->settings['poststats_group'];

			$message = "[b] Following users still haven't completed their posts:[/b]

			";

	$query = $db->simple_select("users", "*", "usergroup IN (".$usergroup.") OR additionalgroups IN (".$usergroup.")", array("order_by" => 'poststats_month', "order_dir" => 'DESC'));
	while($user = $db->fetch_array($query)) {
		if($user['poststats_month'] < $mybb->settings['poststats_limit']) {
			$message .="@'".$user['username']."'  -  [b][color=red]".$user['poststats_month']."[/color][/b]
			";
		}
	}
	
	$message .= "
	Terminate Their Services!";


		poststats_publish_post($mybb->settings['poststats_tid'], $message, $mybb->settings['poststats_poster_uid']);
		
	$day_record = array(
	"poststats_month" => $db->escape_string('0')
	);

	$query = $db->update_query('users', $day_record);
	add_task_log($task, "Post Statistics for this month have been reset.");
}

?>

Screenshot of its Settings:
[Image: VhrbkQy.png]


What it does:
Posts $user['poststats_month'] of each user in a Reply of a Thread having TID - $mybb->settings['poststats_tid'] and poster as a User whose user id - $mybb->settings['poststats_poster_uid']
Then changes the Status of Thread to 'CLOSED'.
After that it changes $user['poststats_month'] of every user to 0

for some reason it only works when I manually go to Task Manager and execute it,
I have enabled cron on my server which execute all Tasks every few sec(10-20sec).

all other tasks runs fine. Only idk why this get stuck.

I am free to give any other info if needed.
Any Help would be greatly appreciated
I don't know why you create a function poststats_publish_post (which is used only one time when task runs) and why you declare it inside the main function

This code might be a little bit better:
<?php
//Task File poststats_month.php
require_once MYBB_ROOT . 'inc/datahandlers/post.php';

function task_poststats_month($task)
{
	global $mybb, $db;
	$usergroup = $mybb->settings['poststats_group'];
	$message = "[b] Following users still haven't completed their posts:[/b]".PHP_EOL;
	$query = $db->simple_select("users", "*", "usergroup IN (".$usergroup.") OR additionalgroups IN (".$usergroup.")", ['order_by' => 'poststats_month', 'order_dir' => 'DESC']);
	while($user = $db->fetch_array($query)) {
		if($user['poststats_month'] < $mybb->settings['poststats_limit']) {
			$message .="@'".$user['username']."'  -  [b][color=red]".$user['poststats_month']."[/color][/b]".PHP_EOL;
		}
	}
	$message .= PHP_EOL."Terminate Their Services!";
	poststats_publish_post($mybb->settings['poststats_tid'], $message, $mybb->settings['poststats_poster_uid']);
	$day_record = ["poststats_month" => 0];
	$query = $db->update_query('users', $day_record);
	add_task_log($task, "Post Statistics for this month have been reset.");
}

function poststats_publish_post($tid, $message, $uid) {
	global $db;
	$posthandler = new PostDataHandler('insert');
	$posthandler->action = 'do_newreply';
	$new_post = [
		"tid" => $tid,
		"uid" => $uid,
		"message" => $message,
		"ipaddress" => '127.0.0.1',
		"signature" => "0",
		"subscriptionmethod" => "0",
		"disablesmilies" => "0"
	];
	$posthandler->set_data($new_post);
	$valid_post = $posthandler->validate_post();
	if($valid_post) {
		$posthandler->insert_post();
		$db->update_query('threads', ['closed'=>1], "tid = ".$tid);
	}
}
Crazycat,
I had created the function to make it look abit cleaner for future progress.
Thanks for the help,
but it still haven't solved the problem.

Regards,
Pehaps the trouble is that the thread is closed.
Try the following code:
<?php
//Task File poststats_month.php
require_once MYBB_ROOT . 'inc/datahandlers/post.php';

function task_poststats_month($task)
{
	global $mybb, $db;
	$usergroup = $mybb->settings['poststats_group'];
	$message = "[b] Following users still haven't completed their posts:[/b]".PHP_EOL;
	$query = $db->simple_select("users", "*", "usergroup IN (".$usergroup.") OR additionalgroups IN (".$usergroup.")", ['order_by' => 'poststats_month', 'order_dir' => 'DESC']);
	while($user = $db->fetch_array($query)) {
		if($user['poststats_month'] < $mybb->settings['poststats_limit']) {
			$message .="@'".$user['username']."'  -  [b][color=red]".$user['poststats_month']."[/color][/b]".PHP_EOL;
		}
	}
	$message .= PHP_EOL."Terminate Their Services!";
	if (poststats_publish_post($mybb->settings['poststats_tid'], $message, $mybb->settings['poststats_poster_uid'])==true) {
		$day_record = ["poststats_month" => 0];
		$query = $db->update_query('users', $day_record);
		add_task_log($task, "Post Statistics for this month have been reset.");
	} else {
		add_task_log($task, "Error validating post");
	}
}

function poststats_publish_post($tid, $message, $uid) {
	global $db;
	$db->update_query('threads', ['closed'=>0], "tid = ".$tid);
	$posthandler = new PostDataHandler('insert');
	$posthandler->action = 'do_newreply';
	$new_post = [
		"tid" => $tid,
		"uid" => $uid,
		"message" => $message,
		"ipaddress" => '127.0.0.1',
		"signature" => "0",
		"subscriptionmethod" => "0",
		"disablesmilies" => "0"
	];
	$posthandler->set_data($new_post);
	$valid_post = $posthandler->validate_post();
	if($valid_post) {
		$posthandler->insert_post();
		$db->update_query('threads', ['closed'=>1], "tid = ".$tid);
		return true;
	} else {
		return false;
	}
}
I added 2 things:
- unclose the thread before add new post
- tasklog if there is an error when validating post
thanks for the extra effort,
though looks like no success, though this time I am able to conclude a few things:

first time when I manually executed the task, then it worked, it posted the post and closed the thread, then I waited it to execute by itself, then I found that the thread which was closed, opens automatically but without getting any extra post. and after that, it not works, even the manual execution on that particular thread, I need to change the $mybb->settings['poststats_tid'] to any other thread id for it to work again manually.

also when the first time I executed the task manually, then it gave the task log:
Post Statistics for this month have been reset.
but after that when the thread got auto - opened, there was no task logs for the task, not even after my manual execution.
and by then the Next Run time gets stuck on a particular time,
like after the 1st execution if next run should be at 5:20(today) then even after 5:22(today), it will show the next run to be 5:20.

Also If I delete the first post which was created by the task (the first time mentioned above),
Even after deletion in the index page of forum it shows the last post to be that only (for the forum in which the thread is placed), though when I click it, then it redirects me to the thread where the post does not exist (since I had deleted it).

few more points to point out:
Here the poster is an admin's uid,
and the threads are from a private forum that can't be accessed by normal users.

and I have removed the task_image from each template of the themes since I was using system's cron.
It's late for me, so I'll have a deeper look tomorrow, but I noticed something with your last explanations:
Your settings are (in words) : run the task every firstday of the month at 0:00.
Even if you run the task manually, you won't override the "nextrun" field. If you want to test the task, set minutes and hour to "*" and reset the field in the database
(2020-06-04, 10:03 PM)Crazycat Wrote: [ -> ]Even if you run the task manually, you won't override the "nextrun" field. If you want to test the task, set minutes and hour to "*" and reset the field in the database
For the nextrun test, I had feeded the time to be 1st of every month,
Other than that, I am currently using the * on hours and minutes,days.

Quote:and reset the field in the database
You meant to say 'submit the updates after putting * on each fields of the task'? If so, then I already had. Just for the nextrun test I had the date to be 1st of every month.

Regards,
(2020-06-05, 11:25 AM)HMR Wrote: [ -> ]
Quote:and reset the field in the database
You meant to say 'submit the updates after putting * on each fields of the task'? If so, then I already had. Just for the nextrun test I had the date to be 1st of every month.
No, I said what I said. Do an update in mysql: update mybb_tasks set newtrun=0 where tid=XX, with XX which is your task id.
Any updates?
Pages: 1 2