For example, here is a simple dart code:
#import('dart:io');
main() {
var server = new HttpServer();
server.listen('127.0.0.1', 8080);
server.defaultRequestHandler = (HttpRequest request, HttpResponse response) {
response.outputStream.write('Hello, world'.charCodes());
response.outputStream.close();
};
}
when the web server print the 'Hello, world', I would like to run a progress to run a
long heavy task, but don't want to it blocking the current process. May I know how to handle it? Thanks.
I tried with Process.run and Process.start with no success.
From you comment I can tell there are a misunderstanding of how Dart works spawning external processes. When you spawn a process in Dart it is by default running so the Dart program and the external program are running separate (so in different processes) and the Dart program can execute other stuff. You can then await for the result from the program (e.g. when it closes).
Therefore it does not make much sense to run the process with "&" as parameter (I guess this was an attempt to tell it should run separately from the Dart program).
But, since you are spawning another Dart program your should also consider using an Isolate which can execute both your own method on another thread or run external code by using:
https://api.dart.dev/stable/2.6.0/dart-isolate/Isolate/spawnUri.html
Related
I'm new to the Dart functions framework. My goal is to use this package to create several functions and deploy them to Cloud Run (in combination with Firebase, but I guess that's irrelevant to this question).
I've run the quick starts and I've read all of the contents in the docs.
The quick start mentions just one function at a time (e.g. Hello World, Cloud Events, etc..), like this:
import 'package:functions_framework/functions_framework.dart';
import 'package:shelf/shelf.dart';
#CloudFunction()
Response function(Request request) {
return Response.ok('Hello, World!');
}
But as you can see in the quickstarts only one function is handled in a project at a time. How about me wanting to deploy several functions? Should I:
Write several functions in the same project / file, so that the function framework compiles the 'server.dart` by itself
OR
Create a different functions_framework for each function?
Let me be more specific. Should I do the following (option 1 - which makes more sense to me):
import 'dart:math';
import 'package:functions_framework/functions_framework.dart';
import 'package:shelf/shelf.dart';
#CloudFunction()
Response function(Request request) {
return Response.ok('Hello, World!');
}
#CloudFunction()
Response function2(Request request) {
if (Random().nextBool()) {
return Response.ok('Hello, World!');
} else {
return Response.internalServerError();
}
}
Or should I build a different folder by running a build_runner for each function I need in my project?
Is there a difference and/or a best practice?
Thanks in advance.
EDIT. This question is related to the deployment on Cloud Run itself, and not just testing on my own PC. To test my own functions I did the following:
Run dart run build_runner build, so that it updates the server.dart file correctly (I can see that the framework does a lot behind the scenes and that the _nameToFunctionTarget is basically a router);
Run the server in two different terminals, like this: dart run bin/server.dart --port MYPORT --target MYFUNCTION (where MYPORT and MYFUNCTION are either 8080/8081 or function/function2 respectively).
I guess I'm just confused on how to correctly manage this framework once deployed on Cloud Run.
EDIT 2. I just gave up using Dart as a Serverless language or even a Backend language. There's just too much jargon even for the basic things. Any backend framework is either dead, or maintained by one single enthusiast guy (props to him!). This language has not yet received enough love from the Google Team / the community and at this moment in time is basically not possible to go fullstack on just Dart. It's a dream, but it can't be realized now. Furthermore, Dart hardly lacks a proper SDKs to use Firestore, etc., so Firebase isn't an option. I find it easier to just learn NodeJS and exploit the Firebase support for Firebase Functions written in NodeJS, and I'll wait for more support in there in the future, if there ever will be.
The documentation is a bit sparse right now (and I'm new to it also! I couldn't find any good examples, so here goes...)
You can only have a single function that is served. It should be
named 'function' (the type and name can be overriden, see the
cloudevent example dartfn generate cloudevent)
You 'could' have many of these deployed so that each does a specific thing, such as processing cloudevents above, but most people
want something more REST-like (see next)
You need to attach a Router() so that you can have the single entry point (function) handled by specific logic in your code.
Example for Rest
add to pubspec.yaml (in dependencies:) shelf_router: ^1.1.2
delegate the #CloudFunction to use the Router()
functions.dart
import 'package:functions_framework/functions_framework.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf_router/shelf_router.dart';
Router app = Router()
..get('/health', (Request request) {
return Response.ok('healthy');
})
..get('/user/<user>', (Request request, String user) {
// fetch the user... (probably return as json)
return Response.ok('hello $user');
})
..post('/user', (Request request) {
// convert request body to json and persist... (probably return as json)
return Response.ok('saved the user');
});
#CloudFunction()
Future<Response> function(Request request) => app.call(request);
I want to execute some processes one by one in dart using either Process.run() or Process.start()
And I want to show the verbose log of the process. But the problem is Whenever I am using run it's providing the stdout in a string i.e., at the end of the process completion. But I want to display the logs while the process is being executed.
Cause run is returning us the instance of ProcessResult instead of Process, the returned stdout is String.
Have to use stdout.write(cleanProcess.stdout); in case of run;
OR
Now in case start(), I am getting stdout as Stream which is desired but the only problem is the start returns us to the Process and Doesn't actually await for the Process to complete.
Can use stdout.addStream(buildProcess.stdout); to display stdout as stream
Code that I am trying to execute, and display the verbose.
print("HighSupply Executor : flutter clean");
var cleanProcess = await Process.run('flutter', ["clean", "--verbose"],
workingDirectory: "${flutterFolder.path}");
stdout.write(cleanProcess.stdout); //only getting stdout in one go. needed as stream.
print("HighSupply Executor : flutter pub get");
var pubGetProcess = await Process.run('flutter', //OR can we use start and wait for process to complete
["pub", "get", "--verbose"],
workingDirectory: "${flutterFolder.path}");
stdout.write(pubGetProcess.stdout);
print("HighSupply Executor : flutter build apk");
var buildProcess = await Process.run(
'flutter', ["build", "apk", "--verbose", generatedParams],
workingDirectory: "${flutterFolder.path}");
stdout.write(buildProcess.stdout);
TL;DR
Can I anyhow get stdout as a stream in case Prcocess.run() OR Can we wait for Process to complete before executing another in case of Process.start()
Any help is appreciated.
Edit: 1
As #julemand101 suggested to use exitcode and wait for it to return after the process completion, It works fine with my case, and haven't run into any issue yet but As he mentioned and stated in docs that is not reliable, so we are looking for a better concrete solution for it.
Source: Process
Process.start() is used to interact with the code. The future gives us a Process, after it has finished starting, but the Process is still running when we have the Process.
When the future completes the process is started and your code can interact with the process: writing to stdin, listening to stdout, and so on
Now we can access a Stream of integers as Process.stdout and parse it with a utf8 decoder.
Sample code:
main() async {
var process = await Process.start('cat', []);
process.stdout
.transform(utf8.decoder)
.forEach(print);
process.stdin.writeln('Hello, world!');
process.stdin.writeln('Hello, galaxy!');
process.stdin.writeln('Hello, universe!');
}
Out of the scope of this answer, we can manipulate the process objects, send interrupts, kill etc. and when the process ends, expect to get a done event on the stream.
I know this answer is too late, but I will leave it here maybe someone will find it helpful;
After playing around with the Process start() and run() methods, I found a way to achieve the required behavior of the original question by awaiting for addStream Future to complete.
void main() async {
final p = await Process.start('bash', ['-c', 'sleep 3']);
await stdout.addStream(p.stdout);
print('the end 😎');
}
Can we use graph database neo4j with react js? If not so is there any alternate option for including graph database in react JS?
Easily, all you need is neo4j-driver: https://www.npmjs.com/package/neo4j-driver
Here is the most simplistic usage:
neo4j.js
//import { v1 as neo4j } from 'neo4j-driver'
const neo4j = require('neo4j-driver').v1
const driver = neo4j.driver('bolt://localhost', neo4j.auth.basic('username', 'password'))
const session = driver.session()
session
.run(`
MATCH (n:Node)
RETURN n AS someName
`)
.then((results) => {
results.records.forEach((record) => console.log(record.get('someName')))
session.close()
driver.close()
})
It is best practice to close the session always after you get the data. It is inexpensive and lightweight.
It is best practice to only close the driver session once your program is done (like Mongo DB). You will see extreme errors if you close the driver at a bad time, which is incredibly important to note if you are beginner. You will see errors like 'connection to server closed', etc. In async code, for example, if you run a query and close the driver before the results are parsed, you will have a bad time.
You can see in my example that I close the driver after, but only to illustrate proper cleanup. If you run this code in a standalone JS file to test, you will see node.js hangs after the query and you need to press CTRL + C to exit. Adding driver.close() fixes that. Normally, the driver is not closed until the program exits/crashes, which is never in a Backend API, and not until the user logs out in the Frontend.
Knowing this now, you are off to a great start.
Remember, session.close() immediately every time, and be careful with the driver.close().
You could put this code in a React component or action creator easily and render the data.
You will find it no different than hooking up and working with Axios.
You can run statements in a transaction also, which is beneficial for writelocking affected nodes. You should research that thoroughly first, but transaction flow is like this:
const session = driver.session()
const tx = session.beginTransaction()
tx
.run(query)
.then(// same as normal)
.catch(// errors)
// the difference is you can chain multiple transactions:
const tx1 = await tx.run().then()
// use results
const tx2 = await tx.run().then()
// then, once you are ready to commit the changes:
if (results.good !== true) {
tx.rollback()
session.close()
throw error
}
await tx.commit()
session.close()
const finalResults = { tx1, tx2 }
return finalResults
// in my experience, you have to await tx.commit
// in async/await syntax conditions, otherwise it may not commit properly
// that operation is not instant
tl;dr;
Yes, you can!
You are mixing two different technologies together. Neo4j is graph database and React.js is framework for front-end.
You can connect to Neo4j from JavaScript - http://neo4j.com/developer/javascript/
Interesting topic. I am using the driver in a React App and recently experienced some issues. I am closing the session every time a lifecycle hook completes like in your example. When there where more intensive queries I would see a timeout error. Going back to my setup decided to experiment by closing the driver in some more expensive queries and it looks like (still need more testing) the crashes are gone.
If you are deploying a real-world application I would urge you to think about Authentication and Authorization when using a DB-React setup only as you would have to store username/password of the neo4j server in the client. I am looking into options of having the Neo4J server issuing a token and receiving it for Authorization but the best practice is for sure to have a Node.js server in the middle with something like Passport to handle Authentication.
So, all in all, maybe the best scenario is to only use the driver in Node and have the browser always communicating with the Node server using axios...
I'm using this snippet to read a data file in a unit test:
var file = new File('/Users/chambery/projects/Foo/src/resources/skills.yaml');
Future<String> finishedReading = file.readAsString();
finishedReading.then((text) {
print(text);
print(loadYaml(text));
});
Running in the Dart Editor I get no error (but no printout),
...
PASS: calc_ranks
PASS: load_skills
All 7 tests passed.
unittest-suite-success
(edit: removed command line error; dart vm was out-of-date)
I don't need async file read.
I'm guessing that you don't tell the unittest framework that your test is asynchronous. The framework will therefore not wait for your asynchronous tests to finish and assume that they passed.
Use expectAsyncX (where "X" is the number of arguments) to make sure that the framework waits for your asynchronous tests to finish.
See the unittest documentation: https://api.dartlang.org/docs/channels/stable/latest/unittest.html
If you are dealing with Futures, you can also use expect(future, completes).
I have a fairly lengthy command-line program that requires user input of parameters and then processes using those parameters. What I would like to do is split the program into interactive and non-interactive. I attempted to do that, and intended to have the non-interactive program "call" the interactive program and using the results (parameters), process based on those parameters. The non-interactive part of the program displays results on the console as it processes. I have looked at Process.run and Process.start, but apparently they don't function that way. There is another similar question to this that is about 12-months old, so I thought it worthwhile asking again.
I have looked at Process.run and Process.start, but apparently they don't function that way.
Process.start is what you want here. It can do what you want, but you'll have to become a bit more comfortable with async programming if you aren't already. You'll spawn the process and then asynchronously read and write to the spawned processes stdout and stdin streams.
Your interactive program can do something like this:
// interactive.dart
import 'dart:io';
main() {
var input = stdin.readLineSync();
print(input.toUpperCase());
}
It's using stdin to read input from the command line. Then it outputs the processed result using regular print().
The non-interactive script can spawn and drive that using something like:
import 'dart:convert';
import 'dart:io';
main() {
Process.start("dart", ["interactive.dart"]).then((process) {
process.stdin.writeln("this is the input");
UTF8.decoder.fuse(new LineSplitter()).bind(process.stdout).listen((line) {
print(line);
});
});
}
It uses Process.start to spawn the interactive script. It writes to it using process.stdin. To read the resulting output it has to jump through some hoops to convert the raw byte output to strings for each line, but this is the basic idea.