Are node.js module variables shared across multiple invocation? - variables

i m creating a node.js server, where i have a "notifications" module. Every request on the node server will invoke this module and i need to have a set of private variables for each separate invocation.
For example, when a request is made to notifications module, it needs to remember the user id, request time, session id, etc.
How can i do that efficiently?
If i simple declare variables in module scope, they seem to be shared for each module invocation. So, it fails in remembering every request's data privately.
What i need is each time i invoke a node.js module, it will remember its data at that time. So, please point out how can i do that?
Thanks,
Anjan
Update
The node.js server is to uses as a chat server. the "notifications" module will scan the db for new messages and send the output in json format to the client using long polling technique.
I tried to wrap the data and the functions into an object. Then each time a request is made to chat server a new object will be created and it will carry on the desired functions. But what it did is that instead of working in parallel it executes each request in serial. So, if i make 3 request to the server, they just queue up and executes one after another.
Any clue on that?
The module source code can be found here: http://www.ultrasoftbd.com/notifications.js
Thanks,
Anjan

There are a couple ways that come to mind to approach this issue:
1) Have your module export a constructor which can be called by the API users of your module to give them a new instance of an object. This way, each user will have its own object instance which has its own private variables.
// Client example.
var mod = require('myModule')
, sess = new mod.Session();
sess.method(args);
sess.finalize();
2) Have your module provide a "registry" interface to these private variables which includes an identifier unique to the caller.
// Client example.
var mod = require('myModule');
var id = mod.initialize(); // Returns a unique ID.
mod.method(id, args); // Each method requires the ID.
mod.finalize(id);
These solutions share the idea that each instance (or ID) is tracked separately by your module so that the statistics (or whatever your module does) can be computed per client instance rather than globally to the module.

Related

CKAN: Get user object or id when a context object is not available in extension

When writing a CKAN extension, I can create a custom GET-able method, which automatically receives the context.
e.g.
#side_effect_free
def custom_method(context, data_dict):
# Do something with the context and/or data_dict
The context argument above, which is basically injected by CKAN, contains, among other things, the user object which can be used to identify the user.
In other cases, like for example in a template helper, how can I get access to the user information? Ideally, I would like to have a context object just as above, so that I can call for example package_search and the rest of the actions provided in the toolkit.
Turns out that when calling actions, skipping the context variable will cause ckan to add it later inside the call, so there's no need to provide the context yourself. Also, if you want information on the user, the g variable from Flask has everything you need.

Is there a way to set up number of users for different thread in JMeter 5.3?

I am currently working on testing small API in our company and I need to randomly distribute a number of calls on all methods of that API. I am using 5.3 version of JMeter, company security politics, so we do not have newer versions if that matters.
Because number of methods is around 15 my idea right now is supply .properties file to JMeter, which will contain overall number of calls to API, then, through JSR223 sampler in SetUp Thread Group I will set properties with random amount of users in thread. However, I encountered a problem in doing so: I successfully set all properties, but I cannot access them, when calling ___property function in another Thread groups.
Is there any method to set those properties by script and access them through JMeter function?
Edit: Adding the code I am using in SetUp Thread Group to add properties
jmeter_properties.load(new FileInputStream(new File('env.properties')));
def allUsers = jmeter_properties.get('number.of.users') as Integer;
def random = new Random();
def thisUsers = random.nextInt(allUsers);
allUsers = allUsers - thisUsers;
props.put('getProjectById.users', thisUsers);```
I'm not sure whether it's expected behaviour or a bug in JMeter which should be reported via JMeter Bugzilla, but I do confirm that:
Your code is correct
The generated value cannot be referenced either by __P() or by __property() functions calls
However if you use __groovy() function the property is resolved just fine, so if you do something like:
${__groovy(props.get('getProjectById.users'),)}
in the 2nd (or whatever thread group) you will get the outcome you're looking for

Jmeter - How to use the variable set in first request for all the threads in one thread group

I would like to run one request only once, and get an authorization token from its response using Json extractor and then use that token as header in another request that runs under the same thread group.
I tried to use "setup Thread Group", but the variable value was not available to the main thread group.
So, I used "If Controller" under the same thread group, with below condition:
${__groovy(ctx.getThreadNum() == 0 && vars.getIteration() == 1,)}
This is making the specific request to be executed only once.
However, variable value is available only for one thread for the subsequent requests, but not for all the threads. Below is the picture of results tree:
May I know how to access the variable value set in first request for all the threads instead of just one thread?
As per JMeter Documentation:
Properties are not the same as variables. Variables are local to a thread; properties are common to all threads
So if you want to use a single token for all threads (virtual users) you need to convert it into a JMeter Property first like:
Under the If Controller use __setProperty() function to convert your variable into a property
In your GET request use __P() function to read the property value
Another way of sharing data between threads (even if they're in different thread groups) is using Inter-Thread Communication Plugin
You are not allowed to use the variable created in one thread in other threads this is how JMeter scoping for variables extracted works.
You already near to solution, providing steps so any one can approach problem like this:-
Use If Controller to make sure only once request being made to get Authorization token
Extract the token using post-processor
Save token in the property using post-processor so that same token can get used in multiple threads
Use newly created property instead of a variable in subsequent requests by referring property function instead of variable
You can use one JSR223 post-processor like below to create a property from the variable:
Please note that if you are mimicking multiple users using thread group, ideally you should create different auth token for different users.
P.S.: Balzemeter have an article which uses the BeanShell to demonstrate how to solve this problem

Is it possible to add additional logic to the Parse Server?

I want to perform some actions on X value depending on input from received in Y value. Can I perform such actions writing server side code in parse server?
Any pointers will be helpful.
Thanks.
Custom server side code can be achieved via cloud code. Cloud code allows you to create custom functions that are written in NodeJS and those functions can do various operations like: query from database, integrate with other solutions like: social, sending emails and more. The big advantage in parse-server is that you can use any npm module that you like from within the cloud code function and because there are millions of modules out there you have unlimited options.
Another very cool features of cloud code is the server side hooks
server side hooks allows you to write a code that will be triggered by parse-server core when an object is being saved or deleted. such events can be:
beforeSave - do something before the object is being saved to the database
afterSave - do something after the object is being saved
beforeDelete - do something before deleting
and more and more..
in order to define new cloud code function you will need to use the following code:
Parse.Cloud.define("{YOUR_FUNCTION_NAME}", function (request, response) {
// write your code, require some npm module and more...
});
In order to create server side hook you can write the following code:
Parse.Cloud.beforeSave("{PARSE_OBJECT_NAME}", function (request, response) {
// write your code and handle before saving an object
});
Triggering cloud code functions can be done easily via parse-server REST API or via parse-server client SDK's (iOS,Android,JavaScript and more)
There is a great guide on cloud code in here:
http://parseplatform.github.io/docs/cloudcode/guide/
Good Luck :)

How to find the Transport Request with my custom objects?

I've copied two Function Modules QM06_SEND_PAPER_STEP2 and QM06_FM_TASK_CLAIM_SEND_PAPER to similar Z* Function Modules. I've put these FMs into a ZQM06 Function Group which was created by another developer.
I want to use Transaction SCC1 to move my developments from one client to another. In transaction SE01 Transport Organizer I don't find the names of my 2 function modules anywhere.
How can I find out the change request with my work?
I copied the FM in order to modify functionality and I know FMs are client independent.
Function modules, like other ABAP workbench entities, are client-independent. That is, you do not need to copy them between clients on the same instance.
However, you can find the transport request that contains your changes by going to transaction SE37, entering the name of your function module, and then choosing Utilities -> Versions -> Version Management from the menu.
Provided you did not put the changes into a local package (like $TMP) the system will have asked you for a transport request when you saved or activated your changes, that is, unless the function group is in a modifiable transport request, in which case it would have created a new task for your user under that request which will contain you changes. To check the package, use Goto -> Object Directory Entry from the menu in SE37.
Function modules are often added to transports under the function group name, especially if they're new.
The easiest way to fin dhte transport is to go to SE37, display the function module, and then go to Version Management.
The answer from mydoghasworms is correct. Alternatively you can also use transaction SE03 -> Search for Objects in Requests/Tasks (top of the transaction screen) -> check the box next to "R3TR FUGR" and type in your function group name.