/private/syncQueueProcess.php

https://github.com/ciniki/core · PHP · 105 lines · 52 code · 7 blank · 46 comment · 13 complexity · 995373648f86a84d5be06e04c98d576b MD5 · raw file

  1. <?php
  2. //
  3. // Description
  4. // -----------
  5. // This function will execute the queue requests for the sync.
  6. //
  7. // Arguments
  8. // ---------
  9. // ciniki:
  10. // tnid: The ID of the tenant on the local side to check sync.
  11. //
  12. function ciniki_core_syncQueueProcess(&$ciniki, $tnid) {
  13. //
  14. // Get the list of push syncs for this tenant,
  15. // and then execute all the queue process on each sync.
  16. //
  17. ciniki_core_loadMethod($ciniki, 'ciniki', 'core', 'private', 'dbHashIDQuery');
  18. ciniki_core_loadMethod($ciniki, 'ciniki', 'core', 'private', 'syncCheckVersions');
  19. ciniki_core_loadMethod($ciniki, 'ciniki', 'core', 'private', 'syncObjectPush');
  20. ciniki_core_loadMethod($ciniki, 'ciniki', 'core', 'private', 'syncLog');
  21. $strsql = "SELECT ciniki_tenant_syncs.id, ciniki_tenants.uuid AS local_uuid, "
  22. . "ciniki_tenants.sitename, "
  23. . "ciniki_tenant_syncs.flags, local_private_key, "
  24. . "remote_name, remote_uuid, remote_url, remote_public_key, UNIX_TIMESTAMP(last_sync) AS last_sync "
  25. . "FROM ciniki_tenants, ciniki_tenant_syncs "
  26. . "WHERE ciniki_tenants.id = '" . ciniki_core_dbQuote($ciniki, $tnid) . "' "
  27. . "AND ciniki_tenants.id = ciniki_tenant_syncs.tnid "
  28. . "AND (ciniki_tenant_syncs.flags&0x01) = 0x01 " // Push syncs only
  29. . "AND ciniki_tenant_syncs.status = 10 " // Active syncs only
  30. . "";
  31. $rc = ciniki_core_dbHashIDQuery($ciniki, $strsql, 'ciniki.tenants', 'syncs', 'id');
  32. if( $rc['stat'] != 'ok' ) {
  33. return $rc;
  34. }
  35. if( isset($rc['syncs']) ) {
  36. foreach($rc['syncs'] as $sync_id => $sync) {
  37. //
  38. // Setup logging
  39. //
  40. if( isset($ciniki['config']['ciniki.core']['sync.log_dir']) ) {
  41. $ciniki['synclogfile'] = $ciniki['config']['ciniki.core']['sync.log_dir'] . "/sync_" . $sync['sitename'] . "_$sync_id.log";
  42. }
  43. $ciniki['synclogprefix'] = "[" . $sync['sitename'] . "-" . $sync['remote_name'] . "]";
  44. $sync['type'] = 'tenant';
  45. //
  46. // Check the versions
  47. //
  48. $rc = ciniki_core_syncCheckVersions($ciniki, $sync, $tnid);
  49. if( $rc['stat'] != 'ok' ) {
  50. // Skip this sync
  51. ciniki_core_syncLog($ciniki, 0, "Unable to check sync versions for $sync_id", $rc['err']);
  52. continue;
  53. }
  54. foreach($ciniki['syncqueue'] as $queue_item) {
  55. //
  56. // Check if this is a sync we are to ignore, because the request came from that sync
  57. //
  58. // error_log("Sync: $sync_id, " . $queue_item['args']['ignore_sync_id']);
  59. if( isset($queue_item['args']['ignore_sync_id']) && $queue_item['args']['ignore_sync_id'] == $sync_id ) {
  60. continue;
  61. }
  62. if( isset($queue_item['push']) ) {
  63. ciniki_core_syncLog($ciniki, 1, "Push " . $queue_item['push'] . '(' . serialize($queue_item['args']) . ')', null);
  64. ciniki_core_loadMethod($ciniki, 'ciniki', 'core', 'private', 'syncObjectLoad');
  65. $rc = ciniki_core_syncObjectLoad($ciniki, $sync, $tnid, $queue_item['push'], array());
  66. if( $rc['stat'] != 'ok' ) {
  67. return array('stat'=>'fail', 'err'=>array('code'=>'ciniki.core.318', 'msg'=>'Unable to load object ' . $queue_item['push'], 'err'=>$rc['err']));
  68. }
  69. $o = $rc['object'];
  70. $rc = ciniki_core_syncObjectPush($ciniki, $sync, $tnid, $o, $queue_item['args']);
  71. }
  72. // else {
  73. // $method_filename = $ciniki['config']['ciniki.core']['root_dir'] . preg_replace('/^(.*)\.(.*)\.(.*)\.(.*)$/','/\1-mods/\2/sync/\3_\4.php', $queue_item['method']);
  74. // $method_function = preg_replace('/^(.*)\.(.*)\.(.*)\.(.*)$/','\1_\2_\3_\4', $queue_item['method']);
  75. // if( file_exists($method_filename) ) {
  76. // require_once($method_filename);
  77. // if( is_callable($method_function) ) {
  78. // error_log("SYNC-INFO: [$tnid] " . $queue_item['method'] . '(' . serialize($queue_item['args']) . ')');
  79. // $rc = $method_function($ciniki, $sync, $tnid, $queue_item['args']);
  80. // if( $rc['stat'] != 'ok' ) {
  81. // error_log('SYNC-ERR: ' . $queue_item['method'] . '(' . serialize($queue_item['args']) . ') - (' . serialize($rc['err']) . ')');
  82. // continue;
  83. // }
  84. // } else {
  85. // error_log('SYNC-ERR: Not executable ' . $queue_item['method'] . '(' . serialize($queue_item['args']) . ')');
  86. // continue;
  87. // }
  88. // } else {
  89. // error_log('SYNC-ERR: Doesn\'t exist' . $queue_item['method'] . '(' . serialize($queue_item['args']) . ')');
  90. // continue;
  91. // }
  92. // }
  93. }
  94. }
  95. }
  96. return array('stat'=>'ok');
  97. }
  98. ?>