Skip to content

Commit

Permalink
Issue 16795 improve savepublish 2 (#16819)
Browse files Browse the repository at this point in the history
* #16252 format source. Introduce format checker on build. Alter style … (#16288)

* #16252 format source. Introduce format checker on build. Alter style files

* #16252 remove unwanted file

* #16252 remove unwanted file

* Updating commit reference for src/main/enterprise

* try different location for checkstyle.xml

* Revert "#16252 format source. Introduce format checker on build. Alter style … (#16288)" (#16291)

This reverts commit 05f9082.

* Updating commit reference for src/main/enterprise

* #16795 adding new stuff to avoid reindex on some api call and WF

* #16795 Minor refactoring

* #16795 Adding the support for include all dependencies

* #16795 ignoring some logs

* #16795 moving code to a constants

* #16795 coded the feedback
  • Loading branch information
jdotcms authored and jgambarios committed Jul 19, 2019
1 parent ff66cbf commit c6d21e5
Show file tree
Hide file tree
Showing 10 changed files with 262 additions and 159 deletions.
Expand Up @@ -441,6 +441,9 @@ public void addContentToIndex(final Contentlet parentContenlet, final boolean in
if (null == parentContenlet || !UtilMethods.isSet(parentContenlet.getIdentifier())) {
return;
}

Logger.info(this, "Indexing: " + parentContenlet.getIdentifier() + ", includeDependencies: " + includeDependencies);

// parentContenlet.setIndexPolicy(IndexPolicy.WAIT_FOR);
final List<Contentlet> contentToIndex =
(includeDependencies) ? ImmutableList.<Contentlet>builder().add(parentContenlet).addAll(loadDeps(parentContenlet)).build()
Expand Down

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions dotCMS/src/main/java/com/dotcms/util/LogTime.java
@@ -1,7 +1,5 @@
package com.dotcms.util;

import org.apache.log4j.Level;

import java.lang.annotation.*;

/**
Expand Down
30 changes: 30 additions & 0 deletions dotCMS/src/main/java/com/dotcms/util/ThreadContext.java
@@ -0,0 +1,30 @@
package com.dotcms.util;

/**
* Encapsulates Thread Local context information
* @author jsanca
*/
public class ThreadContext {

// by default the api will reindex, set to false if do not want to reindex in an api call.
private boolean reindex = true;

// when the reindex happens later, the api call can set this to true in order to tell at the end of the thread process to do reindex including dependencies
private boolean includeDependencies = false;

public boolean isReindex() {
return reindex;
}

public void setReindex(boolean reindex) {
this.reindex = reindex;
}

public boolean isIncludeDependencies() {
return includeDependencies;
}

public void setIncludeDependencies(boolean includeDependencies) {
this.includeDependencies = includeDependencies;
}
}
118 changes: 118 additions & 0 deletions dotCMS/src/main/java/com/dotcms/util/ThreadContextUtil.java
@@ -0,0 +1,118 @@
package com.dotcms.util;

import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotRuntimeException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.util.UtilMethods;

/**
* Thread Context Util provides methods to handle reindex stuff and other thread local context things
* @author jsancas
*/
public class ThreadContextUtil {

private static ThreadLocal<ThreadContext> contextLocal = new ThreadLocal<>();

/**
* Get the context from the current thread
* @return {@link ThreadContext}
*/
public static ThreadContext getOrCreateContext() {

return UtilMethods.get(contextLocal.get(), ()-> {

final ThreadContext context = new ThreadContext();
contextLocal.set(context);
return context;
});
}


/**
* Return true if the current thread is config to reindex things in the api calls, otherwise false.
* @return Boolean
*/
public static boolean isReindex () {

final ThreadContext context = getOrCreateContext();
return context.isReindex();
}

/**
* Executes the delegate if the reindex is set to true for the current thread
* @param delegate
* @throws DotSecurityException
* @throws DotDataException
*/
public static void ifReindex (final VoidDelegate delegate) throws DotSecurityException, DotDataException {

if (isReindex()) {

delegate.execute();
}
}

/**
* Executes the delegate if the reindex is set to true for the current thread
* @param delegate
* @throws DotSecurityException
* @throws DotDataException
*/
public static void ifReindex (final VoidDelegate delegate, final boolean includeDependencies) throws DotSecurityException, DotDataException {

if (isReindex()) {

delegate.execute();
} else {

getOrCreateContext().setIncludeDependencies(includeDependencies);
}
}

/**
* Wrap a void method into not reindex call
* @param delegate {@link VoidDelegate}
* @throws Exception
*/
public static void wrapVoidNoReindex (final VoidDelegate delegate) {

final ThreadContext threadContext = getOrCreateContext();
final boolean reindex = threadContext.isReindex();

try {

threadContext.setReindex(false);
delegate.execute();
} catch(Throwable e) {

throw new DotRuntimeException(e);
} finally {

threadContext.setReindex(reindex);
}
}


/**
* Wrap a return method into not reindex call
* @param delegate {@link VoidDelegate}
* @throws Exception
*/
public static <T> T wrapReturnNoReindex (final ReturnableDelegate<T> delegate) {

final ThreadContext threadContext = getOrCreateContext();
final boolean reindex = threadContext.isReindex();

try {

threadContext.setReindex(false);
return delegate.execute();
} catch(Throwable e) {

throw new DotRuntimeException(e);
} finally {

threadContext.setReindex(reindex);
}
}
}
1 change: 0 additions & 1 deletion dotCMS/src/main/java/com/dotcms/util/TimeUtil.java
@@ -1,6 +1,5 @@
package com.dotcms.util;

import com.dotmarketing.util.Config;
import com.dotmarketing.util.Logger;

import java.util.function.Supplier;
Expand Down
Expand Up @@ -129,6 +129,8 @@ public List<ReindexEntry> getFailedReindexRecords() throws DotDataException {
@Override
public void addIdentifierReindex(final String id) throws DotDataException {

Logger.info(this, "addIdentifierReindex: " + id);

this.reindexQueueFactory.addIdentifierReindex(id);
}

Expand Down
Expand Up @@ -2014,9 +2014,12 @@ public void fireWorkflowPostCheckin(final WorkflowProcessor processor) throws Do
this.saveWorkflowTask(processor);

if (UtilMethods.isSet(processor.getContentlet()) && processor.getContentlet().needsReindex()) {
Contentlet content = processor.getContentlet();

final Contentlet content = processor.getContentlet();
content.setIndexPolicy(IndexPolicy.WAIT_FOR);
this.contentletIndexAPI.addContentToIndex(content, false);
final ThreadContext threadContext = ThreadContextUtil.getOrCreateContext();
final boolean includeDependencies = null != threadContext && threadContext.isIncludeDependencies();
this.contentletIndexAPI.addContentToIndex(content, includeDependencies);
}
}
} catch(Exception e) {
Expand Down Expand Up @@ -2694,10 +2697,16 @@ public Contentlet fireContentWorkflow(final Contentlet contentlet, final Content
this.validateActionStepAndWorkflow(contentlet, dependencies.getModUser());
this.checkShorties (contentlet);

final WorkflowProcessor processor = this.fireWorkflowPreCheckin(contentlet, dependencies.getModUser());
final WorkflowProcessor processor = ThreadContextUtil.wrapReturnNoReindex(()-> this.fireWorkflowPreCheckin(contentlet, dependencies.getModUser()));

processor.setContentletDependencies(dependencies);
this.fireWorkflowPostCheckin(processor);
processor.getContentlet().setProperty(Contentlet.WORKFLOW_IN_PROGRESS, Boolean.TRUE);

ThreadContextUtil.wrapVoidNoReindex(() -> this.fireWorkflowPostCheckin(processor));

if (null != processor.getContentlet()) {
processor.getContentlet().setProperty(Contentlet.WORKFLOW_IN_PROGRESS, Boolean.FALSE);
}

return processor.getContentlet();
} // fireContentWorkflow
Expand Down
Expand Up @@ -156,7 +156,7 @@ public void execute(JobExecutionContext jobContext) throws JobExecutionException
/**
* Setup the job and trigger it immediately
*
* @param ContentType {@link ContentType}
* @param type {@link ContentType}
* @param user {@link User}
*/
Expand Down
13 changes: 13 additions & 0 deletions dotCMS/src/main/java/com/dotmarketing/util/UtilMethods.java
Expand Up @@ -55,6 +55,7 @@
import java.util.Random;
import java.util.StringTokenizer;
import java.util.TimeZone;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
Expand Down Expand Up @@ -311,6 +312,18 @@ public static final boolean isSet(String x) {

}

/**
* If argument instance is set, returns it otherwise will invoke the supplier to get the default value
* @param instance
* @param supplier
* @param <T>
* @return T
*/
public static <T> T get(final T instance, final Supplier<T> supplier) {

return isSet(instance)? instance : supplier.get();
}

public static final boolean isSet(java.util.Date x) {
return ((x != null) && (x.getTime() > 0));
}
Expand Down

0 comments on commit c6d21e5

Please sign in to comment.