summaryrefslogtreecommitdiffstats
path: root/toolkit/components/backgroundtasks/BackgroundTask_removeDirectory.sys.mjs
blob: 57faefb49c9f231d3a6eabeba67d58c5f28f6f37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
  setTimeout: "resource://gre/modules/Timer.sys.mjs",
});

import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";
import { EXIT_CODE } from "resource://gre/modules/BackgroundTasksManager.sys.mjs";

class Metrics {
  /**
   * @param {string} metricsId
   */
  constructor(metricsId) {
    this.metricsId = metricsId;
    this.startedTime = new Date();

    this.wasFirst = true;
    this.retryCount = 0;
    this.removalCountObj = { value: 0 };
    this.succeeded = true;

    this.suffixRemovalCountObj = { value: 0 };
    this.suffixEverFailed = false;
  }

  async report() {
    if (!this.metricsId) {
      console.warn(`Skipping Glean as no metrics id is passed`);
      return;
    }
    if (AppConstants.MOZ_APP_NAME !== "firefox") {
      console.warn(
        `Skipping Glean as the app is not Firefox: ${AppConstants.MOZ_APP_NAME}`
      );
      return;
    }

    const elapsedMs = new Date().valueOf() - this.startedTime.valueOf();

    // Note(krosylight): This FOG initialization happens within a unique
    // temporary directory created for each background task, which will
    // be removed after each run.
    // That means any failed submission will be lost, but we are fine with
    // that as we only have a single submission.
    Services.fog.initializeFOG(undefined, "firefox.desktop.background.tasks");

    const gleanMetrics = Glean[`backgroundTasksRmdir${this.metricsId}`];
    if (!gleanMetrics) {
      throw new Error(
        `The metrics id "${this.metricsId}" is not available in toolkit/components/backgroundtasks/metrics.yaml. ` +
          `Make sure that the id has no typo and is in PascalCase. ` +
          `Note that you can omit the id for testing.`
      );
    }

    gleanMetrics.elapsedMs.set(elapsedMs);
    gleanMetrics.wasFirst.set(this.wasFirst);
    gleanMetrics.retryCount.set(this.retryCount);
    gleanMetrics.removalCount.set(this.removalCountObj.value);
    gleanMetrics.succeeded.set(this.succeeded);
    gleanMetrics.suffixRemovalCount.set(this.suffixRemovalCountObj.value);
    gleanMetrics.suffixEverFailed.set(this.suffixEverFailed);

    GleanPings.backgroundTasks.submit();

    // XXX: We wait for arbitrary time for Glean to submit telemetry.
    // Bug 1790702 should add a better way.
    console.error("Pinged glean, waiting for submission.");
    await new Promise(resolve => lazy.setTimeout(resolve, 5000));
  }
}

// Recursively removes a directory.
// Returns true if it succeeds, false otherwise.
function tryRemoveDir(aFile, countObj) {
  try {
    aFile.remove(true, countObj);
  } catch (e) {
    return false;
  }

  return true;
}

const FILE_CHECK_ITERATION_TIMEOUT_MS = 1000;

function cleanupDirLockFile(aLock, aProfileName) {
  let lockFile = aLock.getLockFile(aProfileName);
  try {
    // Try to clean up the lock file
    lockFile.remove(false);
  } catch (ex) {}
}

async function deleteChildDirectory(
  parentDirPath,
  childDirName,
  secondsToWait,
  metrics
) {
  if (!childDirName || !childDirName.length) {
    return;
  }

  let targetFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
  targetFile.initWithPath(parentDirPath);
  targetFile.append(childDirName);

  // We create the lock before the file is actually there so this task
  // is the first one to acquire the lock. Otherwise a different task
  // could be cleaning suffixes and start deleting the folder while this
  // task is waiting for it to show up.
  let dirLock = Cc["@mozilla.org/net/CachePurgeLock;1"].createInstance(
    Ci.nsICachePurgeLock
  );

  let locked = false;
  try {
    dirLock.lock(childDirName);
    locked = true;
    metrics.wasFirst = !dirLock.isOtherInstanceRunning();
  } catch (e) {
    console.error("Failed to check dirLock");
  }

  if (!metrics.wasFirst) {
    if (locked) {
      dirLock.unlock();
      locked = false;
    }
    console.error("Another instance is already purging this directory");
    return;
  }

  // This backgroundtask process is spawned by the call to
  // PR_CreateProcessDetached in CacheFileIOManager::SyncRemoveAllCacheFiles
  // Only if spawning the process is successful is the cache folder renamed,
  // so we need to wait until that is done.
  while (!targetFile.exists()) {
    if (
      metrics.retryCount * FILE_CHECK_ITERATION_TIMEOUT_MS >
      secondsToWait * 1000
    ) {
      // We don't know for sure if the folder was renamed or if a different
      // task removed it already. The second variant is more likely but to
      // be sure we'd have to consult a log file, which introduces
      // more complexity.
      console.error(`couldn't find cache folder ${targetFile.path}`);
      if (locked) {
        dirLock.unlock();
        locked = false;
      }
      return;
    }
    await new Promise(resolve =>
      lazy.setTimeout(resolve, FILE_CHECK_ITERATION_TIMEOUT_MS)
    );
    metrics.retryCount++;
    console.error(`Cache folder attempt no ${metrics.retryCount}`);
  }

  if (!targetFile.isDirectory()) {
    if (locked) {
      dirLock.unlock();
      locked = false;
    }
    throw new Error("Path was not a directory");
  }

  console.error(`started removing ${targetFile.path}`);
  try {
    targetFile.remove(true, metrics.removalCountObj);
  } catch (err) {
    console.error(
      `failed removing ${targetFile.path}. removed ${metrics.removalCountObj.value} entries.`
    );
    throw err;
  } finally {
    console.error(
      `done removing ${targetFile.path}. removed ${metrics.removalCountObj.value} entries.`
    );
    if (locked) {
      dirLock.unlock();
      locked = false;
      cleanupDirLockFile(dirLock, childDirName);
    }
  }
}

async function cleanupOtherDirectories(
  parentDirPath,
  otherFoldersSuffix,
  metrics
) {
  if (!otherFoldersSuffix || !otherFoldersSuffix.length) {
    return;
  }

  let targetFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
  targetFile.initWithPath(parentDirPath);

  let entries = targetFile.directoryEntries;
  while (entries.hasMoreElements()) {
    let entry = entries.nextFile;

    if (
      otherFoldersSuffix !== "*" &&
      !entry.leafName.endsWith(otherFoldersSuffix)
    ) {
      continue;
    }

    let shouldProcessEntry = false;
    // The folder could already be gone, so isDirectory could throw
    try {
      shouldProcessEntry = entry.isDirectory();
    } catch (e) {}

    if (!shouldProcessEntry) {
      continue;
    }

    let dirLock = Cc["@mozilla.org/net/CachePurgeLock;1"].createInstance(
      Ci.nsICachePurgeLock
    );
    let wasFirst = false;

    try {
      dirLock.lock(entry.leafName);
      wasFirst = !dirLock.isOtherInstanceRunning();
    } catch (e) {
      console.error("Failed to check dirlock. Skipping folder");
      dirLock.unlock();
      continue;
    }

    if (!wasFirst) {
      dirLock.unlock();
      continue;
    }

    // Remove directory recursively.
    let removedDir = tryRemoveDir(entry, metrics.suffixRemovalCountObj);
    if (!removedDir && entry.exists()) {
      // If first deletion of the directory failed, then we try again once more
      // just in case.
      metrics.suffixEverFailed = true;
      removedDir = tryRemoveDir(entry, metrics.suffixRemovalCountObj);
    }
    console.error(
      `Deletion of folder ${entry.leafName} - success=${removedDir}`
    );
    dirLock.unlock();
    cleanupDirLockFile(dirLock, entry.leafName);
  }
}

// Usage:
// removeDirectory parentDirPath childDirName secondsToWait [otherFoldersSuffix]
//                  arg0           arg1     arg2            arg3
//                 [--test-sleep testSleep]
//                 [--metrics-id metricsId]
// parentDirPath - The path to the parent directory that includes the target directory
// childDirName - The "leaf name" of the moved cache directory
//                If empty, the background task will only purge folders that have the "otherFoldersSuffix".
// secondsToWait - String representing the number of seconds to wait for the cacheDir to be moved
// otherFoldersSuffix - [optional] The suffix of directories that should be removed
//                      When not empty, this task will also attempt to remove all directories in
//                      the parent dir that end with this suffix
//                      As a special command, "*" will remove all subdirectories.
// testSleep - [optional] A test-only argument to sleep for a given milliseconds before removal.
//             This exists to test whether a long-running task can survive.
// metricsId - [optional] The identifier for Glean metrics, in PascalCase.
//             It'll be submitted only when the matching identifier exists in
//             toolkit/components/backgroundtasks/metrics.yaml.
export async function runBackgroundTask(commandLine) {
  const testSleep = Number.parseInt(
    commandLine.handleFlagWithParam("test-sleep", false)
  );
  const metricsId = commandLine.handleFlagWithParam("metrics-id", false) || "";

  if (commandLine.length < 3) {
    throw new Error("Insufficient arguments");
  }

  const parentDirPath = commandLine.getArgument(0);
  const childDirName = commandLine.getArgument(1);
  let secondsToWait = parseInt(commandLine.getArgument(2));
  if (isNaN(secondsToWait)) {
    secondsToWait = 10;
  }
  commandLine.removeArguments(0, 2);

  let otherFoldersSuffix = "";
  if (commandLine.length) {
    otherFoldersSuffix = commandLine.getArgument(0);
    commandLine.removeArguments(0, 0);
  }

  if (commandLine.length) {
    throw new Error(
      `${commandLine.length} unknown command args exist, closing.`
    );
  }

  console.error(
    parentDirPath,
    childDirName,
    secondsToWait,
    otherFoldersSuffix,
    metricsId
  );

  if (!Number.isNaN(testSleep)) {
    await new Promise(resolve => lazy.setTimeout(resolve, testSleep));
  }

  const metrics = new Metrics(metricsId);

  try {
    await deleteChildDirectory(
      parentDirPath,
      childDirName,
      secondsToWait,
      metrics
    );
    await cleanupOtherDirectories(parentDirPath, otherFoldersSuffix, metrics);
  } catch (err) {
    metrics.succeeded = false;
    throw err;
  } finally {
    await metrics.report();
  }

  return EXIT_CODE.SUCCESS;
}