You are here

function gardens_site_data_json_alert_flag_set in Acquia Cloud Site Factory Connector 8.2

Same name and namespace in other branches
  1. 8 acsf_init/lib/sites/g/sites.inc \gardens_site_data_json_alert_flag_set()

Tries to set a flag, marking that an issue with sites.json exists.

This function is not supposed to be used for checking that there is an issue with sites.json; it should only be called if an issue exists.

As we do not have a DB connection, and we assume gluster is the primary suspect for issues, the lock will live on the ephemeral disk. If something strange happens while setting the flag (like the file cannot be opened or written to), the function will always return empty string, and no logging/ alerting is done at all. We basically have a choice between this and flooding watchdog/syslog/the factory with alerts.

1 string reference to 'gardens_site_data_json_alert_flag_set'
gardens_site_data_refresh_domains in acsf_init/lib/sites/g/sites.inc
Returns data for the specified domains directly from the JSON file.

File

acsf_init/lib/sites/g/sites.inc, line 678
ACSF helper functions for Drupal's multi-site directory aliasing feature.

Code

function gardens_site_data_json_alert_flag_set() {
  $lock_file = sprintf(GARDENS_SITE_JSON_ALERT_LOCK_TEMPLATE, $_ENV['AH_SITE_GROUP'], $_ENV['AH_SITE_ENVIRONMENT']);
  if (!file_exists($lock_file)) {

    // Create/open file, do not generate an error in race conditions (two
    // processes opening the file at the same time).
    $fh = fopen($lock_file, 'c');
    if ($fh) {

      // Get (exclusive, non-blocking) lock. Note we assume we can actually rely
      // on flock; see multithreading notes in the php.net docs.
      if (flock($fh, LOCK_EX | LOCK_NB)) {

        // Something more evasive than the 'fopen()' race condition: what
        // happens just around the time a sites.json issue stops existing? Could
        // one slow process that still thinks there is an issue, be delayed and
        // execute this code just _after_ another process removed the flag? That
        // would result in a superfluous alert being sent out at that time. To
        // prevent this, we repeat the check. (We often would need to do this
        // check anyway, somewhere, if we did not know the issue type yet.)
        $issue_type = gardens_site_data_sites_json_issue_type_get();

        // Send the alert to the Site Factory.
        $alert_sent = FALSE;
        if ($issue_type) {
          $response = gardens_site_data_alert_send('sites_json', $issue_type);
          if ($response->code == 200 && !empty($response->body['received'])) {
            $alert_sent = TRUE;
          }
        }

        // Remove the lock file if issue has gone away or the alert was not
        // processed by the Site Factory.
        if (!$alert_sent) {

          // Remove the lock file (name; the file/handle itself is still
          // locked/open, which is fine).
          unlink($lock_file);
        }

        // Release the lock.
        flock($fh, LOCK_UN);
      }
      fclose($fh);
    }
  }
}