001/*
002 * (C) Copyright 2012-2018 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.work;
020
021import java.util.UUID;
022import java.util.concurrent.atomic.AtomicInteger;
023
024import org.apache.logging.log4j.LogManager;
025import org.apache.logging.log4j.Logger;
026
027/**
028 * Simple work that just sleeps, mostly used for tests.
029 */
030public class SleepWork extends AbstractWork {
031    
032    private static final Logger log = LogManager.getLogger(SleepWork.class);
033
034    public static final String CATEGORY = "SleepWork";
035
036    private static final long serialVersionUID = 1L;
037
038    protected long durationMillis;
039
040    protected String category;
041
042    protected AtomicInteger count = new AtomicInteger();
043
044    protected String partitionKey;
045
046    protected boolean idempotent = true;
047
048    protected boolean coalescing = false;
049
050    /**
051     * Creates a work instance that does nothing but sleep.
052     *
053     * @param durationMillis the sleep duration
054     * @since 10.2
055     */
056    public SleepWork(long durationMillis, String category, String id) {
057        super(id);
058        this.durationMillis = durationMillis;
059        this.category = category;
060        partitionKey = String.valueOf(count.incrementAndGet());
061        setProgress(Progress.PROGRESS_0_PC);
062    }
063
064    /**
065     * @since 10.2
066     */
067    public SleepWork(long durationMillis, String id) {
068        this(durationMillis, CATEGORY, id);
069    }
070
071    /**
072     * @since 10.2
073     */
074    public SleepWork(long durationMillis) {
075        this(durationMillis, CATEGORY, UUID.randomUUID().toString());
076    }
077
078    /**
079     * If debug is true, then the various debug* methods must be called in the proper order for the work to start and
080     * stop: {@link #debugStart}, {@link #debugFinish}.
081     *
082     * @param durationMillis the sleep duration
083     * @param debug {@code true} for debug
084     * @deprecated since 10.2 debug flag is unused
085     */
086    @Deprecated
087    public SleepWork(long durationMillis, boolean debug) {
088        this(durationMillis, null, CATEGORY);
089    }
090
091    /**
092     * @deprecated since 10.2 debug flag is unused
093     */
094    @Deprecated
095    public SleepWork(long durationMillis, boolean debug, String id) {
096        this(durationMillis, null, id);
097    }
098
099    /**
100     * @deprecated since 10.2 debug flag is unused
101     */
102    @Deprecated
103    public SleepWork(long durationMillis, String category, boolean debug) {
104        this(durationMillis, category, CATEGORY);
105    }
106
107    /**
108     * @deprecated since 10.2 debug flag is unused
109     */
110    @Deprecated
111    public SleepWork(long durationMillis, String category, boolean debug, String id) {
112        this(durationMillis, category, id);
113    }
114
115    @Override
116    public String getCategory() {
117        return category;
118    }
119
120    @Override
121    public String getTitle() {
122        return "Sleep " + durationMillis + " ms";
123    }
124
125    @Override
126    public void work() {
127        log.debug("Running " + this);
128        try {
129            doWork();
130        } catch (InterruptedException e) {
131            Thread.currentThread().interrupt();
132            throw new RuntimeException(e);
133        }
134    }
135
136    protected void doWork() throws InterruptedException {
137        for (;;) {
138            long elapsed = System.currentTimeMillis() - getStartTime();
139            if (elapsed > durationMillis) {
140                break;
141            }
142            setProgress(new Progress(100F * elapsed / durationMillis));
143
144            if (isSuspending()) {
145                durationMillis -= elapsed; // save state
146                suspended();
147                return;
148            }
149
150            if (WorkStateHelper.isCanceled(getId())) {
151                durationMillis -= elapsed; // save state
152                return;
153            }
154
155            Thread.sleep(10);
156        }
157
158    }
159
160    @Override
161    public String getPartitionKey() {
162        return partitionKey;
163    }
164
165    @Override
166    public boolean isIdempotent() {
167        return idempotent;
168    }
169
170    public void setIdempotent(boolean idempotent) {
171        this.idempotent = idempotent;
172    }
173
174    @Override
175    public boolean isCoalescing() {
176        return coalescing;
177    }
178
179    public void setCoalescing(boolean coalescing) {
180        this.coalescing = coalescing;
181    }
182
183    @Override
184    public String toString() {
185        return getClass().getSimpleName() + "(" + (getId().length() > 10 ? "" : getId() + ", ") + durationMillis
186                + "ms, " + getProgress() + ")";
187    }
188
189}