s12 cron 持久化存储。 只负责 .scheduled_tasks.json 的读写,不涉及调度逻辑。 只保存 durable=true 的任务。
| 17 | * 只保存 durable=true 的任务。 |
| 18 | */ |
| 19 | public class CronStore { |
| 20 | |
| 21 | private final File file; |
| 22 | |
| 23 | public CronStore(File workdir) { |
| 24 | this.file = new File(workdir, ".scheduled_tasks.json"); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * 保存所有 durable job 到文件。 |
| 29 | */ |
| 30 | public void save(List<CronJob> jobs) { |
| 31 | List<CronJob> durable = new ArrayList<>(); |
| 32 | for (CronJob j : jobs) { |
| 33 | if (j.isDurable()) { |
| 34 | durable.add(j); |
| 35 | } |
| 36 | } |
| 37 | try { |
| 38 | Files.writeString(file.toPath(), |
| 39 | JSON.toJSONString(durable, true), |
| 40 | StandardCharsets.UTF_8); |
| 41 | } catch (IOException e) { |
| 42 | System.err.println(" [cron] failed to save durable jobs: " + e.getMessage()); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * 从文件加载所有 durable job。 |
| 48 | */ |
| 49 | public List<CronJob> load() { |
| 50 | List<CronJob> jobs = new ArrayList<>(); |
| 51 | if (!file.exists()) { |
| 52 | return jobs; |
| 53 | } |
| 54 | try { |
| 55 | String content = Files.readString(file.toPath(), StandardCharsets.UTF_8); |
| 56 | JSONArray arr = JSON.parseArray(content); |
| 57 | if (arr != null) { |
| 58 | for (int i = 0; i < arr.size(); i++) { |
| 59 | CronJob job = arr.getObject(i, CronJob.class); |
| 60 | if (job.getId() != null && job.getCron() != null) { |
| 61 | jobs.add(job); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } catch (Exception e) { |
| 66 | System.err.println(" [cron] failed to load durable jobs: " + e.getMessage()); |
| 67 | } |
| 68 | return jobs; |
| 69 | } |
| 70 | } |
nothing calls this directly
no outgoing calls
no test coverage detected