1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import java.util.List;
|
---|
5 | import java.util.concurrent.Executors;
|
---|
6 | import java.util.concurrent.ScheduledExecutorService;
|
---|
7 | import java.util.concurrent.ScheduledFuture;
|
---|
8 | import java.util.concurrent.TimeUnit;
|
---|
9 | import java.util.stream.Collectors;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
12 | import org.openstreetmap.josm.data.osm.ChangesetCache;
|
---|
13 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
14 | import org.openstreetmap.josm.tools.Logging;
|
---|
15 | import org.openstreetmap.josm.tools.Utils;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Checks periodically if open changesets have been closed on server side.
|
---|
19 | * @since 14326
|
---|
20 | */
|
---|
21 | public final class ChangesetUpdater {
|
---|
22 |
|
---|
23 | private ChangesetUpdater() {
|
---|
24 | // Hide default constructor for utils classes
|
---|
25 | }
|
---|
26 |
|
---|
27 | /** Property defining the update interval in minutes */
|
---|
28 | public static final IntegerProperty PROP_INTERVAL = new IntegerProperty("changeset.updater.interval", 60);
|
---|
29 |
|
---|
30 | private static final ScheduledExecutorService EXECUTOR =
|
---|
31 | Executors.newSingleThreadScheduledExecutor(Utils.newThreadFactory("changeset-updater-%d", Thread.NORM_PRIORITY));
|
---|
32 |
|
---|
33 | private static final Runnable WORKER = new Worker();
|
---|
34 |
|
---|
35 | private static volatile ScheduledFuture<?> task;
|
---|
36 |
|
---|
37 | private static class Worker implements Runnable {
|
---|
38 |
|
---|
39 | private long lastTimeInMillis;
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public void run() {
|
---|
43 | long currentTime = System.currentTimeMillis();
|
---|
44 | // See #14671 - Make sure we don't run the API call many times after system wakeup
|
---|
45 | if (currentTime >= lastTimeInMillis + TimeUnit.MINUTES.toMillis(PROP_INTERVAL.get())) {
|
---|
46 | lastTimeInMillis = currentTime;
|
---|
47 | check();
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Checks for open changesets that have been closed on server side, and update Changeset cache if needed.
|
---|
54 | */
|
---|
55 | public static void check() {
|
---|
56 | long now = System.currentTimeMillis();
|
---|
57 | List<Long> changesetIds = ChangesetCache.getInstance().getOpenChangesets().stream()
|
---|
58 | .filter(x -> x.getCreatedAt() != null
|
---|
59 | && now - x.getCreatedAt().getTime() > TimeUnit.HOURS.toMillis(1))
|
---|
60 | .map(Changeset::getId)
|
---|
61 | .map(Integer::longValue)
|
---|
62 | .collect(Collectors.toList());
|
---|
63 | if (!changesetIds.isEmpty()) {
|
---|
64 | try {
|
---|
65 | ChangesetCache.getInstance().update(new OsmServerChangesetReader().queryChangesets(
|
---|
66 | new ChangesetQuery().forChangesetIds(changesetIds), null));
|
---|
67 | } catch (OsmTransferException e) {
|
---|
68 | Logging.warn(e);
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Starts the changeset updater task if not already started
|
---|
75 | */
|
---|
76 | public static void start() {
|
---|
77 | int interval = PROP_INTERVAL.get();
|
---|
78 | if (!isRunning() && interval > 0) {
|
---|
79 | task = EXECUTOR.scheduleAtFixedRate(WORKER, 0, interval, TimeUnit.MINUTES);
|
---|
80 | Logging.info("Changeset updater active (checks every "+interval+" minute"+(interval > 1 ? "s" : "") +
|
---|
81 | " if open changesets have been closed)");
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Stops the changeset updater task if started
|
---|
87 | */
|
---|
88 | public static void stop() {
|
---|
89 | if (isRunning()) {
|
---|
90 | task.cancel(false);
|
---|
91 | Logging.info("Changeset updater inactive");
|
---|
92 | task = null;
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Determines if the changeset updater is currently running
|
---|
98 | * @return {@code true} if the updater is running, {@code false} otherwise
|
---|
99 | */
|
---|
100 | public static boolean isRunning() {
|
---|
101 | return task != null;
|
---|
102 | }
|
---|
103 | }
|
---|