Skip to content
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

🐛 Bug Report: cloud functions with Dart 2.14 runtime not working #2353

Closed
2 tasks done
Silfalion opened this issue Oct 25, 2021 · 36 comments
Closed
2 tasks done

🐛 Bug Report: cloud functions with Dart 2.14 runtime not working #2353

Silfalion opened this issue Oct 25, 2021 · 36 comments
Assignees
Labels
bug Something isn't working

Comments

@Silfalion
Copy link

👟 Reproduction steps

  • Create a small functions that prints hello world in dart.
  • push it to the function worker
  • execute the function

👍 Expected behavior

Prints hello world

👎 Actual Behavior

empty stdout and stderr

🎲 Appwrite version

Different version (specify in environment)

💻 Operating system

Linux

🧱 Your Environment

uses 0.11 version

👀 Have you spent some time to check if this issue has been raised before?

  • I checked and didn't find similar issue

🏢 Have you read the Code of Conduct?

@Silfalion Silfalion added the bug Something isn't working label Oct 25, 2021
@lohanidamodar
Copy link
Member

@Silfalion hi, I'm unable to reproduce the error using your guidelines. Might be something to do with your system. Please provide logs of your docker console as well as the code used. I'm using the following code

import "dart:io";
import "package:dart_appwrite/dart_appwrite.dart";

void main() { // Init SDK
  Client client = Client();

  client
    .setEndpoint(Platform.environment["APPWRITE_ENDPOINT"]) // Your API Endpoint
    .setProject(Platform.environment["APPWRITE_PROJECT"]) // Your project ID
    .setKey(Platform.environment["APPWRITE_SECRET"]) // Your secret API key
  ;

  Storage storage = Storage(client);

  //Future result = storage.getFile(fileId: '[FILE_ID]');

  print(Platform.environment["APPWRITE_FUNCTION_ID"]);
  print(Platform.environment["APPWRITE_FUNCTION_NAME"]);
  print(Platform.environment["APPWRITE_FUNCTION_TAG"]);
  print(Platform.environment["APPWRITE_FUNCTION_TRIGGER"]);
  print(Platform.environment["APPWRITE_FUNCTION_ENV_NAME"]);
  print(Platform.environment["APPWRITE_FUNCTION_ENV_VERSION"]);
  // print(result['$id']);
  print(Platform.environment["APPWRITE_FUNCTION_EVENT"]);
  print(Platform.environment["APPWRITE_FUNCTION_EVENT_DATA"]);
}

and I'm able to run and and print as below
image

@Silfalion
Copy link
Author

Silfalion commented Oct 28, 2021

@lohanidamodar Hey, I was able to use your code without problem but I still get the same issue. Here is the log:

appwrite-worker-functions       | Container is ready to run
appwrite-worker-functions       | Function executed in 4.3708670139313 seconds, status: completed
appwrite-worker-functions       | 20 running containers counted
appwrite-worker-functions       | [notice] (Job{v1-functions} | ID: caa4ccb6c7d6d7bf14870c880f213008 | FunctionsV1 | [{"projectId":"61323a8aa31c4","webhooks":[],"functionId":"617a2d181242b","executionId":"617a39fe94251","trigger":"http","data":"{\"order\":{\"$id\":\"0\",\"clientId\":\"614ca67accb5f\",\"deliverymanId\":null,\"shopId\":\"61328a2cb7abd\",\"orderedProducts\":[],\"productIds\":[\"6178532d870eb\"],\"orderTime\":1635403785.223,\"pickedUpTime\":null,\"deliveredTime\":null,\"durationToDeliver\":15,\"total\":420.0,\"deliveryFee\":200.0,\"deliverymanPercentage\":120.0,\"status\":\"preparing\",\"durationToPickUp\":1,\"typeTransport\":\"bicycle\",\"deliveryCode\":null,\"pickupCode\":null,\"deliveryScore\":5.0,\"pickUpPosition\":\"{\\\"latitude\\\":36.1499932,\\\"longitude\\\":1.3663652000000002,\\\"accuracy\\\":null,\\\"altitude\\\":null,\\\"speed\\\":null,\\\"speedAccuracy\\\":null,\\\"heading\\\":null,\\\"time\\\":null,\\\"verticalAccuracy\\\":null,\\\"headingAccuracy\\\":null}\",\"deliveryPosition\":\"{\\\"latitude\\\":36.154531910110016,\\\"longitude\\\":1.3224109134464015,\\\"accuracy\\\":null,\\\"altitude\\\":null,\\\"speed\\\":null,\\\"speedAccuracy\\\":null,\\\"heading\\\":null,\\\"time\\\":null,\\\"verticalAccuracy\\\":null,\\\"headingAccuracy\\\":null}\"},\"orderedProducts\":[{\"productId\":\"6178532d870eb\",\"bundleOfferId\":null,\"buyOneGetOneId\":null,\"rating\":null,\"quantity\":1,\"options\":\"{}\",\"discount\":0.0,\"createdAt\":1635403780.934,\"updatedAt\":1635403780.935}],\"shopUserId\":\"61328a2c5e037\",\"userId\":\"614ca66855569\"}","userId":"614ca66855569","jwt":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOiI2MTRjYTY2ODU1NTY5Iiwic2Vzc2lvbklkIjoiNjE3OTA0NDViODQ4NyIsImV4cCI6MTYzNTQwMTA5MH0.yjMd3DulJ5NkzG0MpKg7s2DvSTo5mMN0nak5yinNRiY"}]) has finished
appwrite-worker-functions       | [notice] Starting work on (Job{v1-functions} | ID: 2ead440cd665e8b6feb824aa58149ce6 | FunctionsV1 | [{"projectId":"61323a8aa31c4","webhooks":[],"userId":"614ca66855569","event":"functions.executions.create","eventData":{"$id":"617a39fe94251","$permissions":{"read":["user:614ca66855569"],"write":[]},"functionId":"617a2d181242b","dateCreated":1635400190,"trigger":"http","status":"waiting","exitCode":0,"stdout":"","stderr":"","time":0},"functionId":null,"executionId":null,"trigger":"event"}])
appwrite-worker-functions       | Fetched 10 functions...
appwrite-worker-functions       | Itterating function: autoselect_deliverymen
appwrite-worker-functions       | Itterating function: autoselect_deliverymen
appwrite-worker-functions       | Itterating function: check_deliverymen_availability
appwrite-worker-functions       | Itterating function: check_top_code
appwrite-worker-functions       | Itterating function: confirm_order_operations
appwrite-worker-functions       | Itterating function: create_otp_code
appwrite-worker-functions       | Itterating function: create_team_membership
appwrite-worker-functions       | Itterating function: popularity_trending_calculator
appwrite-worker-functions       | Itterating function: send_notifications
appwrite-worker-functions       | Itterating function: test
appwrite-worker-functions       | [notice] (Job{v1-functions} | ID: 2ead440cd665e8b6feb824aa58149ce6 | FunctionsV1 | [{"projectId":"61323a8aa31c4","webhooks":[],"userId":"614ca66855569","event":"functions.executions.create","eventData":{"$id":"617a39fe94251","$permissions":{"read":["user:614ca66855569"],"write":[]},"functionId":"617a2d181242b","dateCreated":1635400190,"trigger":"http","status":"waiting","exitCode":0,"stdout":"","stderr":"","time":0},"functionId":null,"executionId":null,"trigger":"event"}]) has finished

The code is too long to paste here but this is the beginning of it:

import 'dart:convert';
import 'dart:io';
import 'dart:math';

import 'package:autoselect_deliveryman/create_orders.dart';
import 'package:autoselect_deliveryman/services/appwrite_api_service.dart';
import 'package:dart_appwrite/dart_appwrite.dart';
import 'package:maps_toolkit/maps_toolkit.dart';
import 'package:oreeed_models_data/oreeed_models_data.dart';
import 'package:tuple/tuple.dart';

import '../lib/services/mini_database.dart';

main() async {
  print("does it work here");
  Map<String, String> envVars = Platform.environment;
  final payload = jsonDecode(envVars['APPWRITE_FUNCTION_DATA']!);
  // the collection and document id of the new document

  final clientId = payload['userId'];
  final shopUserId = payload['shopUserId'];
  final Map<String, dynamic> orderMap = payload['order'];
  final List<dynamic> orderedProductsMap = payload['orderedProducts'];

But it still doesnt print that.

@lohanidamodar
Copy link
Member

@Silfalion Were you able to get the output with my code or not?

@Silfalion
Copy link
Author

I was. It outputted normally.

@lohanidamodar
Copy link
Member

lohanidamodar commented Oct 28, 2021

@Silfalion Does also simple hello world work? If not can you provide exact hello world code that did not work?

@Silfalion
Copy link
Author

@Silfalion Does also simple hello world work?

Per the code I showed you now, it doesn't. Even though it was the very first line on the main function, it didn't print it.

@Silfalion
Copy link
Author

Nothing on STDOUT and nothing on STDERR.

@Silfalion
Copy link
Author

@Silfalion Does also simple hello world work? If not can you provide exact hello world code that did not work?

It's the one I copied above. The whole project is a little bit too big to copy over here.

@lohanidamodar
Copy link
Member

@Silfalion There might be other issues in the code. It's not a simple hello world function, it has lots of imports and other codes. Try to run your code normally in your local environment without using Appwrite runtime to make sure the code is working as expected.

@Silfalion
Copy link
Author

It works without issues with both the dart 2.13 and 2.14 environments.

@lohanidamodar
Copy link
Member

It works without issues with both the dart 2.13 and 2.14 environments.

Then may be try to run locally using our runtime docker image?

@Silfalion
Copy link
Author

An error spawns every now and then though, just got it now:

Warning: unlimk/usr/local/code/files/code.tar.gz): No such file or directory in /usr/local/code/app/functions.php on line 328

But the function is added normally even then.

@Silfalion
Copy link
Author

It works without issues with both the dart 2.13 and 2.14 environments.

Then may be try to run locally using our runtime docker image?

Sorry, can you explain? It's I meant I tried.

@lohanidamodar
Copy link
Member

I mean run locally using the Docker image of our runtime, instead of local on your computer. If you already ran with our docker image successfully then let me know.

@lohanidamodar
Copy link
Member

lohanidamodar commented Oct 28, 2021

An error spawns every now and then though, just got it now:

Warning: unlimk/usr/local/code/files/code.tar.gz): No such file or directory in /usr/local/code/app/functions.php on line 328

But the function is added normally even then.

When does this error come?lo

An error spawns every now and then though, just got it now:

Warning: unlimk/usr/local/code/files/code.tar.gz): No such file or directory in /usr/local/code/app/functions.php on line 328

But the function is added normally even then.

When does this error come?

When creating a new tag through appwrite cli. But it happens sporadically, with no exact pattern. Does the appwrite cli version may be causing that? I think I'm still running the previous version.

If you think it is, can you direct me on how to update it.

Running the installation again should update it

@Silfalion
Copy link
Author

I mean run locally using the Docker image of our runtime, instead of local on your computer. If you already ran with our docker image successfully then let me know.

Ah yes. That's what I did, I don't have a copy of the server locally. I used both the 2.13 and 2.14 runtime docker images locally and they worked without issues.

@Silfalion
Copy link
Author

An error spawns every now and then though, just got it now:

Warning: unlimk/usr/local/code/files/code.tar.gz): No such file or directory in /usr/local/code/app/functions.php on line 328

But the function is added normally even then.

When does this error come?

When creating a new tag through appwrite cli. But it happens sporadically, with no exact pattern. Does the appwrite cli version may be causing that? I think I'm still running the previous version.

If you think it is, can you direct me on how to update it.

@Silfalion
Copy link
Author

Silfalion commented Oct 28, 2021

I tried just now to run the same function with an empty file containing only a main and a print and it still didn't show anything. What does that mean lol? Is it possible the other containers are causing this?

If you noticed, there are 20 containers running, but I don't have that many cloud functions. It looks like they're not being disposed of.

@Silfalion
Copy link
Author

Correcting my last message. It does work where there nothing else but the print message. Will try to look for what else is causing it.

@Silfalion
Copy link
Author

@lohanidamodar Ok, I think I found the issue. The problem is when importing files from the folder, it just exits then.

If the folder is named project then:

import 'package:project/some_file.dart'

Will cause it to exit instantly. This didn't happen with dart 2.13 although I still an issue of the function exiting instantly in that version, but idk if its related.

Basically the solution would be to put everything in the same file but I didn't try it yet.

@Silfalion
Copy link
Author

Tested again and I have a private repository that's part of the dependencies when I delete its imports it works. With the just printing stuff, nothing else.

@Silfalion
Copy link
Author

@lohanidamodar sorry, is there a timeout for the functions to stops? What I mean is that there is a future that also pushes the function to exit. Multiple future all connecting to appwrite. On each function execution you can find a different outputs.

Either the first future finishes then the function stops or it can go until the third but doesn't go beyond, it's like the function is forced to exit at that point, is this normal?

I can open a separate issue if this seems unrelated.

@TafadzwaD
Copy link
Contributor

@Silfalion under the function's setting there is a Timeout field. Dart is quite slow when it comes to cloud functions so you can increase the timeout to prevent the function from being terminated before completion, the default is 15 seconds and the maximum is 900 seconds
gitboy

@Silfalion
Copy link
Author

@TafadzwaD already increased it to the limit of 900. What did the trick temporarily for now is to add prints to the functions with a future return type. It seems like that the code really awaits until it completes.

@Silfalion
Copy link
Author

Apparently the issue is dart 2.14 not liking imports of libraries not from pub.dev. deleting the imports makes the code execute. But I can't work without the package. Any idea about this please?

@TafadzwaD
Copy link
Contributor

Which version of AppWrite are you on??

@Silfalion
Copy link
Author

0.11

@TafadzwaD
Copy link
Contributor

@lohanidamodar Kindly assist on this one..

@Silfalion
Copy link
Author

@TafadzwaD I think he may be waiting for the release of version 0.12 to solve everything in one go.

@TafadzwaD
Copy link
Contributor

@Silfalion Maybe that might be case.. So hang in there buddie 😅😅😅

@Silfalion
Copy link
Author

Will do haha Thank you very much for your assistance!

@TafadzwaD
Copy link
Contributor

You're most welcome 😀

@Silfalion
Copy link
Author

Silfalion commented Dec 23, 2021

:) So did some more testing, tested on a frensh install of 0.11 and on a fresh install on 0.12 on two different machines. And both give the same result. Thanks to drake we found something. When executing into the function's container and running dart bin/main.dart and it returns Killed. I'm very lost at this behavior.

@lohanidamodar
Copy link
Member

@Silfalion Here is my test result.

  1. Package from local directory works perfectly fine
  2. package from git, works fine
  3. Your package- gets killed
    so my guess is it's issue with trying to take too much of memory. Try working by assigning largest possible memory to test it and then bring it down.

However, I also suggest few things

  1. Don't try to do too many things in same function, you should have different functions to handle different things
  2. Functions should only work on small short one time tasks, should not take long
  3. Dart function is already heavy, boil it down as much as possible
  4. May be compile and run compiled output, that should be much faster

@Silfalion
Copy link
Author

On point recommendations. Was thinking about it since I need quicker responses for this function in particular. Will give them a try and see how it goes. Thank you so much.

@eldadfux
Copy link
Member

Closing this issue for now, we can reopen it if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants
@eldadfux @lohanidamodar @Silfalion @TafadzwaD and others