8

I have a list of unpublished nodes, created with the Views module. I would like a task to be run 3 times a day, at specified times. Every time the task runs, only one of the nodes on the list should be published. How can I do this?

Steps I have tried

Scheduler

The Scheduler module requires you to specify the exact time you want the node to be published. This makes it complicated, as I would have to work out when each node is supposed to be published.

Views Bulk Operations + Rules + Rules Scheduler

With the Views Bulk Operations module, you can create a list of Nodes that are exposed to the Rules module. Rules can then act on the list and publish the node on the list. You can tell Rules to only publish a specific number of nodes.

This works perfectly. Except, it doesn't seem possible to schedule the Rule to run 3 times a day.

You can link the Rule to the main site Cron job, but this doesn't seem very precise as the Cron job could run more than 3 times.

There is a Rules Scheduler module (a sub-module of Rules), but it doesn't look like you can use this a Rules condition. There also doesn't seem to be a way to specify the running time.

Job Scheduler

This lets you set a time, but it doesn't seem to integrate with Rules or Views.

  • Any reason you don't want to do this through a custom module? I can provide you an answer if you want to do it this way. – Aram Boyajyan May 26 '15 at 18:01
  • @Topsitemakers I was hoping to use an existing module, as it will have been passed through more users so will be better code than mine!. I'm under the impression that what I want to do is possible, I just need to get the task scheduling bit sorted! – big_smile May 26 '15 at 18:04
5
+50

Using modules VBO, Rules and Rules Scheduler

About your approach using modules Views Bulk Operations (VBO), Rules and Rules scheduler (submodule of Rules), for which you wrote "it doesn't seem possible to schedule the Rule to run 3 times a day.": that appears to me "a" valid approach.

What you actually need to do is a minor variation of what is explained in the tutorial Hourly tasks using rules scheduler: in your case you should use an offset of 8 hours (instead of just 1).

And as explained also in that tutorial: Initially execute the component manually. (from then on every other 8 hours the rule will be executed again).

Using modules Rules and Rules Once per Day

There is a fairly new module related to rules, i.e. the "Rules Once per Day" module. Here is how it works (as per the comment in issue 2495775, from the module owner):

  • You specify a trigger hour on the administration settings page for this module.
  • The Rule trigger will then run when cron tasks are first run after the start of that hour. The actual run time will depend on your cron task timings.

The way I understand/Read this (haven't experimented with it yet though) is like so:

  • The "Event" will only be triggered when a cron job is run.
  • And that event will only be triggered 1 time / day, i.e. "next time cron runs after the trigger hour has passed".

So by creating 3 similar rules, you could have 1 of them run around (say) 6 pm, another one around 2 pm and the ast one at 10 pm.

PS: I'm assuming this question is about D7.

  • How do I get it to run at one time. There doesn't seem to be an option to activate a rule at a certain time. – big_smile May 27 '15 at 7:33
  • Are you familiar with the Rules Once a Day module? Refer to my updated answer ... curious to hear if that culd work ... – Pierre.Vriens Jun 6 '15 at 16:37
  • "Rules Once a Day" only runs once a day. But I am hoping I could add an offset to get the times I want. Rules allows you to use stringtotime(). If "rules once per a day" ran at midnight and I wanted my rule to run at 6pm, would +18 hours be the correct way to write the stringtotime command? – big_smile Jun 6 '15 at 17:36
  • True that Rules Once a Day only runs once a day (as its name also indicates). But if you define 3 (similar) rules, you then have 3 times once a day, right? Also, the actual trigger will be when your cron job runs. Say cron runs every hour, also on 6:05 pm (18:05). Then your "trigger hour" should be a little bit before that (like +18 hours), so that when the next cron runs (at 18:05), you get the trigger. If your cron also runs at (eg) 12:05 and 04:05, you should have another rule with trigger hour like +12 hours and yet another one like +4 hours. – Pierre.Vriens Jun 6 '15 at 19:37
  • 1
    Although I've only 90% tested this, it's the answer that has worked the best so far, so it gets the bounty. – big_smile Jun 10 '15 at 6:13
1

You can achieve this by using some coding.

  1. A view that lists all the nodes that are published for scheduling. If you also apply DraggableViews you can let administrators reorder the nodes by drag-and-drop, which is always nice.

  2. A custom PHP function publishdaily_get_top_node() that executes the view above and returns the NID for the top node. I would recommend putting this in a custom mini-module.

  3. A rule that is triggered on cron run, and checks whether it is time to publish a new node (by, say, checking if the time is between 00.00 and 00.15 – depending on how often your cron runs). If it is time, then it fires action 'load node' and uses publishdaily_get_top_node() to get the NID to load. Then it publishes the loaded node.

If you want to make it even more clean, you write a Rules plugin instead of just a PHP function.

If you want it less clean, you write the function directly in an action for executing arbitrary PHP code.

Reference

  • checking if the time is between 00.00 and 00.15 How would you check the time in rules? Thanks. – big_smile Jun 6 '15 at 16:26
  • you can use a rule scheduler or job scheduler – rfegade Jun 8 '15 at 14:48
  • Welcome to Drupal Answers! If you are going to copy-paste an answer, please link the original post back. – kiamlaluno Jun 29 '15 at 21:51
1
  1. Configuring cron jobs using the cron command at specified times you like.

  2. Create a module to implement hook_cron(). To update your first node as $node->status = 1.

    $node->status = 0, means unpublished.
    $node->status = 1, means published.

    function yourmodule_cron() {
      update $node->status = 1;    
    }
    
  3. You may use views_get_view() get your node nid

    $view = views_get_view('your_views_name');
    
    $view->execute('default');
    
    $view->preview();
    
    $renderer = $view->style_plugin->row_plugin;
    
    foreach ($view->result as $index => $row) {
    
      // get node NID from first row    
    
    }
    
  • How do I target the unpublished nodes? Thanks! – big_smile Jun 3 '15 at 15:01
  • Use views_get_view() to get your node NID – Dong Tao Jun 3 '15 at 16:30
0

Whether you use a rule or code to publish a single node from the top of your view, I believe the easiest way to schedule it is to use Ultimate Cron.

Set the scheduler to Crontab and for the specific task you can tell it exactly when you want it to run. For instance if you want 8am, 12 noon and 4pm you can make the rule:

0 8,12,16 * * *

Further Information:

Elysia Cron appears to allow for custom cron tasks, which may allow you to target a specific rule (perhaps via php code).

  • with Ultimate Cron is it possible to trigger a rule. I tried playing with it, but couldn't figure it out. Rules doesn't seem to be able to respond to specific cron jobs (it reacts to all of them). – big_smile Jun 5 '15 at 6:51
  • I didn't think so but it looks like Elysia Cron might allow you to create custom jobs. – Darvanen Jun 9 '15 at 1:49
0

Create a rule as follows:

At /admin/config/workflow/rules/components/add

Create a "Action set": Name = Unpublish Content Variables: Data type = Node Label = Node Machine name = node -Continue-

    Add Actions:
      Node > Unpublish content
      Data selector = node
      -Save-

At /admin/config/workflow/rules Create a "rule": Name = Unpublish Rule React on event = Node > After saving new content Restrict by type = your content-type -Save-

    Conditions:
              Content is published
            Data selector = node
              -Save-

    Actions:
      Schedule component evaluation
      Component Value = Unpublish Content
      Scheduled evaluation date data selector = node:field-your-node-date-field
      Identifier value = Unpublish content ID [node:nid] ([node:title]).
      Node Data selector = node
      -Save-

That is, it is not necessary to filter the content when create the component nor add a content-type condition in the rule.

Additionally, check your website cron setting from Admin >> Configuration >> System >> Cron (admin/config/system/cron)

Let me know if you face any query/concern regarding this.

  • Thanks! How do I specify the times when the rule should run? (I only want it to run at specific times). How do I get it to publish only one of the nodes in my Views list of unpublished nodes? – big_smile Jun 3 '15 at 15:00

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.