@@ -23,7 +23,7 @@ async function retry(attempt, cb) {
2323 return await cb ( ) ;
2424 } catch ( err ) {
2525 if ( attempt -- ) {
26- log ( `An error occured , ${ attempt } tries left... ` ) ;
26+ log ( `... failed ( ${ err . message } ) , ${ attempt } tries left` ) ;
2727 continue ;
2828 }
2929 throw err ;
@@ -35,50 +35,136 @@ function log(s) {
3535 document . querySelector ( 'pre' ) . innerText += `[${ new Date ( ) } ] ${ s } \n` ;
3636}
3737
38+ function logres ( res ) {
39+ if ( res . returnValue ) {
40+ if ( res . message ) {
41+ log ( `... success (${ res . message } )` ) ;
42+ }
43+ else {
44+ log ( '... success' ) ;
45+ }
46+ }
47+ else {
48+ log ( `... failed (${ res . message } )` ) ;
49+ }
50+ }
51+
3852( async ( ) => {
53+
54+ /* We'll do a certain number of luna calls.
55+ On startup, sometimes these calls fail (bus overload?),
56+ so we'll retry them 3 times if we get an error.
57+ Each call usually take between 0 and 10 seconds. */
58+
3959 try {
40- log ( 'Registering input app...' ) ;
41- await lunaCall ( 'luna://com.webos.service.eim/addDevice' , {
42- appId : 'org.webosbrew.autostart' ,
43- pigImage : '' ,
44- mvpdIcon : '' ,
60+ /* Be polite and launch the actual previous input app, if any.
61+ This'll give back control to the user while we finish our setup
62+ in the background, as the whole process can be slow due to luna calls
63+ and the number of other processes competing for CPU on startup. */
64+ await retry ( 3 , async ( ) => {
65+ log ( "Checking last input app..." ) ;
66+ const lastinput = await lunaCall ( 'luna://org.webosbrew.hbchannel.service/exec' , {
67+ command : 'cat /var/lib/eim/lastinput' ,
68+ } ) ;
69+ if ( lastinput . stdoutString ) {
70+ const lastinputPayload = JSON . parse ( lastinput . stdoutString ) ;
71+ if ( lastinputPayload . appId ) {
72+ log ( `Last input app: ${ lastinputPayload . appId } ` ) ;
73+ if ( lastinputPayload . appId !== 'org.webosbrew.autostart' ) {
74+ log ( "Relaunching this app..." ) ;
75+ const res = await lunaCall ( 'luna://com.webos.service.applicationManager/launch' , {
76+ id : lastinputPayload . appId ,
77+ } ) ;
78+ logres ( res ) ;
79+ }
80+ }
81+ }
82+ else {
83+ log ( "No app to relaunch." ) ;
84+ }
4585 } ) ;
86+ } catch ( err ) {
87+ log ( `Couldn't start last input app but carrying on:\n${ err . stack } ` ) ;
88+ }
4689
90+ try {
91+ /* Launch autostart soon in the process because even if we fail later down,
92+ at least everything will have started (including SSH). */
4793 await retry ( 3 , async ( ) => {
48- log ( 'Setting up eim overlay...' ) ;
49- const res = await lunaCall ( 'luna://org.webosbrew.hbchannel.service/exec' , {
50- command : 'if [[ ! -d /var/lib/webosbrew/eim ]]; then cp -r /var/lib/eim /var/lib/webosbrew/eim; fi ; if ! findmnt /var/lib/eim; then mount --bind /var/lib/webosbrew/eim /var/lib/eim ; fi' ,
94+ log ( "Launching autostart..." ) ;
95+ const res = await lunaCall ( 'luna://org.webosbrew.hbchannel.service/autostart' , { } ) ;
96+ logres ( res ) ;
97+ } ) ;
98+ } catch ( err ) {
99+ log ( `Couldn't run autostart, but carrying on:\n${ err . stack } ` ) ;
100+ }
101+
102+ /* Now, all that we want is ensuring that we get started on next boot,
103+ all the logic below is here to ensure this is the case. */
104+
105+ try {
106+ /* First, unregister (always succeeds, even if we weren't registered),
107+ this is needed because we'll get started only on the boot following
108+ a successful addDevice, and this only succeeds if we're not already in the list. */
109+ await retry ( 3 , async ( ) => {
110+ log ( 'Unregistering ourselves as input app...' ) ;
111+ const res = await lunaCall ( 'luna://com.webos.service.eim/deleteDevice' , {
112+ appId : 'org.webosbrew.autostart' ,
51113 } ) ;
52- log ( `Result: ${ res . stdoutString } ${ res . stderrString } ` ) ;
114+ logres ( res ) ;
53115 } ) ;
116+ } catch ( err ) {
117+ log ( `Couldn't unregister, but carrying on:\n${ err . stack } ` ) ;
118+ }
54119
55- log ( "Launching autostart..." ) ;
56- const res2 = await lunaCall ( 'luna://org.webosbrew.hbchannel.service/autostart' , { } ) ;
57- log ( `Result: ${ res2 . message } ` ) ;
120+ try {
121+ // Register again to ensure we're started on next boot.
122+ await retry ( 3 , async ( ) => {
123+ log ( 'Registering ourselves as input app...' ) ;
124+ const res = await lunaCall ( 'luna://com.webos.service.eim/addDevice' , {
125+ appId : 'org.webosbrew.autostart' ,
126+ pigImage : '' ,
127+ mvpdIcon : '' ,
128+ type : 'MVPD_IP' , // can be either MVPD_IP or MVPD_RF, required for webOS 3.4 at least
129+ label : 'Autostart' , // required for webOS 3.4 at least
130+ description : 'webosbrew autostart' , // required for webOS 3.4 at least
131+ } ) ;
132+ logres ( res ) ;
133+ } ) ;
134+ } catch ( err ) {
135+ log ( `Couldn't register, but carrying on:\n${ err . stack } ` ) ;
136+ }
58137
59- log ( "Removing input app..." ) ;
60- await lunaCall ( 'luna://com.webos.service.eim/deleteDevice' , {
61- appId : 'org.webosbrew.autostart' ,
138+ try {
139+ /* Now, setup an eim overlay so that any changes done later don't erase our own app
140+ if /var/lib/webosbrew/eim already exists, keep it that way, changes done in previous
141+ sessions will live here, so just bind mount it, otherwise create it
142+ from the /var/lib/eim contents. */
143+ await retry ( 3 , async ( ) => {
144+ log ( 'Setting up eim overlay...' ) ;
145+ const res = await lunaCall ( 'luna://org.webosbrew.hbchannel.service/exec' , {
146+ command : 'if [[ ! -d /var/lib/webosbrew/eim ]]; then cp -r /var/lib/eim /var/lib/webosbrew/eim && echo cp ok || echo cp failed; fi ; if ! findmnt /var/lib/eim; then mount --bind /var/lib/webosbrew/eim /var/lib/eim && echo mount overlay ok || echo mount overlay failed; fi' ,
147+ } ) ;
148+ log ( `Result: ${ res . stdoutString } ${ res . stderrString } ` ) ;
62149 } ) ;
150+ } catch ( err ) {
151+ log ( `Couldn't setup overlay, stopping here:\n${ err . stack } ` ) ;
152+ return ;
153+ }
63154
64- log ( "Checking last input app..." ) ;
65- const lastinput = await lunaCall ( 'luna://org.webosbrew.hbchannel.service/exec' , {
66- command : 'cat /var/lib/eim/lastinput' ,
155+ try {
156+ /* This just removes ourselves from the overlay, but we still live in the real /var/lib/eim,
157+ which guarantees that we'll survive on the next reboot. */
158+ await retry ( 3 , async ( ) => {
159+ log ( 'Unregistering ourselves as input app from the overlay...' ) ;
160+ const res = await lunaCall ( 'luna://com.webos.service.eim/deleteDevice' , {
161+ appId : 'org.webosbrew.autostart' ,
162+ } ) ;
163+ logres ( res ) ;
67164 } ) ;
68- if ( lastinput . stdoutString ) {
69- const lastinputPayload = JSON . parse ( lastinput . stdoutString ) ;
70- if ( lastinputPayload . appId ) {
71- log ( `Last input app: ${ lastinputPayload . appId } ` ) ;
72- if ( lastinputPayload . appId !== 'org.webosbrew.autostart' ) {
73- log ( "Relaunching..." ) ;
74- await lunaCall ( 'luna://com.webos.service.applicationManager/launch' , {
75- id : lastinputPayload . appId ,
76- } ) ;
77- }
78- }
79- }
80- log ( "Done." ) ;
81165 } catch ( err ) {
82- log ( `An error occured: ${ err . stack } ` ) ;
166+ log ( `Couldn't unregister, but carrying on:\n ${ err . stack } ` ) ;
83167 }
168+
169+ log ( "Done." ) ;
84170} ) ( ) ;
0 commit comments