Skip to content

Commit

Permalink
Register activity to call /autostart on boot (webosbrew#143)
Browse files Browse the repository at this point in the history
* service: register activity to call the /autostart on boot

* fix: wrap the activity into spec object

* updated workflows

* fixed missing variable

* Updated package-lock.json (webosbrew#145)

---------

Co-authored-by: Ningyuan Li <[email protected]>
  • Loading branch information
kitsuned and mariotaku authored Jul 18, 2023
1 parent de00433 commit 804f947
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion services/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,46 @@ async function removePackage(packageId: string, service: Service): Promise<void>
});
}

/**
* Register activity to call /autostart on boot
*/
async function registerActivity(service: Service) {
const activity = {
name: 'org.webosbrew.hbchannel.service.autostart',
description: 'Start HBChannel service on boot.',
type: {
foreground: true,
persist: true,
continuous: true,
},
trigger: {
method: 'luna://com.webos.bootManager/getBootStatus',
params: {
subscribe: true,
},
where: {
prop: ['signals', 'core-boot-done'],
op: '=',
val: true,
},
},
callback: {
method: 'luna://org.webosbrew.hbchannel.service/autostart',
params: {
reason: 'activity',
},
},
};

const spec = {
activity,
start: true,
replace: true,
};

return new Promise((resolve) => service.activityManager.create(spec, resolve));
}

/**
* Thin wrapper that responds with a successful message or an error in case of a JS exception.
*/
Expand Down Expand Up @@ -587,9 +627,14 @@ function runService() {
})),
);

service.register(
'registerActivity',
tryRespond(() => registerActivity(service)),
);

service.register(
'autostart',
tryRespond(async () => {
tryRespond(async (message) => {
if (!runningAsRoot()) {
return { message: 'Not running as root.', returnValue: true };
}
Expand All @@ -614,6 +659,12 @@ function runService() {
detached: true,
stdio: ['ignore', 'ignore', 'ignore'],
});

// Register activity if autostart was triggered in traditional way
if (message.payload?.reason !== 'activity') {
await registerActivity(service);
}

return { returnValue: true };
}),
);
Expand Down

0 comments on commit 804f947

Please sign in to comment.