-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathJobManager.gs
More file actions
239 lines (211 loc) · 7.83 KB
/
JobManager.gs
File metadata and controls
239 lines (211 loc) · 7.83 KB
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
// Shared Job Manager for handling long-running background processes with UI feedback
var JobManager = {
GLOBAL_JOB_KEY: 'CB_INVENTORY_CURRENT_JOB',
/**
* Opens the generic Sidebar UI.
* @param {object} config Configuration object for the sidebar.
* @param {string} config.title Title to display (e.g., "Update Devices").
* @param {string} [config.jobType] Type of job to start (e.g., 'UPDATE', 'MOVE').
* @param {number} [config.itemCount] Number of items to process (for confirmation).
*/
showJobSidebar: function(config) {
try {
console.log("JobManager: showJobSidebar called", config);
const template = HtmlService.createTemplateFromFile('Sidebar');
// Pass configuration to the template
if (config) {
template.config = JSON.stringify(config);
} else {
template.config = JSON.stringify({});
}
const html = template.evaluate()
.setTitle('CB_Inventory')
.setWidth(300);
SpreadsheetApp.flush(); // Ensure state is clean before UI render
Utilities.sleep(500); // Slight delay to ensure UI service is ready
SpreadsheetApp.getUi().showSidebar(html);
console.log("JobManager: Sidebar displayed");
} catch (e) {
console.error("JobManager: Failed to show sidebar: " + e.message);
SpreadsheetApp.getUi().alert("Failed to open sidebar: " + e.message);
}
},
/**
* Initializes a new job state.
* @param {string} title - The display title for the job.
* @param {number} totalCount - Total items to process.
* @return {object} The initial status object.
*/
initJob: function(title, totalCount) {
const lock = LockService.getScriptLock();
try {
if (!lock.tryLock(5000)) {
throw new Error("Could not acquire lock to initialize job.");
}
const status = {
title: title,
isActive: true,
isFinished: false,
error: null,
processedCount: 0,
totalCount: totalCount,
message: 'Initializing...',
startTime: Date.now(),
lastUpdated: Date.now(),
cancelRequested: false
};
PropertiesService.getScriptProperties().setProperty(this.GLOBAL_JOB_KEY, JSON.stringify(status));
return status;
} finally {
if (lock.hasLock()) lock.releaseLock();
}
},
/**
* Updates the current job status.
* @param {number} processed - Number of items processed so far.
* @param {string} message - Current activity description.
* @param {boolean} [finished] - (Optional) Mark job as finished.
* @param {string} [error] - (Optional) Error message if failed.
*/
updateJob: function(processed, message, finished, error) {
const lock = LockService.getScriptLock();
try {
if (!lock.tryLock(5000)) {
console.warn("JobManager: Could not acquire lock to update job. Skipping update.");
return;
}
const prop = PropertiesService.getScriptProperties().getProperty(this.GLOBAL_JOB_KEY);
if (!prop) return; // No active job
let status = JSON.parse(prop);
// Preserve cancelRequested if it was set by another process
const wasCancelled = status.cancelRequested;
status.processedCount = processed;
// Update message if not cancelled OR if this is a terminal state (finished/error)
if (!wasCancelled || finished || error) {
status.message = message;
}
status.lastUpdated = Date.now();
// Ensure we don't clear the cancelRequested flag if it was set
if (wasCancelled) status.cancelRequested = true;
if (finished !== undefined) status.isFinished = finished;
if (finished) status.isActive = false;
if (error) {
status.error = error;
status.isActive = false;
status.isFinished = true;
}
PropertiesService.getScriptProperties().setProperty(this.GLOBAL_JOB_KEY, JSON.stringify(status));
} catch (e) {
console.error("Failed to updateJob: " + e.message);
} finally {
if (lock.hasLock()) lock.releaseLock();
}
},
/**
* Checks if the current job has been cancelled by the user.
* @return {boolean} True if cancelled.
*/
isJobCancelled: function() {
// No lock needed for simple read
const prop = PropertiesService.getScriptProperties().getProperty(this.GLOBAL_JOB_KEY);
if (!prop) return false;
return JSON.parse(prop).cancelRequested === true;
},
/**
* Called by client-side polling to get current status.
* @return {object|null} The job status object.
*/
getJobStatus: function() {
// No lock needed for simple read
const prop = PropertiesService.getScriptProperties().getProperty(this.GLOBAL_JOB_KEY);
if (!prop) return null;
return JSON.parse(prop);
},
/**
* Called by client-side to request cancellation.
*/
cancelJob: function() {
const lock = LockService.getScriptLock();
try {
if (!lock.tryLock(5000)) {
console.warn("JobManager: Could not acquire lock to cancel job.");
return;
}
const prop = PropertiesService.getScriptProperties().getProperty(this.GLOBAL_JOB_KEY);
if (!prop) return;
let status = JSON.parse(prop);
status.cancelRequested = true;
status.message = "Cancellation requested...";
status.lastUpdated = Date.now();
PropertiesService.getScriptProperties().setProperty(this.GLOBAL_JOB_KEY, JSON.stringify(status));
} finally {
if (lock.hasLock()) lock.releaseLock();
}
},
/**
* Clears the job status.
*/
clearJob: function() {
// No lock needed for clearJob. Just force delete the property to reset state.
// This avoids deadlocks if a previous job crashed while holding a lock.
PropertiesService.getScriptProperties().deleteProperty(this.GLOBAL_JOB_KEY);
},
/**
* Finds the last row with actual non-empty content in a given column.
* Useful when Sheet.getLastRow() is unreliable due to hidden/empty strings.
* @param {GoogleAppsScript.Spreadsheet.Sheet} sheet The sheet to search.
* @param {number} column The 1-indexed column number to check for content.
* @returns {number} The 1-indexed row number of the last populated cell, or 1 if only headers are present or entirely empty.
*/
_findLastPopulatedRow: function(sheet, column) {
const lastRow = sheet.getLastRow();
if (lastRow === 0) return 1;
// Get all values in the column at once
const values = sheet.getRange(1, column, lastRow, 1).getValues();
// Check from bottom up in memory
for (let i = values.length - 1; i >= 0; i--) {
const val = values[i][0];
if (val !== "" && val !== null && typeof val !== 'undefined') {
return i + 1;
}
}
return 1;
},
/**
* Cleans up all triggers associated with batch processing functions in the project.
* This prevents "Too many triggers" errors when switching between different jobs.
*/
cleanupAllTriggers: function() {
const triggers = ScriptApp.getProjectTriggers();
const knownHandlers = [
'processUpdateBatch',
'processEnableBatch',
'processDisableBatch',
'processClearProfilesBatch',
'processPowerWashBatch',
'processDeprovisionBatch',
'processMoveBatch',
'processFindUserOUsBatch',
'processExportBatch',
'processFindUnlocatedDevicesBatch'
];
for (const trigger of triggers) {
if (knownHandlers.includes(trigger.getHandlerFunction())) {
ScriptApp.deleteTrigger(trigger);
}
}
}
};
/**
* Global wrapper functions to expose JobManager methods to client-side google.script.run
* Client-side cannot call JobManager.method() directly, only top-level functions.
*/
function getJobStatus() {
return JobManager.getJobStatus();
}
function cancelJob() {
return JobManager.cancelJob();
}
function clearJob() {
return JobManager.clearJob();
}