Handles canceled payment transactions.
[platal.git] / bin / cron / google_apps.php
index c23f7d1..ee4a180 100755 (executable)
@@ -1,7 +1,7 @@
 #!/usr/bin/php5 -q
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2011 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -97,12 +97,18 @@ while ($nickname = $res->next()) {
     // we might run in troubler later if we don't keep the two repos. If we need
     // to add a forlife-looking nickname at some point, we'll do it manually.
     if (!preg_match('/^[-a-z]+\.[-a-z]+\.\d{4}$/', $nickname['nickname'])) {
-        XDB::execute(
-            "INSERT  INTO gapps_queue
-                SET  q_recipient_id = {?}, p_entry_date = NOW(), p_notbefore_date = NOW(),
-                     p_priority = 'offline', j_type = 'n_create', j_parameters = {?}",
-            $nickname['uid'],
-            json_encode($nickname));
+        $pending_tasks = XDB::fetchOneCell(
+            "SELECT  COUNT(*)
+               FROM  gapps_queue
+              WHERE  q_recipient_id = {?} AND p_status = 'idle' AND j_type = 'n_create' AND j_parameters = {?}",
+            $nickname['id'], json_encode($nickname));
+        if ($pending_tasks == 0) {
+            XDB::execute(
+                "INSERT  INTO gapps_queue
+                    SET  q_recipient_id = {?}, p_entry_date = NOW(), p_notbefore_date = NOW(),
+                         p_priority = 'offline', j_type = 'n_create', j_parameters = {?}",
+                $nickname['id'], json_encode($nickname));
+        }
     }
 }
 
@@ -114,12 +120,18 @@ $res = XDB::iterator(
   LEFT JOIN  email_source_account AS s ON (s.uid = g.l_userid AND s.type = 'alias' AND s.email = g.g_nickname)
       WHERE  g.l_userid IS NOT NULL AND s.email IS NULL");
 while ($nickname = $res->next()) {
-    XDB::execute(
-        "INSERT  INTO gapps_queue
-            SET  q_recipient_id = {?}, p_entry_date = NOW(), p_notbefore_date = NOW(),
-                 p_priority = 'offline', j_type = 'n_delete', j_parameters = {?}",
-        $nickname['uid'],
-        json_encode($nickname));
+    $pending_tasks = XDB::fetchOneCell(
+        "SELECT  COUNT(*)
+           FROM  gapps_queue
+          WHERE  q_recipient_id = {?} AND p_status = 'idle' AND j_type = 'n_delete' AND j_parameters = {?}",
+        $nickname['id'], json_encode($nickname));
+    if ($pending_tasks == 0) {
+        XDB::execute(
+            "INSERT  INTO gapps_queue
+                SET  q_recipient_id = {?}, p_entry_date = NOW(), p_notbefore_date = NOW(),
+                     p_priority = 'offline', j_type = 'n_delete', j_parameters = {?}",
+            $nickname['id'], json_encode($nickname));
+    }
 }
 
 /* Retrieves successful job queues for post-queue processing. */
@@ -141,5 +153,5 @@ XDB::execute(
       WHERE  p_status = 'success' OR
              (p_status = 'hardfail' AND p_end_date < DATE_SUB(NOW(), INTERVAL 15 DAY))");
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>