drupaldrupal-7drupal-rules

Rules scheduler for expire role and sending reminder email fire all at the same time


I have 3 rules which make up my "membership".

  1. Assign role when product X gets purchased (This one works!)
  2. Send a reminder email after 2 minutes.
  3. Remove role after 4 minutes.

Please note the 2 and 4 minutes are only there for testing purposes, they will become 350 and 365 days.

While both 2. or 3. work, I have two issues:

It seems as soon something triggers, all do.

As you can see from the exports, I made one rule trigger from x minutes of the commerce order completed, and the other from the role assigned, thinking that relating to 2 different events, I would solve the issue. It didn't work.

Here are the exports of my rules:

Rule "Send a reminder" component:

{ "rules_sends_email_to_pipps_reminder_1_week_prior_expiry" : {
"LABEL" : "Sends Email to PIPPS Reminder 1 week prior expiry",
"PLUGIN" : "rule set",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"USES VARIABLES" : { "user_argument" : { "label" : "User {argument}", "type" : "user" } },
"RULES" : [
  { "RULE" : {
      "DO" : [
        { "mail" : {
            "to" : "[commerce-order:mail]",
            "subject" : "Heads up!",
            "message" : "Yo! Heads up!",
            "from" : "pipps@siteemail.com",
            "language" : [ "site:current-cart-order:state" ]
          }
        }
      ],
      "LABEL" : "Send email reminder to PIPPS"
    }
  }
]

} }

Rule "Send a reminder" trigger:

{ "rules_send_reminder_email_to_pipps_role_trigger_rule_" : {
"LABEL" : "Send reminder email to PIPPS Role {trigger rule}",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "commerce_order", "rules", "rules_scheduler", "commerce_checkout" ],
"ON" : { "commerce_checkout_complete" : [] },
"IF" : [
  { "commerce_order_contains_product" : {
      "commerce_order" : [ "commerce_order" ],
      "product_id" : "PIPPS",
      "operator" : "=",
      "value" : "1"
    }
  }
],
"DO" : [
  { "schedule" : {
      "component" : "rules_sends_email_to_pipps_reminder_1_week_prior_expiry",
      "date" : {
        "select" : "site:current-cart-order:created",
        "date_offset" : { "value" : 120 }
      },
      "identifier" : "Reminder email to [account:uid]",
      "param_user_argument" : [ "commerce-order:owner" ]
    }
  }
]

} }

Rule "Role Expire" component:

{ "rules_expire_pipps_role_rule_set_" : {
"LABEL" : "Expire PIPPS Role {rule_set}",
"PLUGIN" : "rule set",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"USES VARIABLES" : { "user_argument" : { "label" : "User {argument}", "type" : "user" } },
"RULES" : [
  { "RULE" : {
      "DO" : [
        { "user_remove_role" : {
            "account" : [ "user_argument" ],
            "roles" : { "value" : { "13" : "13" } }
          }
        }
      ],
      "LABEL" : "Expire action {rule}"
    }
  }
]

} }

Rule "Expire Role" trigger:

{ "rules_remove_pipps_role_trigger_rule_" : {
"LABEL" : "Remove PIPPS Role {trigger rule}",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules", "rules_scheduler" ],
"ON" : { "user_update" : [] },
"IF" : [
  { "user_has_role" : { "account" : [ "account" ], "roles" : { "value" : { "13" : "13" } } } }
],
"DO" : [
  { "schedule" : {
      "component" : "rules_expire_pipps_role_rule_set_",
      "date" : {
        "select" : "site:current-cart-order:created",
        "date_offset" : { "value" : 240 }
      },
      "identifier" : "Remove User role [account:uid]",
      "param_user_argument" : [ "account" ]
    }
  }
]

} }

Any suggestion is very appreciated! Thanks.


Solution

  • Your Rules (and Rules Components), seem to "work as implemented" ... You 'issue' appears to be that you should be more patient during your testing ... Read on for more details ...

    Understanding what is happening

    After a rule is scheduled (using the Rules Scheduler submodule), it will only actually execute next time cron runs.

    Example:

    Because of what is described in the scenario of your question, it seems that timestamp X2 is happening BEFORE Y mins after timestamp X1. That would also explain why BOTH Rules Components actually execute (more or less) at the same time.

    Remedy to make it work as you really want

    Assuming that your cron jobs are running every hour, a straight forward (minor) fix to your Rules Components that is scheduled after 4 minutes, would be to schedule them after (e.g) 2 hours instead. So that you are sure that both Rules Component are not actually executed during the very same cron job.