diff --git a/lib/cronlib.php b/lib/cronlib.php index 703c1fb1719..f1c5caae833 100644 --- a/lib/cronlib.php +++ b/lib/cronlib.php @@ -342,17 +342,6 @@ function cron_run() { mtrace('done'); } - - if ($CFG->enableportfolios) { - // Portfolio cron - mtrace('Starting the portfolio cron...'); - cron_trace_time_and_memory(); - require_once($CFG->libdir . '/portfoliolib.php'); - portfolio_cron(); - mtrace('done'); - } - - //now do plagiarism checks require_once($CFG->libdir.'/plagiarismlib.php'); plagiarism_cron(); @@ -463,6 +452,15 @@ function cron_run() { cache_helper::cron(); mtrace('done.'); + // Portfolio cron - this needs to be close to the end because it may take a long time. + if ($CFG->enableportfolios) { + mtrace('Starting the portfolio cron...'); + cron_trace_time_and_memory(); + require_once($CFG->libdir . '/portfoliolib.php'); + portfolio_cron(); + mtrace('done'); + } + // Run automated backups if required - these may take a long time to execute require_once($CFG->dirroot.'/backup/util/includes/backup_includes.php'); require_once($CFG->dirroot.'/backup/util/helper/backup_cron_helper.class.php'); diff --git a/lib/db/events.php b/lib/db/events.php index d3ef19fa555..177d4d26397 100644 --- a/lib/db/events.php +++ b/lib/db/events.php @@ -36,22 +36,7 @@ defined('MOODLE_INTERNAL') || die(); /* List of legacy event handlers */ $handlers = array( -/* - * portfolio queued event - for non interactive file transfers - * NOTE: this is a HACK, please do not add any more things like this here - * (it is just abusing cron to do very time consuming things which is wrong any way) - * - * TODO: this has to be moved into separate queueing framework.... - * TODO: MDL-25508, MDL-41541 - */ - 'portfolio_send' => array ( - 'handlerfile' => '/lib/portfoliolib.php', - 'handlerfunction' => 'portfolio_handle_event', // argument to call_user_func(), could be an array - 'schedule' => 'cron', - 'internal' => 0, - ), - -/* no more here please, core should not consume any events!!!!!!! */ + /* No more old events! */ ); $observers = array( diff --git a/lib/db/install.xml b/lib/db/install.xml index f2666696832..794b70c6040 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1,5 +1,5 @@ - @@ -2357,6 +2357,7 @@ + @@ -3084,4 +3085,4 @@ - + \ No newline at end of file diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 973d4afa746..acdceb716d2 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2996,5 +2996,20 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2014020500.00); } + if ($oldversion < 2014020700.02) { + + // Define field queued to be added to portfolio_tempdata. + $table = new xmldb_table('portfolio_tempdata'); + $field = new xmldb_field('queued', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'instance'); + + // Conditionally launch add field queued. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Main savepoint reached. + upgrade_main_savepoint(true, 2014020700.02); + } + return true; } diff --git a/lib/portfolio/exporter.php b/lib/portfolio/exporter.php index d5f1fc28d01..29e5e69c01c 100644 --- a/lib/portfolio/exporter.php +++ b/lib/portfolio/exporter.php @@ -434,10 +434,11 @@ class portfolio_exporter { * @return bool whether or not to process the next stage. this is important as the control function is called recursively. */ public function process_stage_queueorwait() { + global $DB; + $wait = $this->instance->get_export_config('wait'); if (empty($wait)) { - // TODO MDL-42541 Removing usage of events_trigger(). - events_trigger_legacy('portfolio_send', $this->id); + $DB->set_field('portfolio_tempdata', 'queued', 1, array('id' => $this->id)); $this->queued = true; return $this->process_stage_finished(true); } diff --git a/lib/portfoliolib.php b/lib/portfoliolib.php index 4c567038713..467855ef17e 100644 --- a/lib/portfoliolib.php +++ b/lib/portfoliolib.php @@ -930,30 +930,9 @@ function portfolio_report_insane($insane, $instances=false, $return=false) { echo $output; } - -/** - * Event handler for the portfolio_send event - * - * @param int $eventdata event id - * @return bool - */ -function portfolio_handle_event($eventdata) { - global $CFG; - - require_once($CFG->libdir . '/portfolio/exporter.php'); - $exporter = portfolio_exporter::rewaken_object($eventdata); - $exporter->process_stage_package(); - $exporter->process_stage_send(); - $exporter->save(); - $exporter->process_stage_cleanup(); - return true; -} - /** * Main portfolio cronjob. * Currently just cleans up expired transfer records. - * - * @todo - MDL-15997 - Add hooks in the plugins - either per instance or per plugin */ function portfolio_cron() { global $DB, $CFG; @@ -969,6 +948,20 @@ function portfolio_cron() { } } } + + $process = $DB->get_records('portfolio_tempdata', array('queued' => 1), 'id ASC', 'id'); + foreach ($process as $d) { + try { + $exporter = portfolio_exporter::rewaken_object($d->id); + $exporter->process_stage_package(); + $exporter->process_stage_send(); + $exporter->save(); + $exporter->process_stage_cleanup(); + } catch (Exception $e) { + // This will get probably retried in the next cron until it is discarded by the code above. + mtrace('Exception thrown in portfolio cron while processing ' . $d->id . ': ' . $e->getMessage()); + } + } } /** diff --git a/version.php b/version.php index 286cc3a8769..61285cbdd49 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2014020700.01; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2014020700.02; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes.