001/* 002 * (C) Copyright 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 * pierre 018 */ 019package org.nuxeo.ecm.core.work; 020 021import org.nuxeo.ecm.core.work.api.Work; 022import org.nuxeo.runtime.api.Framework; 023import org.nuxeo.runtime.kv.KeyValueService; 024import org.nuxeo.runtime.kv.KeyValueStore; 025 026/** 027 * Work state helper to handle, out-of-API, distributed, work states.<br> 028 * 029 * @since 10.2 030 */ 031public class WorkStateHelper { 032 033 protected static final String KV_NAME = "workManager"; 034 035 protected static final String STATE_SUFFIX = ":state"; 036 037 protected static final String OFFSET_SUFFIX = ":offset"; 038 039 protected static final String CANCELED = "canceled"; 040 041 protected static KeyValueStore getKeyValueStore() { 042 return Framework.getService(KeyValueService.class).getKeyValueStore(KV_NAME); 043 } 044 045 /** 046 * Returns the last offset created for a given work id. 047 * <p> 048 * 049 * @param workId id of the work whose we want the last offset 050 * @return the last offset or -1 for convenience 051 * @since 10.3 052 */ 053 protected static long getLastOffset(String workId) { 054 String stringOffset = getKeyValueStore().getString(getOffsetKey(workId)); 055 return stringOffset == null ? -1 : Long.parseLong(stringOffset); 056 } 057 058 protected static String getOffsetKey(String workId) { 059 return workId + OFFSET_SUFFIX; 060 } 061 062 protected static Work.State getState(String workId) { 063 String stringState = getKeyValueStore().getString(getStateKey(workId)); 064 return stringState == null || CANCELED.equals(stringState) ? null : Work.State.valueOf(stringState); 065 } 066 067 protected static String getStateKey(String workId) { 068 return workId + STATE_SUFFIX; 069 } 070 071 protected static boolean isCanceled(String workId) { 072 return CANCELED.equals(getKeyValueStore().getString(getStateKey(workId))); 073 } 074 075 protected static void setCanceled(String workId) { 076 getKeyValueStore().put(getStateKey(workId), CANCELED, 0); 077 } 078 079 protected static void setLastOffset(String workId, Long offset, long ttl) { 080 getKeyValueStore().put(getOffsetKey(workId), offset == null ? null : offset, ttl); 081 } 082 083 protected static void setState(String workId, Work.State state, long ttl) { 084 getKeyValueStore().put(getStateKey(workId), state == null ? null : state.toString(), ttl); 085 } 086 087 private WorkStateHelper() { 088 // hide constructor 089 } 090 091}