Adding a Cron job to a block in moodle - moodle

I am trying to add a cron job in moodle block.
I am following this tutorial on moodle 3.0 https://docs.moodle.org/dev/Blocks#Responding_to_Cron
When I run /admin/cron.php, my cron job does not execute.
Am I missing anything here?

The old way uses cron like this:
/blocks/yourblockname/block_yourblockname.php
class block_yourblockname extends block_base {
...
public function cron() {
// Your code.
}
and in /blocks/yourblockname/version.php
$plugin->cron = xxxx; // Cron interval in seconds. 0 means no cron.
The new way is to use scheduled tasks - https://docs.moodle.org/dev/Task_API
UPDATE: As mentioned by #Developer
If you change the cron value or add a new task then you will also need to increment the version number
$plugin->version = xxxx;

Related

Cadence Java client, unable to get history of workflow executions

I am following the idea, mentioned in this answer and trying this:
workflowTChannel.ListClosedWorkflowExecutions(ListClosedWorkflowExecutionsRequest().apply {
domain = "valid-domain-name"
startTimeFilter = StartTimeFilter().apply {
setEarliestTime(Instant.parse("2023-01-01T00:00:00.000Z").toEpochMilli())
setLatestTime(Instant.parse("2024-01-01T00:59:59.999Z").toEpochMilli())
}
})
However, the result is always an empty list.
Fetching the list via UI works fine at the same time.
Using PostgreSQL and local test installation without advanced visibility.
UPD: debugged Cadence locally and found that it expects nanoseconds instead of milliseconds. This way correct parameters must be prepared like this:
Instant.parse("2023-01-01T00:00:00.000Z").toEpochMilli() * 1000000
My guess is that you are using seconds and Cadence expects nanoseconds timestamps.

Use current Celery task in chord?

I'd like to run tasks in parallel that have a data dependency at the beginning of the first task. It seems that I should be able to start a chord with the current task in the header group that's used as the args for the body callback. I don't see a way to reference the signature of the current task in the documentation, but is there a way to do this?
I was thinking it would be something like this with the get_signature() being the missing piece:
#app.task(bind=True)
def chord_test(self, id_) -> int:
data, next_id = get_data(id_)
chord([self.get_signature(), chord_test.s(next_id)])(handle_results.s())
return expensive_processing(data)

How to manually trigger a cloudwatch rule with ScheduleExpression(10 days)

I have to setup "AWS::Events::Rule" in cloudwatch with ScheduleExpression(10 days), and write some code to test it, but I can not change the "10 days" to 1 minute or call the lambda function directly. I know that we can call put event for calling a rule with EventPattern.
But not know how to do that for ScheduleExpression.
Any comment is welcome, Thanks.
To my knowledge there's no possibility for you to manually trigger the rule and make it execute the lambda function. What you can do is change the frequency from 10 days to 1 minute, let it execute, and when it executes switch it back to 10 days
I also met this problem. I checked AWS document and it says that a rule can only contain either EventPattern or ScheduleExpression. But in order to call aws events put-events we must provide a Source for EventPattern match. So I think we cannot manually trigger a scheduled event.
Not sure what's your use case, but I have decided to move to use Invoke API of AWSLambda client.
SDK Approach:
Yes, you can use the putRule SDK function to update the ScheduleExpression of the CloudWatch Rule. As I mentioned in the below snippet
let params =
{
Name: timezoneCronName, /* required */
ScheduleExpression: cronExpression
}
return CloudWatchEvents.putRule(cloudWatchEventsParams).promise().then((response) => {
console.debug(`CloudWatch Events response`, response);
return response;
}).catch((error) => {
console.error(`Error occurred while updating Cloud Watch Event:${error.message}`);
throw error;
});
See this Official AWS SDK DOC.
CLI Approach:
Run the following command though CLI
aws events put-rule --name "You Rule name (not full ARN)" --schedule-expression "cron(0/1 * * * ? *)"

removeAll() from repository in scheduler task

For my scheduler task, I want to delete all the existing data from repository before updating it, every time the scheduler runs. I am able to save and add new data from XML File using add().
class FunctionFetcherService {
public function fetchRoomandLesson() {
$path = 'http:abc.dll?type=room&content=xml';
$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
$roomRepository = $objectManager->get("School\\booking\\Domain\\Repository\\RoomsRepository");
$this->roomRepository->removeAll();
$xml = simplexml_load_file($path);
$json = json_encode($xml);
$xmlArray = json_decode($json, true);
$serialized_array = serialize($xmlArray);
$unserialized_array = unserialize($serialized_array);
An error occurs removeAll() called on Null. I also referred to the already asked question: use removeAll() in scheduler task but it does doe not work.
You create the repository as variable $roomRepository and then try to access it through $this->roomRepository. Changing $this->roomRepository to $roomRepository should fix the error.
You should create an Extbase CommandController as your scheduler task. All registered commands are available in the scheduler as well as CLI commands.
By using CommandControllers you can use the full extbase framework, such as dependency injections and validations.
Please note that CommandController command methods must be suffixed with Command just like the linked example.

scala - How to run a task every day a 11PM

With playframework, I'm trying to run a function to send a mail every day at 11PM but I don't know how to do.
I found many answers accros the internet but I haven't managed to adapt with Scala language, do you have a example of tutorial ?
Have a look at Akka Scheduler and Play! Scheduled jobs
EDIT:
I'll personally prefer Play! scheduler which uses cron. So in the example (copy/paste), you could create a Scala class similar to this:
import play.jobs.*;
/** Fire at 12pm (noon) every day **/
#On("0 0 12 * * ?")
public class Bootstrap extends Job {
public void doJob() {
Logger.info("Maintenance job ...");
...
}
}
I suggest you to use akka quartz scheduler https://github.com/enragedginger/akka-quartz-scheduler