@@ -147,7 +147,6 @@ class ProcessIndex(recipeInstanceIdleTimeout: Option[FiniteDuration],
147
147
private val restartRandomFactor : Double = config.getDouble(" baker.process-instance.restart-randomFactor" )
148
148
149
149
private val index : mutable.Map [String , ActorMetadata ] = mutable.Map [String , ActorMetadata ]()
150
- private val recipeCache : mutable.Map [String , (CompiledRecipe , Long )] = mutable.Map [String , (CompiledRecipe , Long )]()
151
150
152
151
// TODO chose if to use the CassandraBakerCleanup or the ActorBasedBakerCleanup
153
152
private val cleanup : BakerCleanup = {
@@ -168,41 +167,37 @@ class ProcessIndex(recipeInstanceIdleTimeout: Option[FiniteDuration],
168
167
}
169
168
170
169
// TODO this is a synchronous ask on an actor which createProcessActor is considered bad practice, alternative?
171
- private def getRecipeIdFromManager (recipeId : String ): Option [(CompiledRecipe , Long )] = {
172
- log.debug(" Adding recipe to recipe cache: {}" , recipeId)
173
- val futureResult : Future [Option [RecipeRecord ]] = recipeManager.get(recipeId)
174
- val result = Await .result(futureResult, updateCacheTimeout)
175
- recipeCache ++= result.map(r => r.recipeId -> (r.recipe, r.updated))
176
- log.debug(" Added recipe to recipe cache: {}, current size: {}" , recipeId, recipeCache.size)
177
- result.map(r => r.recipe -> r.updated)
170
+ private def getRecipeRecord (recipeId : String , reactivate : Boolean ): Option [RecipeRecord ] = {
171
+ val eventualRecord = recipeManager.get(recipeId).flatMap {
172
+ case Some (recipeRecord) if recipeRecord.isActive => Future .successful(Some (recipeRecord))
173
+ case Some (recipeRecord) if ! reactivate => Future .successful(Some (recipeRecord)) // inactive recipe, but reactivation is not needed
174
+ case Some (recipeRecord) if reactivate => // inactive recipe, reactivate it
175
+ log.info(s " Inactive recipe $recipeId being reactivated. " )
176
+ val activeRecipe = recipeRecord.copy(isActive = true )
177
+ recipeManager.put(activeRecipe)
178
+ Future .successful(Some (activeRecipe))
179
+ case None => Future .successful(None )
180
+ }
181
+ Await .result(eventualRecord, updateCacheTimeout)
178
182
}
179
183
180
184
def getRecipeIdFromActor (actorRef : ActorRef ) : String = actorRef.path.name
181
185
182
- def getRecipeWithTimeStamp (recipeId : String ): Option [(CompiledRecipe , Long )] =
183
- recipeCache.get(recipeId) match {
184
- case None =>
185
- getRecipeIdFromManager(recipeId)
186
- case other => other
187
- }
188
-
189
- def getCompiledRecipe (recipeId : String ): Option [CompiledRecipe ] =
190
- getRecipeWithTimeStamp(recipeId).fold[Option [CompiledRecipe ]] {
186
+ private def getCompiledRecipe (recipeId : String , reactivate : Boolean = true ): Option [CompiledRecipe ] = {
187
+ getRecipeRecord(recipeId, reactivate).fold[Option [CompiledRecipe ]] {
191
188
log.warning(s " No recipe found for $recipeId" )
192
189
None
193
190
} {
194
- case (recipe, _) => Some (recipe)
191
+ case r : RecipeRecord => Some (r. recipe)
195
192
case _ => None
196
193
}
197
-
198
- def getOrCreateProcessActor (recipeInstanceId : String ): Option [ActorRef ] =
199
- context.child(recipeInstanceId).orElse(createProcessActor(recipeInstanceId))
194
+ }
200
195
201
196
def getProcessActor (recipeInstanceId : String ): Option [ActorRef ] =
202
197
context.child(recipeInstanceId)
203
198
204
- def createProcessActor (recipeInstanceId : String ): Option [ActorRef ] =
205
- getCompiledRecipe(index(recipeInstanceId).recipeId).map(createProcessActor(recipeInstanceId, _))
199
+ private def createProcessActor (recipeInstanceId : String , reactivateRecipe : Boolean = true ): Option [ActorRef ] =
200
+ getCompiledRecipe(index(recipeInstanceId).recipeId, reactivateRecipe ).map(createProcessActor(recipeInstanceId, _))
206
201
207
202
// creates a ProcessInstanceActor, does not do any validation
208
203
def createProcessActor (recipeInstanceId : String , compiledRecipe : CompiledRecipe ): ActorRef = {
@@ -238,7 +233,7 @@ class ProcessIndex(recipeInstanceIdleTimeout: Option[FiniteDuration],
238
233
239
234
def shouldDelete (meta : ActorMetadata ): Boolean = {
240
235
if (meta.processStatus != Deleted )
241
- getCompiledRecipe(meta.recipeId) match {
236
+ getCompiledRecipe(meta.recipeId, reactivate = false ) match {
242
237
case Some (recipe) =>
243
238
recipe.retentionPeriod.exists { p => meta.createdDateTime + p.toMillis < System .currentTimeMillis() }
244
239
case None =>
@@ -257,7 +252,7 @@ class ProcessIndex(recipeInstanceIdleTimeout: Option[FiniteDuration],
257
252
actorRef ! Stop (delete = true )
258
253
case None =>
259
254
log.debug(s " Deleting ${meta.recipeInstanceId} via cleanup tool " )
260
- getCompiledRecipe(meta.recipeId) match {
255
+ getCompiledRecipe(meta.recipeId, reactivate = false ) match {
261
256
case Some (compiledRecipe) =>
262
257
val persistenceId = ProcessInstance .recipeInstanceId2PersistenceId(compiledRecipe.name, meta.recipeInstanceId)
263
258
log.debug(s " Deleting with persistenceId: ${persistenceId}" )
@@ -284,6 +279,10 @@ class ProcessIndex(recipeInstanceIdleTimeout: Option[FiniteDuration],
284
279
}
285
280
}
286
281
282
+ // This util function is used only for delete process functionality, therefore passing reactivateRecipe=false to avoid reactivating the recipe
283
+ private def getOrCreateProcessActor (recipeInstanceId : String ): Option [ActorRef ] =
284
+ context.child(recipeInstanceId).orElse(createProcessActor(recipeInstanceId, reactivateRecipe = false ))
285
+
287
286
private def forgetProcesses (): Unit = {
288
287
rememberProcessDuration.map {
289
288
duration : Duration =>
@@ -520,8 +519,8 @@ class ProcessIndex(recipeInstanceIdleTimeout: Option[FiniteDuration],
520
519
index.get(recipeInstanceId) match {
521
520
case Some (processMetadata) if processMetadata.isDeleted => sender() ! ProcessDeleted (recipeInstanceId)
522
521
case Some (processMetadata) =>
523
- getRecipeWithTimeStamp (processMetadata.recipeId) match {
524
- case Some ((compiledRecipe, timestamp )) => sender() ! RecipeFound (compiledRecipe, timestamp )
522
+ getRecipeRecord (processMetadata.recipeId, reactivate = true ) match {
523
+ case Some (RecipeRecord (_, _, updated, recipe, _, _ )) => sender() ! RecipeFound (recipe, updated )
525
524
case None => sender() ! NoSuchProcess (recipeInstanceId)
526
525
}
527
526
case None => sender() ! NoSuchProcess (recipeInstanceId)
0 commit comments