Adhoc scheduled task?

Adhoc scheduled task?

by Daniel Neis Araujo -
Number of replies: 4
Picture of Core developers Picture of Plugin developers Picture of Translators

Hello,


is there a way to schedule a task to run once in future?

What I am trying to achieve with this is the following:

* Let's say the user has to do something at least N minutes, so when the user starts to do something, I would like to schedule a task to now + N minutes that will check if it's done and take the necessary actions (of course it will take care of user's logouts and session timeouts and so, but there is no reason to test if it's done before the minimum amount of time and I think this approach is better than call a query each minute (even if it is only one query for all users)).

In classic unix cron I could set all the "time variables", that includes the year (moodle does not have year on it's syntax), and it would be executed only once in the expected date/time. But then that task would be there forever and would accumulate with others of the same kind and it's not really a good side effect.

Does anyone have any ideas?


Kind regards,

Daniel

Average of ratings: -
In reply to Daniel Neis Araujo

Re: Adhoc scheduled task?

by Brendan Anderson -

When the user does the thing, you could kick off another process to monitor his or her progress. I have had success with Gearman for similar stuff and it has a nice PHP client. The Gearman job could just sleep for N minutes and then check back with Moodle through it's web services to see if the user has completed the task. Or better, the job could be a Moodle CLI script, so it can communicate with Moodle directly.

Honestly, it's a lot to set up though. The more conventional approach is probably better, especially if many people are engaged in this workflow. If you run a query for users who have begun, but not completed, a task every single time cron runs, it is a waste only if no users are returned.

In reply to Brendan Anderson

Re: Adhoc scheduled task?

by Daniel Neis Araujo -
Picture of Core developers Picture of Plugin developers Picture of Translators

Hello, Brendan


this Gearman seems to be a nice tool, but really also seems to much work.

When you say "The Gearman job could just sleep for N minutes and then check", well, that is not really that elegant too, and could be done with Moodle's ad-hoc tasks that are non-blockable by default and run in background.


Kind regards,

Daniel


Average of ratings: Useful (1)
In reply to Daniel Neis Araujo

Re: Adhoc scheduled task?

by Michael Aherne -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers

I don't think that's possible at the moment. It would be a useful feature, though, so it might be worth raising in the Moodle Tracker.

On the face of it, it looks as if it would be a simple change to the queue_adhoc_task() method, to avoid overwriting a "next run time" value if one is supplied. It's possible it was implemented the way it is deliberately to prevent some other problem, though, so it's maybe not that simple.

In reply to Michael Aherne

Re: Adhoc scheduled task?

by Daniel Neis Araujo -
Picture of Core developers Picture of Plugin developers Picture of Translators

Hello, Michael


thanks for pointing out the method that needs changes.

I've created MDL-52385 (that has a link to the patch on github) to address this.

Let's say what other people think about it =)


Kind regards,

Daniel