I’m trying to use the node sdk to restart a service on a device. balena.models.device.restartService(uuid, imageId)
I’m getting the image id via the current_services array returned as part of balena.models.device.getWithServiceDetails(uuid)
in the shape of: { "<service-name>": [{ ... "image_id": 999999, ... }] }
However when I run the restartService method above the response is:
ERROR : (node:90) UnhandledPromiseRejectionWarning: BalenaRequestError: Request error: Service not found, a container must exist for this endpoint to work
I can restart the service by using the supervisor api but only if I send the serviceName not imageId which results in the same error. Is there a different way to get the imageId or a way to get a different imageId that works?
Hi there, I just did some experimenting on my own machine to reproduce the behaviour you are reporting - it seems as though the object that is returned by balena.models.device.getWithServiceDetails(uuid) returns an object that contains something like this:
Unfortunately the id you found deep within the image_install object is the same image_id value I get from the current_services array from the getWithServiceDetails(uuid) function call.
At least now I know I was using the right image_id. Where are you getting your device uuid? I’m just copying it from the balena cloud dashboard. Could that be the wrong value possibly?
I used this on an open source balena project on balenaHub called balenaSound.
If you want to try this too and have an extra device laying around, you can try too, if you want to try something that is known to work. Maybe you’ll see something you missed and get it working that way.
For the next diagnostic, I’d like you to tunnel into your balena device, however you want to do it (cloud dashboard or balena ssh) and run the following commands:
Obviously substituting “1234” and “my-service” with your real service name/image id.
and let us know the results.
The purpose of this is to determine if there’s something in the pipeline between the SDK and the device that is preventing the service from restarting or if it’s the supervisor itself that is bugged.
Thank you for you patience and assistance in making Balena a better product. I hope to hear from you soon.
Also, sorry, forgot to mention, you’ll need to enable those variables in order to send that command. From the docs:
To enable these Supervisor environment variables, the io.balena.features.supervisor-api label must be applied for each service that requires them. See here for further details.
Thank you for the help so far. Still no luck here. I’ve tried to add the labels to the compose file to enable the supervisor environment variables but they don’t seem to be available. Would you mind looking over my compose file to see if I’m doing it correctly?
when I ssh into the into the service after deploying with: balena ssh <device uuid> <service name>
and try to echo a variable with: echo $BALENA_SUPERVISOR_ADDRESS
I get nothing returned.
I was able to run curl --header "Content-Type:application/json" "$BALENA_SUPERVISOR_ADDRESS/v2/applications/$BALENA_APP_ID/restar t-service?apikey=$BALENA_SUPERVISOR_API_KEY" -d '{"serviceName": "pi-services"}' from your electron container just fine.
How are you trying to run this? Can you mention detailed steps?
Hi @anujdeshpande. As a matter of fact I am also able to restart the service by sending a supervisor api request and specifying a service name. My issue is with the node sdk with an image id, i.e. sdk.models.device.restartService(uuid, imageId)
And it’s also probably significant that while I can restart a service using the api by specifying a service name, I am unable to restart a service using the api by specifying an imageId. In fact the error outputs of both methods - api using imageId and sdk using imageId - are the same: ... Service not found, a container must exist for this endpoint to work
Hi @georgiats sure, although there’s not much more to it:
2021-02-02 8:51:12 PM [Console] ERROR : (node:115) UnhandledPromiseRejectionWarning: BalenaRequestError: Request error: Service not found, a container must exist for this endpoint to work
at Object.<anonymous> (/app/node_modules/balena-request/build/request.js:190:27)
at Generator.next (<anonymous>)
at fulfilled (/app/node_modules/balena-request/node_modules/tslib/tslib.js:114:62)
at process._tickCallback (internal/process/next_tick.js:68:7)
2021-02-02 8:51:12 PM [Console] ERROR : (node:115) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
2021-02-02 8:51:12 PM [Console] ERROR : (node:115) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
This is a result of running: sdk.models.device.restartService('80c335fe0600452f174b88b05eafd5e9', 3216996)
OK, I’ve tested below in browser dev tools console against your device and found it to be working correctly (restarted both electron and pi-services services):
Can you please check the image_id you are supplying by inspecting the device dictionary returned by the getWithServiceDetails method to get the current (running) image ids.
Yes the image ids I’m using, that are returned by the getWithServiceDetails() method are 3220189 for electron and 3220190 for pi-services. Are those the same image ids you have?
As luck would have it support agents were disallowed from attempting to restart containers a mere 7 hours ago.
So, there’s no way for me to manually try to restart your containers. I believe this is to prevent an accidentally restart of a container that messes up the application of a customer, but makes diagnosing things like this rather difficult.
But to answer your question, yes those are the same image ids my Anton and I both got.
Can you try opening Chrome Developer Tools (or your browser equivalent) while on the balena cloud dashboard and past the exact command:
I’m really at a loss right now because I was testing it with, as far as I can tell, accurate hard-coded values.
This proves the sdk is working though and there is something faulty with my code. Thank you to everyone on this thread in helping me to debug this. I really appreciate the time you took.