-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Service installs: Avoid creating duplicate tasks on device registration #1867
base: master
Are you sure you want to change the base?
Changes from all commits
e6d22cd
39fb0d9
ad0c14d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -260,6 +260,13 @@ hooks.addPureHook('POST', 'resin', 'device', { | |
const app = request.values.belongs_to__application; | ||
if (app != null) { | ||
await createAppServiceInstalls(rootApi, app, [deviceId], tx); | ||
if (ASYNC_TASKS_ENABLED && ASYNC_TASK_CREATE_SERVICE_INSTALLS_ENABLED) { | ||
// When creating service installs via the async tasks, a single call to createServiceInstallsAsync() | ||
// creates all service installs (target release, hostApp & SV release), so we don't need | ||
// to run the extra code below to handle them separately, and doing so would create | ||
// duplicate & unnecessary tasks. | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since I expect that soon tasks will become the only supported way for creating service installs, I decided to fix this in the simplest way that required the less changes in the current code. |
||
} | ||
} | ||
|
||
const release = request.values.is_pinned_on__release; | ||
|
@@ -273,6 +280,23 @@ hooks.addPureHook('POST', 'resin', 'device', { | |
tx, | ||
); | ||
} | ||
|
||
const svAndHostAppReleaseIds = [ | ||
request.values.should_be_managed_by__release, | ||
request.values.should_be_operated_by__release, | ||
].filter((releaseId) => releaseId != null); | ||
|
||
if (svAndHostAppReleaseIds.length > 0) { | ||
// Create supervisor/hostApp service installs when the supervisor/hostApp is pinned on device creation | ||
await createReleaseServiceInstalls( | ||
rootApi, | ||
[deviceId], | ||
{ | ||
id: { $in: svAndHostAppReleaseIds }, | ||
}, | ||
tx, | ||
); | ||
} | ||
}, | ||
}); | ||
|
||
|
@@ -346,40 +370,20 @@ hooks.addPureHook('PATCH', 'resin', 'device', { | |
}, | ||
}); | ||
|
||
const addSystemAppServiceInstallHooks = ( | ||
fieldName: 'should_be_managed_by__release' | 'should_be_operated_by__release', | ||
) => { | ||
hooks.addPureHook('POST', 'resin', 'device', { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved this higher, in the |
||
POSTRUN: async ({ request, api, tx, result: deviceId }) => { | ||
// Don't try to add service installs if the device wasn't created | ||
if (typeof deviceId !== 'number') { | ||
return; | ||
} | ||
|
||
const releaseId = request.values[fieldName]; | ||
// Create supervisor/hostApp service installs when the supervisor/hostApp is pinned on device creation | ||
if (releaseId != null) { | ||
const rootApi = api.clone({ | ||
passthrough: { tx, req: permissions.root }, | ||
}); | ||
await createReleaseServiceInstalls( | ||
rootApi, | ||
[deviceId], | ||
{ | ||
id: releaseId, | ||
}, | ||
tx, | ||
); | ||
} | ||
}, | ||
}); | ||
|
||
hooks.addPureHook('PATCH', 'resin', 'device', { | ||
POSTRUN: async ({ api, request, tx }) => { | ||
const affectedIds = request.affectedIds!; | ||
hooks.addPureHook('PATCH', 'resin', 'device', { | ||
POSTRUN: async ({ api, request, tx }) => { | ||
const affectedIds = request.affectedIds!; | ||
if (affectedIds.length === 0) { | ||
return; | ||
} | ||
// add system app service install PATCH hooks | ||
for (const fieldName of [ | ||
'should_be_managed_by__release', | ||
'should_be_operated_by__release', | ||
] as const) { | ||
const releaseId = request.values[fieldName]; | ||
// Create supervisor/hostApp service installs when the supervisor/hostApp is pinned on device update | ||
if (releaseId != null && affectedIds.length !== 0) { | ||
if (releaseId != null) { | ||
await createReleaseServiceInstalls( | ||
api, | ||
affectedIds, | ||
|
@@ -389,13 +393,6 @@ const addSystemAppServiceInstallHooks = ( | |
tx, | ||
); | ||
} | ||
}, | ||
}); | ||
}; | ||
|
||
for (const fieldName of [ | ||
'should_be_managed_by__release', | ||
'should_be_operated_by__release', | ||
] as const) { | ||
addSystemAppServiceInstallHooks(fieldName); | ||
} | ||
} | ||
}, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't
ASYNC_TASK_CREATE_SERVICE_INSTALLS_ENABLED
only ever equaltrue
ifASYNC_TASKS_ENABLED
is alsotrue
? Maybe there was a discussion about this at some point, but I feel like system init should fail ifASYNC_TASKS_ENABLED === false && ASYNC_TASK_CREATE_SERVICE_INSTALLS_ENABLED === true
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea I agree on that this was strange. I expected it to only have to check one of the two, maybe something like:
But since I don't have background on why it was done this way, I just copied it "as is" from above where we do the same check. I guess that it might be this way to make mocking in tests easier. I would prefer to PR such a change separately and fix all occurrences and whichever tests might break after having a short chat on it.