Example #1
0
  public static Object call(final String key, final Map<String, Object> parameters)
      throws FrameworkException {

    final SecurityContext superUserContext = SecurityContext.getSuperUserInstance();
    final App app = StructrApp.getInstance(superUserContext);

    // we might want to introduce caching here at some point in the future..
    // Cache can be invalidated when the schema is rebuilt for example..

    final List<SchemaMethod> methods = app.nodeQuery(SchemaMethod.class).andName(key).getAsList();
    if (methods.isEmpty()) {

      logger.debug("Tried to call method {} but no SchemaMethod entity was found.", key);

    } else {

      for (final SchemaMethod method : methods) {

        // only call methods that are NOT part of a schema node
        final AbstractSchemaNode entity = method.getProperty(SchemaMethod.schemaNode);
        if (entity == null) {

          final String source = method.getProperty(SchemaMethod.source);
          if (source != null) {

            return Actions.execute(superUserContext, null, "${" + source + "}", parameters);

          } else {

            logger.warn("Schema method {} has no source code, will NOT be executed.", key);
          }

        } else {

          logger.warn("Schema method {} is attached to an entity, will NOT be executed.", key);
        }
      }
    }

    return null;
  }
Example #2
0
  @Override
  public void run() {

    final Services servicesInstance = Services.getInstance();

    // wait for service layer to be initialized
    while (!servicesInstance.isInitialized()) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException iex) {
      }
    }

    // sleep 5 seconds more
    try {
      Thread.sleep(5000);
    } catch (InterruptedException iex) {
    }

    while (doRun) {

      // sleep for some time
      try {
        Thread.sleep(GRANULARITY_UNIT.toMillis(GRANULARITY));
      } catch (InterruptedException iex) {
      }

      for (CronEntry entry : cronEntries) {

        if (entry.getDelayToNextExecutionInMillis() < GRANULARITY_UNIT.toMillis(GRANULARITY)) {

          final String taskClassName = entry.getName();
          final Class taskClass = instantiate(taskClassName);

          try {

            if (taskClass != null) {

              Task task = (Task) taskClass.newInstance();

              logger.debug("Starting task {}", taskClassName);
              StructrApp.getInstance().processTasks(task);

            } else {

              try (final Tx tx = StructrApp.getInstance().tx()) {

                // check for schema method with the given name
                Actions.call(taskClassName, Collections.EMPTY_MAP);

                tx.success();
              }
            }

          } catch (Throwable t) {
            logger.warn(
                "Exception while executing cron task {}: {}", taskClassName, t.getMessage());
          }
        }
      }
    }
  }