2

Is it possible to schedule Migrate to import/update my XML regularly, say, once an hour? Maybe in Rules?

EDIT: I have a skeleton of a module written for this now, to hook into Rules.

Event: If there are unimported nodes, or if it has been more than 15 minutes since the last import

Condition: Just the machine name of the Migration, and its enddate timestamp

Actions: I want two, one to do an import and rollback, and another to update and import.

I am having trouble trying to find what functions to call to initiate the actions, and which ones to get my arguments. Is there any good documentation on the core Migrate functions (not field handlers or anything) so that I can see which ones I need?

EDIT 2 I ended up with this cron function. Is this how it's written? And I can put it into my module where the rest of my migration is? I also need to specify that it should update any nodes already imported. How do I do that?

    function mymodule_cron() {
            $last_run = variable_get('mymodule_last_cron', 0);
            $interval = variable_get('mymodule_interval', 60);
            if(time() - $last_run > $interval){
                 variable_set('mymodule_last_cron', time());
                 $migration = Migration::getInstance('ImportNodes');
                 $result = $migration->processImport();
            }
     }
3

I think the best way to achieve your goal is to use Drupal's cron, not Rules.

Cron is a service that will trigger the execution of a function, depending on the frequency you want it to.

You just have to implement hook_cron in your custom module to execute your migration. You will have to take care of the frequency you need for your script. I found this example useful.

How to run migration programmatically? As stated here, it's very simple:

<?php
  $migration = Migration::getInstance($machine_name);
  $result = $migration->processImport();
?>
  • I had just read something about cron but wasn't sure if it would work for Migration. I'm gonna try this. Thanks! – dodododo Jan 23 '13 at 15:27
  • Is there a way to get this to rollback and then update? Can I say $result = $migration->processRollback(); $result = $migration->processImport();? – dodododo Jan 23 '13 at 17:02
  • And do I just plug this into my migrate module .module file? Inside the cron call? I just want to make sure I have written everything correctly and put it where it belongs. – dodododo Jan 23 '13 at 21:05
  • Yes, $result = $migration->processRollback(); is good too. And yes, you should put that inside the cron call into .module file. – ermannob Jan 24 '13 at 9:19
  • Thanks a lot for the help! Now I need to get the rollback to not happen at the same time as the import. At least that's what I think is happening, because I end up with duplicate nodes... – dodododo Jan 24 '13 at 11:17
0

Have you had a look at the Feeds module? That could maybe help you with whatever you're trying to do. Also the Feeds XPath Parser will help you with any specific XML.

  • I've looked at Feeds and Migrate was better to work with, considering the complexity of the XML. I have all my data in; that isn't the problem. All I need is to be able to generate the new nodes from the XML files without having to manually click the Import button. I'd like it to run automatically. – dodododo Jan 22 '13 at 12:36
0

For Drupal 8, you can use the Migrate Scheduler module for it. It uses the Cron API internally.

The Migrate Scheduler module provides the functionality of executing the migrations on a particular schedule.

P.S - I am the author and maintainer of the module. Any feedback is welcomed in the issue queue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.