Esempio n. 1
0
function digest_process($settings)
{
    global $DB_QUERY_CACHE;
    global $ENTITY_CACHE;
    global $interval_ts_upper;
    $interval_ts_upper = (int) elgg_extract("timestamp", $settings, time());
    $fork_id = (int) elgg_extract("fork_id", $settings, 0);
    // should new users be included
    $never_logged_in = false;
    if (elgg_get_plugin_setting("include_never_logged_in", "digest") == "yes") {
        $never_logged_in = true;
    }
    // backup some cache
    $entity_cache_backup = $ENTITY_CACHE;
    // should the site digest be sent
    if (digest_site_enabled()) {
        // prepare stats logging
        $site_stats = digest_prepare_site_statistics();
        // log some beginning stats
        $site_stats["general"]["mts_start_digest"] = microtime(true);
        $site_stats["general"]["ts_start_cron"] = $interval_ts_upper;
        $site_stats["general"]["peak_memory_start"] = memory_get_peak_usage(false);
        $site_intervals = array(DIGEST_INTERVAL_DEFAULT => digest_get_default_site_interval(), DIGEST_INTERVAL_WEEKLY => digest_get_default_distribution(DIGEST_INTERVAL_WEEKLY), DIGEST_INTERVAL_FORTNIGHTLY => digest_get_default_distribution(DIGEST_INTERVAL_FORTNIGHTLY), DIGEST_INTERVAL_MONTHLY => digest_get_default_distribution(DIGEST_INTERVAL_MONTHLY), "include_never_logged_in" => $never_logged_in, "offset" => (int) elgg_extract("site_offset", $settings, 0), "limit" => (int) elgg_extract("site_limit", $settings, 0));
        // find users
        if ($users = digest_get_site_users($site_intervals)) {
            // log selection time
            $site_stats["general"]["mts_user_selection_done"] = microtime(true);
            // use a fair memory footprint
            if ($DB_QUERY_CACHE) {
                $DB_QUERY_CACHE->clear();
            }
            $stats_last_memory = memory_get_usage(false);
            // process users
            foreach ($users as $user_setting) {
                // stat logging
                $site_stats[$user_setting["user_interval"]]["users"]++;
                $site_stats["general"]["users"]++;
                // sent site digest for this user
                $user = get_user($user_setting["guid"]);
                // log start time
                $stats_mts_before = microtime(true);
                // sent out the digest
                if (digest_site($user, $user_setting["user_interval"]) === true) {
                    // mail was sent
                    $site_stats[$user_setting["user_interval"]]["mails"]++;
                    $site_stats["general"]["mails"]++;
                }
                // stats logging
                $site_stats[$user_setting["user_interval"]]["total_time"] += microtime(true) - $stats_mts_before;
                // reset cache
                $GLOBALS["ENTITY_CACHE"] = $entity_cache_backup;
                if ($DB_QUERY_CACHE) {
                    $DB_QUERY_CACHE->clear();
                }
                unset($user);
                // stats logging of memory leak
                $stats_current_memory = memory_get_usage(false);
                $site_stats[$user_setting["user_interval"]]["total_memory"] += $stats_current_memory - $stats_last_memory;
                $stats_last_memory = $stats_current_memory;
            }
        }
        // cleanup some stuff
        unset($users);
        unset($site_intervals);
        // log some end stats
        $site_stats["general"]["mts_end_digest"] = microtime(true);
        $site_stats["general"]["peak_memory_end"] = memory_get_peak_usage(false);
        // save stats logging
        digest_save_site_statistics($site_stats, $interval_ts_upper, $fork_id);
        unset($site_stats);
    }
    // should the group digest be sent
    if (digest_group_enabled()) {
        // prepare stats logging
        $group_stats = digest_prepare_group_statistics();
        // log some beginning stats
        $group_stats["general"]["mts_start_digest"] = microtime(true);
        $group_stats["general"]["ts_start_cron"] = $interval_ts_upper;
        $group_stats["general"]["peak_memory_start"] = memory_get_peak_usage(false);
        // prepare group options
        $options = array("type" => "group", "limit" => false, "callback" => "digest_row_to_guid");
        if ($limit = (int) elgg_extract("group_limit", $settings, 0)) {
            $options["limit"] = $limit;
            $options["offset"] = (int) elgg_extract("group_offset", $settings, 0);
        }
        $group_intervals = array(DIGEST_INTERVAL_WEEKLY => digest_get_default_distribution(DIGEST_INTERVAL_WEEKLY), DIGEST_INTERVAL_FORTNIGHTLY => digest_get_default_distribution(DIGEST_INTERVAL_FORTNIGHTLY), DIGEST_INTERVAL_MONTHLY => digest_get_default_distribution(DIGEST_INTERVAL_MONTHLY));
        // ignore access to get all groups
        $ia = elgg_set_ignore_access(true);
        if ($group_guids = elgg_get_entities($options)) {
            // log selection time
            $group_stats["general"]["mts_group_selection_done"] = microtime(true);
            // use a fair memory footprint
            if ($DB_QUERY_CACHE) {
                $DB_QUERY_CACHE->clear();
            }
            $stats_last_group_memory = memory_get_usage(false);
            foreach ($group_guids as $group_guid) {
                // stats logging
                $group_stats["general"]["groups"]++;
                // make sure we can get the group
                elgg_set_ignore_access(true);
                // get group
                $group = get_entity($group_guid);
                // get group default interval
                $group_interval = $group->digest_interval;
                if (empty($group_interval)) {
                    // group has no interval, so fallback to site default
                    $group_interval = digest_get_default_group_interval();
                }
                $group_intervals[DIGEST_INTERVAL_DEFAULT] = $group_interval;
                // restore access
                elgg_set_ignore_access($ia);
                $stats_begin_user_selection = microtime(true);
                if ($users = digest_get_group_users($group_guid, $group_intervals, $never_logged_in)) {
                    // stats loggin
                    $group_stats["general"]["total_time_user_selection"] += microtime(true) - $stats_begin_user_selection;
                    // use a fair memory footprint
                    if ($DB_QUERY_CACHE) {
                        $DB_QUERY_CACHE->clear();
                    }
                    $stats_last_memory = memory_get_usage(false);
                    // process users
                    foreach ($users as $user_setting) {
                        // stat logging
                        $group_stats[$user_setting["user_interval"]]["users"]++;
                        if (!in_array($group_guid, $group_stats[$user_setting["user_interval"]]["groups"])) {
                            $group_stats[$user_setting["user_interval"]]["groups"][] = $group_guid;
                        }
                        $group_stats["general"]["users"]++;
                        // get the user
                        $user = get_user($user_setting["guid"]);
                        // log start time
                        $stats_mts_before = microtime(true);
                        // sent digest
                        if (digest_group($group, $user, $user_setting["user_interval"]) === true) {
                            // mail was sent
                            $group_stats[$user_setting["user_interval"]]["mails"]++;
                            $group_stats["general"]["mails"]++;
                        }
                        // stats logging
                        $group_stats[$user_setting["user_interval"]]["total_time"] += microtime(true) - $stats_mts_before;
                        // reset cache
                        $GLOBALS["ENTITY_CACHE"] = $entity_cache_backup;
                        if ($DB_QUERY_CACHE) {
                            $DB_QUERY_CACHE->clear();
                        }
                        unset($user);
                        // stats logging of memory leak
                        $stats_current_memory = memory_get_usage(false);
                        $group_stats[$user_setting["user_interval"]]["total_memory"] += $stats_current_memory - $stats_last_memory;
                        $stats_last_memory = $stats_current_memory;
                    }
                } else {
                    // stats logging
                    $group_stats["general"]["total_time_user_selection"] += microtime(true) - $stats_begin_user_selection;
                }
                // reset cache
                $GLOBALS["ENTITY_CACHE"] = $entity_cache_backup;
                if ($DB_QUERY_CACHE) {
                    $DB_QUERY_CACHE->clear();
                }
                unset($group);
                // stats logging of memory leak
                $stats_current_group_memory = memory_get_usage(false);
                $group_stats["general"]["total_memory"] += $stats_current_group_memory - $stats_last_group_memory;
                $stats_last_group_memory = $stats_current_group_memory;
            }
        }
        // restore access settings
        elgg_set_ignore_access($ia);
        // log some end stats
        $group_stats["general"]["mts_end_digest"] = microtime(true);
        $group_stats["general"]["peak_memory_end"] = memory_get_peak_usage(false);
        // save stats logging
        digest_save_group_statistics($group_stats, $interval_ts_upper, $fork_id);
    }
}
Esempio n. 2
0
/**
 * Lets figure out what we need to do
 * 
 * @param string $hook
 * @param string $entity_type
 * @param bool $returnvalue
 * @param array $params
 */
function digest_cron_handler($hook, $entity_type, $returnvalue, $params)
{
    global $interval_ts_upper;
    if (!empty($params) && is_array($params)) {
        $interval_ts_upper = (int) elgg_extract("time", $params, time());
        // prepare some settings
        $digest_settings = array("timestamp" => $interval_ts_upper, "fork_id" => 0);
        // rebase the stats
        digest_rebase_stats($interval_ts_upper);
        // is multicore support enabled
        if (($cores = (int) elgg_get_plugin_setting("multi_core", "digest")) && $cores > 1) {
            // add some settings for the commandline
            $digest_settings["memory_limit"] = ini_get("memory_limit");
            $digest_settings["host"] = $_SERVER["HTTP_HOST"];
            $digest_settings["secret"] = digest_generate_commandline_secret();
            if (isset($_SERVER["HTTPS"])) {
                $digest_settings["https"] = $_SERVER["HTTPS"];
            }
            // shoul we include users who have never logged in
            $include_never_logged_in = false;
            if (elgg_get_plugin_setting("include_never_logged_in", "digest") == "yes") {
                $include_never_logged_in = true;
            }
            // multi core is enabled now try to find out how many users/groups to send per core
            $site_users_count = 0;
            $site_users_interval = 0;
            $group_count = 0;
            $group_interval = 0;
            // site digest settings
            if (digest_site_enabled()) {
                $site_intervals = array(DIGEST_INTERVAL_DEFAULT => digest_get_default_site_interval(), DIGEST_INTERVAL_WEEKLY => digest_get_default_distribution(DIGEST_INTERVAL_WEEKLY), DIGEST_INTERVAL_FORTNIGHTLY => digest_get_default_distribution(DIGEST_INTERVAL_FORTNIGHTLY), DIGEST_INTERVAL_MONTHLY => digest_get_default_distribution(DIGEST_INTERVAL_MONTHLY), "include_never_logged_in" => $include_never_logged_in);
                $site_users = digest_get_site_users($site_intervals);
                $site_users_count = count($site_users);
                $site_users_interval = (int) ceil($site_users_count / $cores);
            }
            // group digest settings
            if (digest_group_enabled()) {
                $group_options = array("type" => "group", "count" => true);
                $group_count = elgg_get_entities($group_options);
                $group_interval = (int) ceil($group_count / $cores);
            }
            // start processes
            $site_offset = 0;
            $group_offset = 0;
            for ($i = 0; $i < $cores; $i++) {
                $digest_settings["fork_id"] = $i;
                if ($site_users_count > 0) {
                    $digest_settings["site_offset"] = $site_users_interval * $i;
                    $digest_settings["site_limit"] = $site_users_interval;
                }
                if ($group_count > 0) {
                    $digest_settings["group_offset"] = $group_interval * $i;
                    $digest_settings["group_limit"] = $group_interval;
                }
                digest_start_commandline($digest_settings);
            }
        } else {
            // procces the digest
            digest_process($digest_settings);
        }
    }
}