-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: remove group from task args and move user to datashare app
- Loading branch information
Showing
59 changed files
with
561 additions
and
385 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
datashare-app/src/main/java/org/icij/datashare/tasks/DatashareTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.icij.datashare.tasks; | ||
|
||
import java.util.HashMap; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import org.icij.datashare.asynctasks.Task; | ||
import org.icij.datashare.user.User; | ||
|
||
public class DatashareTask { | ||
public static final String USER_KEY = "user"; | ||
|
||
public static <V> Task<V> task(String name, User user, Map<String, Object> args) { | ||
return new Task<>(name, addTo(args, user)); | ||
} | ||
|
||
public static <V> Task<V> task(String id, String name, User user) { | ||
return new Task<>(id, name, addTo(new HashMap<>(), user)); | ||
} | ||
|
||
public static User getUser(Task<?> task) { | ||
return (User) task.args.get(USER_KEY); | ||
} | ||
|
||
private static Map<String, Object> addTo(Map<String, Object> properties, User user) { | ||
LinkedHashMap<String, Object> result = new LinkedHashMap<>(properties); | ||
result.put(USER_KEY, user); | ||
return result; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
datashare-app/src/main/java/org/icij/datashare/tasks/DatashareTaskManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.icij.datashare.tasks; | ||
|
||
import static java.util.stream.Collectors.toMap; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import org.icij.datashare.asynctasks.Group; | ||
import org.icij.datashare.asynctasks.Task; | ||
import org.icij.datashare.asynctasks.TaskGroup; | ||
import org.icij.datashare.asynctasks.TaskManager; | ||
import org.icij.datashare.user.User; | ||
|
||
public interface DatashareTaskManager extends TaskManager { | ||
// TODO: can we do better using generics instead of casts ? | ||
default String startTask(Class<?> taskClass, User user, Map<String, Object> properties) throws IOException { | ||
return startTask(DatashareTask.task(taskClass.getName(), user, properties), new Group(taskClass.getAnnotation( | ||
TaskGroup.class).value())); | ||
} | ||
|
||
default String startTask(String id, Class<?> taskClass, User user) throws IOException { | ||
return startTask(DatashareTask.task(id, taskClass.getName(), user), new Group(taskClass.getAnnotation(TaskGroup.class).value())); | ||
} | ||
|
||
default List<Task<?>> getTasks(Stream<Task<?>> stream, User user, Pattern pattern) { | ||
return getTasks(stream, pattern).stream().map( t -> ( Task<?> ) t) | ||
.filter(t -> user.equals(DatashareTask.getUser(t))).collect(Collectors.toList()); | ||
} | ||
|
||
default List<Task<?>> getTasks(User user, Pattern pattern) throws IOException { | ||
return getTasks(this.getTasks().stream(), user, pattern); | ||
} | ||
|
||
default Map<String, Boolean> stopAllTasks(User user) throws IOException { | ||
return getTasks().stream() | ||
.filter(t -> user.equals(DatashareTask.getUser(t))) | ||
.filter(t -> t.getState() == Task.State.RUNNING || t.getState() == Task.State.QUEUED).collect( | ||
toMap(t -> t.id, t -> { | ||
try { | ||
return stopTask(t.id); | ||
} catch (IOException e) { | ||
logger.error("cannot stop task {}", t.id, e); | ||
return false; | ||
} | ||
})); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.