Scalr::StoreEvent PHP Method

StoreEvent() public static method

Store event in database
public static StoreEvent ( integer $farmid, AbstractServerEvent $event, $eventTime = null )
$farmid integer
$event AbstractServerEvent
    public static function StoreEvent($farmid, AbstractServerEvent $event, $eventTime = null)
    {
        $eventServerId = isset($event->DBServer->serverId) ? $event->DBServer->serverId : null;
        try {
            $DB = self::getDb();
            // Generate event message
            $message = $event->getTextDetails();
            $suspend = 0;
            if ($event instanceof HostDownEvent) {
                $suspend = $event->isSuspended;
            } elseif ($event instanceof BeforeHostTerminateEvent) {
                $suspend = $event->suspend;
            }
            // short_message temporary used for time tracking
            // Store event in database
            $DB->Execute("INSERT INTO events SET\n                farmid\t= ?,\n                type\t= ?,\n                dtadded\t= NOW(),\n                message\t= ?,\n                event_object = ?,\n                event_id\t = ?,\n                event_server_id = ?,\n                short_message = ?,\n                msg_expected = ?,\n                msg_created = ?,\n                scripts_total = ?,\n                is_suspend = ?", array($farmid, $event->GetName(), $message, json_encode($event->handledObservers), $event->GetEventID(), $eventServerId, $eventTime, $event->msgExpected, $event->msgCreated, $event->scriptsCount, $suspend));
        } catch (Exception $e) {
            self::getContainer()->logger(__CLASS__)->fatal(sprintf(_("Cannot store event in database: %s"), $e->getMessage()));
        }
        try {
            if (isset($eventServerId)) {
                $dbServer = DBServer::LoadByID($eventServerId);
                if (!$dbServer->farmRoleId) {
                    return true;
                }
                $dt = new DateTime('now', new DateTimeZone("UTC"));
                $timestamp = $dt->format("D d M Y H:i:s e");
                $payload = new stdClass();
                $payload->eventName = $event->GetName();
                $payload->eventId = $event->GetEventID();
                $payload->timestamp = $timestamp;
                $globalVars = Scalr_Scripting_GlobalVariables::listServerGlobalVariables($dbServer, true, $event);
                $webhooks = WebhookConfig::findByEvent($event->GetName(), $farmid, $dbServer->clientId, $dbServer->envId);
                $count = 0;
                foreach ($webhooks as $webhook) {
                    /* @var $webhook \Scalr\Model\Entity\WebhookConfig */
                    $payload->configurationId = $webhook->webhookId;
                    $payload->data = array();
                    $variables = [];
                    foreach ($globalVars as $gv) {
                        $variables[$gv->name] = $gv->value;
                        if ($gv->private && $webhook->skipPrivateGv == 1 && !$gv->system) {
                            continue;
                        }
                        $payload->data[$gv->name] = $gv->value;
                    }
                    if ($webhook->postData) {
                        //Parse variable
                        $keys = array_keys($variables);
                        $keys = array_map(function ($item) {
                            return '{' . $item . '}';
                        }, $keys);
                        $values = array_values($variables);
                        // Strip undefined variables & return value
                        $payload->userData = preg_replace("/{[A-Za-z0-9_-]+}/", "", str_replace($keys, $values, $webhook->postData));
                    } else {
                        $payload->userData = '';
                    }
                    foreach ($webhook->getEndpoints() as $ce) {
                        /* @var $ce \Scalr\Model\Entity\WebhookConfigEndpoint */
                        $endpoint = $ce->getEndpoint();
                        if (!$endpoint->isValid) {
                            continue;
                        }
                        $payload->endpointId = $endpoint->endpointId;
                        $encPayload = json_encode($payload);
                        $history = new WebhookHistory();
                        $history->eventId = $event->GetEventID();
                        $history->eventType = $event->GetName();
                        $history->payload = $encPayload;
                        $history->serverId = $event->DBServer ? $event->DBServer->serverId : null;
                        $history->endpointId = $endpoint->endpointId;
                        $history->webhookId = $webhook->webhookId;
                        $history->farmId = $farmid;
                        $history->save();
                        $count++;
                    }
                }
                if ($count != 0) {
                    $DB->Execute("UPDATE events SET wh_total = ? WHERE event_id = ?", array($count, $event->GetEventID()));
                }
            }
        } catch (Exception $e) {
            self::getContainer()->logger(__CLASS__)->fatal(sprintf(_("WebHooks: %s"), $e->getMessage()));
        }
    }