All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cedarsoftware.ncube.PreCompiler.groovy Maven / Gradle / Ivy

There is a newer version: 5.6.9
Show newest version
package com.cedarsoftware.ncube

import com.cedarsoftware.ncube.rules.RulesConfiguration
import com.cedarsoftware.ncube.rules.RulesEngine
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import org.springframework.cache.Cache
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
import org.springframework.context.ConfigurableApplicationContext

import static com.cedarsoftware.ncube.ApplicationID.DEFAULT_TENANT
import static com.cedarsoftware.ncube.ApplicationID.HEAD
import static com.cedarsoftware.ncube.NCubeAppContext.ncubeRuntime
import static com.cedarsoftware.ncube.NCubeConstants.SEARCH_ACTIVE_RECORDS_ONLY
import static com.cedarsoftware.ncube.NCubeConstants.SEARCH_INCLUDE_CUBE_DATA
import static com.cedarsoftware.ncube.ReleaseStatus.RELEASE
import static com.cedarsoftware.ncube.ReleaseStatus.SNAPSHOT
import static com.cedarsoftware.ncube.rules.RulesConfiguration.APP_BRANCH
import static com.cedarsoftware.ncube.rules.RulesConfiguration.APP_NAME
import static com.cedarsoftware.ncube.rules.RulesConfiguration.APP_STATUS
import static com.cedarsoftware.ncube.rules.RulesConfiguration.APP_TENANT
import static com.cedarsoftware.ncube.rules.RulesConfiguration.APP_VERSION
import static com.cedarsoftware.util.StringUtilities.hasContent

/**
 * @author John DeRegnaucourt ([email protected]), Josh Snyder ([email protected])
 *         
* Copyright (c) Cedar Software LLC *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* http://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitatœions under the License. */ @Slf4j @CompileStatic class PreCompiler implements ApplicationContextAware { private ApplicationContext ctx private final RulesConfiguration rulesConfiguration private final List> precompiledAppIds PreCompiler(RulesConfiguration rulesConfiguration, List> precompiledAppIds) { this.rulesConfiguration = rulesConfiguration this.precompiledAppIds = precompiledAppIds } /** * Compiles all NCube ApplicationIDs for all configured RulesEngines as well as any configured under the * {@code ncube.performance.precompiledAppIds} property.
*
* Example to pick latest RELEASE version from HEAD branch: *
     * ncube:
     *   performance:
     *     precompiledAppIds:
     *       - app: MyAppName
     *         version: LATEST
     * 
* * Example to pick specific version: *
     * ncube:
     *   performance:
     *     precompiledAppIds:
     *       - app: MyAppName
     *         version: 1.0.0
     *         status: RELEASE
     *         branch: HEAD
     * 
* @see com.cedarsoftware.config.NCubeConfiguration */ void compile() { Closure closure = { precompile() } Thread t = new Thread(closure) t.name = 'Ncube.PreCompiler' t.daemon = true t.start() } protected void precompile() { long start = System.nanoTime() Set appIds = new LinkedHashSet<>() Map rulesEngines = rulesConfiguration.rulesEngines for (RulesEngine rulesEngine : rulesEngines.values()) { appIds.add(rulesEngine.appId) } appIds.addAll(configuredAppIds) boolean hasCompileErrors = false Map searchOptions = [(SEARCH_ACTIVE_RECORDS_ONLY): true, (SEARCH_INCLUDE_CUBE_DATA): true] as Map // compile all NCube cells for (ApplicationID appId : appIds) { log.info("Compiling NCubes for appId: ${appId}") Cache cache = ncubeRuntime.getCacheForApp(appId) List dtos = ncubeRuntime.search(appId, '*', null, searchOptions) for (NCubeInfoDto dto : dtos) { NCube ncube = cache.get(dto.name, NCube.class) if (ncube == null) { NCube ncubeFromBytes = NCube.createCubeFromBytes(dto.bytes) ncubeFromBytes.applicationID = dto.applicationID hasCompileErrors = checkCompileErrors(ncubeFromBytes, hasCompileErrors) ncubeRuntime.addCube(ncubeFromBytes, false) } else { hasCompileErrors = checkCompileErrors(ncube, hasCompileErrors) } } } long stop = System.nanoTime() long total = Math.round((stop - start) / 1000000.0d) if (hasCompileErrors) { log.error('Error compiling NCubes. See previous logs.') ((ConfigurableApplicationContext) ctx).close() } else { log.info("Compiled all NCubes in: ${total} ms") } } private static boolean checkCompileErrors(NCube ncube, boolean hasCompileErrors) { CompileInfo compileInfo = ncube.compile() if (hasCompileErrors) { return true } else { return !compileInfo.getExceptions().empty } } private Set getConfiguredAppIds() { Set appIds = new HashSet<>() for (Map precompiledAppId : precompiledAppIds) { String tenant = precompiledAppId[APP_TENANT] ?: DEFAULT_TENANT String app = precompiledAppId[APP_NAME] if (!hasContent(app)) { log.warn("Missing 'app' property in ncube.performance.precompileApps: ${precompiledAppId}.") continue } String status = precompiledAppId[APP_STATUS] ?: RELEASE.name() if (![RELEASE.name(), SNAPSHOT.name()].contains(status)) { log.warn("Illegal 'status' property in ncube.performance.precompileApps: ${precompiledAppId}. Must be SNAPSHOT or RELEASE.") continue } String version = precompiledAppId[APP_VERSION] if (!hasContent(version)) { log.warn("Missing 'version' property in ncube.performance.precompileApps: ${precompiledAppId}.") continue } if ('latest' == version?.toLowerCase()) { Object[] versions = ncubeRuntime.getVersions(app) for (Object ver : versions) { String versionStatus = (String) ver if (versionStatus.endsWith(RELEASE.name())) { version = versionStatus - '-RELEASE' break } } } String branch = precompiledAppId[APP_BRANCH] ?: HEAD ApplicationID appId = new ApplicationID(tenant, app, version, status, branch) appIds.add(appId) } return appIds } void setApplicationContext(ApplicationContext applicationContext) { ctx = applicationContext } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy