Example #1
0
        private UndeploymentResult UndeployRemoveInternal(
            DeploymentInformation info,
            UndeploymentOptions undeploymentOptions)
        {
            var reverted = new DeploymentInformationItem[info.Items.Length];

            for (int i = 0; i < info.Items.Length; i++)
            {
                reverted[i] = info.Items[info.Items.Length - 1 - i];
            }

            var revertedStatements = new List <DeploymentInformationItem>();

            if (undeploymentOptions.IsDestroyStatements)
            {
                var referencedTypes = new HashSet <string>();

                Exception firstExceptionEncountered = null;

                foreach (DeploymentInformationItem item in reverted)
                {
                    EPStatement statement = _epService.GetStatement(item.StatementName);
                    if (statement == null)
                    {
                        Log.Debug("Deployment id '" + info.DeploymentId + "' statement name '" + item + "' not found");
                        continue;
                    }
                    referencedTypes.AddAll(_statementEventTypeRef.GetTypesForStatementName(statement.Name));
                    if (statement.IsDisposed)
                    {
                        continue;
                    }
                    try
                    {
                        statement.Dispose();
                    }
                    catch (Exception ex)
                    {
                        Log.Warn("Unexpected exception destroying statement: " + ex.Message, ex);
                        if (firstExceptionEncountered == null)
                        {
                            firstExceptionEncountered = ex;
                        }
                    }
                    revertedStatements.Add(item);
                }
                EPLModuleUtil.UndeployTypes(
                    referencedTypes, _statementEventTypeRef, _eventAdapterService, _filterService);
                revertedStatements.Reverse();

                if (firstExceptionEncountered != null &&
                    _undeployRethrowPolicy ==
                    ConfigurationEngineDefaults.UndeployRethrowPolicy.RETHROW_FIRST)
                {
                    throw firstExceptionEncountered;
                }
            }

            return(new UndeploymentResult(info.DeploymentId, revertedStatements));
        }
        private DeploymentResult DeployInternal(Module module, DeploymentOptions options, String deploymentId, DateTimeOffset addedDate)
        {
            if (options == null)
            {
                options = new DeploymentOptions();
            }

            if (Log.IsDebugEnabled)
            {
                Log.Debug("Deploying module " + module);
            }
            IList <String> imports;

            if (module.Imports != null)
            {
                foreach (var imported in module.Imports)
                {
                    if (Log.IsDebugEnabled)
                    {
                        Log.Debug("Adding import " + imported);
                    }
                    _epService.Configuration.AddImport(imported);
                }
                imports = new List <String>(module.Imports);
            }
            else
            {
                imports = Collections.GetEmptyList <string>();
            }

            if (options.IsCompile)
            {
                var itemExceptions = new List <DeploymentItemException>();
                foreach (var item in module.Items)
                {
                    if (item.IsCommentOnly)
                    {
                        continue;
                    }

                    try {
                        _epService.CompileEPL(item.Expression);
                    }
                    catch (Exception ex) {
                        itemExceptions.Add(new DeploymentItemException(ex.Message, item.Expression, ex, item.LineNumber));
                    }
                }

                if (itemExceptions.IsNotEmpty())
                {
                    throw BuildException("Compilation failed", module, itemExceptions);
                }
            }

            if (options.IsCompileOnly)
            {
                return(null);
            }

            var exceptions           = new List <DeploymentItemException>();
            var statementNames       = new List <DeploymentInformationItem>();
            var statements           = new List <EPStatement>();
            var eventTypesReferenced = new HashSet <String>();

            foreach (var item in module.Items)
            {
                if (item.IsCommentOnly)
                {
                    continue;
                }

                String statementName = null;
                Object userObject    = null;
                if (options.StatementNameResolver != null || options.StatementUserObjectResolver != null)
                {
                    var ctx = new StatementDeploymentContext(item.Expression, module, item, deploymentId);
                    statementName = options.StatementNameResolver != null?options.StatementNameResolver.GetStatementName(ctx) : null;

                    userObject = options.StatementUserObjectResolver != null?options.StatementUserObjectResolver.GetUserObject(ctx) : null;
                }

                try {
                    EPStatement stmt;
                    if (_optionalStatementIdGenerator == null)
                    {
                        if (options.IsolatedServiceProvider == null)
                        {
                            stmt = _epService.CreateEPL(item.Expression, statementName, userObject);
                        }
                        else
                        {
                            var unit = _statementIsolationService.GetIsolationUnit(options.IsolatedServiceProvider, -1);
                            stmt = unit.EPAdministrator.CreateEPL(item.Expression, statementName, userObject);
                        }
                    }
                    else
                    {
                        var statementId = _optionalStatementIdGenerator.Invoke();
                        if (options.IsolatedServiceProvider == null)
                        {
                            stmt = _epService.CreateEPLStatementId(item.Expression, statementName, userObject, statementId);
                        }
                        else
                        {
                            var unit = _statementIsolationService.GetIsolationUnit(options.IsolatedServiceProvider, -1);
                            var spi  = (EPAdministratorIsolatedSPI)unit.EPAdministrator;
                            stmt = spi.CreateEPLStatementId(item.Expression, statementName, userObject, statementId);
                        }
                    }
                    statementNames.Add(new DeploymentInformationItem(stmt.Name, stmt.Text));
                    statements.Add(stmt);

                    ICollection <String> types = _statementEventTypeRef.GetTypesForStatementName(stmt.Name);
                    if (types != null)
                    {
                        eventTypesReferenced.AddAll(types);
                    }
                }
                catch (EPException ex) {
                    exceptions.Add(new DeploymentItemException(ex.Message, item.Expression, ex, item.LineNumber));
                    if (options.IsFailFast)
                    {
                        break;
                    }
                }
            }

            if (exceptions.IsNotEmpty())
            {
                if (options.IsRollbackOnFail)
                {
                    Log.Debug("Rolling back intermediate statements for deployment");
                    foreach (var stmt in statements)
                    {
                        try {
                            stmt.Dispose();
                        }
                        catch (Exception ex) {
                            Log.Debug("Failed to destroy created statement during rollback: " + ex.Message, ex);
                        }
                    }
                    EPLModuleUtil.UndeployTypes(eventTypesReferenced, _statementEventTypeRef, _eventAdapterService, _filterService);
                }
                var text = "Deployment failed";
                if (options.IsValidateOnly)
                {
                    text = "Validation failed";
                }
                throw BuildException(text, module, exceptions);
            }

            if (options.IsValidateOnly)
            {
                Log.Debug("Rolling back created statements for validate-only");
                foreach (var stmt in statements)
                {
                    try {
                        stmt.Dispose();
                    }
                    catch (Exception ex) {
                        Log.Debug("Failed to destroy created statement during rollback: " + ex.Message, ex);
                    }
                }
                EPLModuleUtil.UndeployTypes(eventTypesReferenced, _statementEventTypeRef, _eventAdapterService, _filterService);
                return(null);
            }

            var deploymentInfoArr = statementNames.ToArray();
            var desc = new DeploymentInformation(deploymentId, module, addedDate.TranslateTo(_timeZone), DateTime.Now, deploymentInfoArr, DeploymentState.DEPLOYED);

            _deploymentStateService.AddUpdateDeployment(desc);

            if (Log.IsDebugEnabled)
            {
                Log.Debug("Module " + module + " was successfully deployed.");
            }
            return(new DeploymentResult(desc.DeploymentId, statements.AsReadOnlyList(), imports));
        }