Programming scheduled tasks

Re: Programming scheduled tasks

by Lloyd D -
Number of replies: 6

Hi everyone, 

I'm not sure if Alejandro managed to do what he wanted due to the Moodle version he was running... but looks like I'm trying to achieve the same task but I'm having difficulties in doing so. I've created the class for the scheduler to look at to perform the scheduled tasks (under mod/assign/submission/myplugin/classes/task/scheduler.php), as so:

testing code to run in schedule

I've also created an entry for my plugin under mod/assign/submission/myplugin/db/tasks.php

tasks.php under db directory

I don't think my namespaces are correct too.. When I upgrade my plugin I get the following error:

Failed to load task: \/mod/assign/submission/myplugin/task/scheduleTest

But my question is, how can I schedule a task which I have currently running in locallib.php? I have a function in that file which is executed when a student submits their assignment (when the file uploaded event is triggered). How can I run the piece of code which I currently have in that function in the scheduler class in scheduler.php? Hope that all makes a bit of sense :/ 

In reply to Lloyd D

Re: Programming scheduled tasks

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

/mod/assign/submission/myplugin/task/scheduleTest is not a valid class name in PHP so this cannot possibly work. Note, this is a class name, not a path. Try looking at some examples of other plugins with working scheduled tasks.

https://docs.moodle.org/dev/Automatic_class_loading may also provide some useful background.

In reply to Tim Hunt

Re: Programming scheduled tasks

by Lloyd D -

Ah yes I thought what I had wasn't quite right. I'll follow your advice and look for other plugins that make use of scheduled tasks. Would it be possible though for the scheduled task to trigger when a function in my plugin is executed? i.e. a function I have in locallib.php executes when the student submits their submission.. their submission (programming code they submitted) will be sent to an external server for unit testing. The result is then returned to the student after completion. Would this set-up be possible using the Task API? i.e. their submission will be scheduled as a task to be performed later, and the results displayed back to the student when the task is completed?

In reply to Tim Hunt

Re: Programming scheduled tasks

by Lloyd D -

Hi Tim, I've just managed to successfully set up the task by following your advice in looking at other plugins, but now I'm wondering how can I pass parameters to that task from my plugin (from locallib.php).

In reply to Tim Hunt

Re: Programming scheduled tasks

by Lloyd D -

Sorry for posting again but I've made great progress so far. I've gone for the adhoc approach as I thought it would be most suitable for my plugin needs. Only thing I'm unsure of though.. how do I schedule the execute function in the class to be run? At the moment I have:


$domination = new \assignsubmission_myplugin\task\example();

// queue it

 \core\task\manager::queue_adhoc_task($domination);


But when the above is run the execute function is not scheduled to run. 

I'm unsure to how the Task API handles tasks. Am I missing the point somewhere :/

In reply to Lloyd D

Re: Programming scheduled tasks

by Lloyd D -

I still can't work out what to do here :/ I'm sure I've set the task up correctly but when the task is triggered by clicking the submit button on the form, the browser hangs until the task is done.. shouldn't it allow the browser to complete the submit request then execute the task? Would it be possible that when the student clicks the submit button, could their submission be used in the task then once the task is completed the results are sent back to the student feedback form? 

In reply to Lloyd D

This forum post has been removed

The content of this forum post has been removed and can no longer be accessed.