How can I get the username in the task script? - camunda

I have a task within a workflow that invokes a service on the back end that, along with other data sent from the task, waits for the name of the user who executed that task.
How can I get the user or the username in the task script which is written in Groovy?

You mean a script task following a user task? Assuming you would like to access the name of the user who completed the previous user task from your script, you can store the user name on completion in a process data as shown here:
https://github.com/rob2universe/bpmrun-add-to-dockerimg/blob/e2dd62122b7d006a8a719be96b0a3d043efc9851/configuration/resources/groovyprocess.bpmn#L23
You can then subsequently access the process data in the groovy script task like this:
https://github.com/rob2universe/bpmrun-add-to-dockerimg/blob/e2dd62122b7d006a8a719be96b0a3d043efc9851/configuration/resources/groovyprocess.bpmn#L18

You could either get the task assignee as mentioned by #rob2universe, or make use of the execution object, which is always accessible from the script (see Camunda Docs), as follows:
user = execution.getProcessEngineServices().getIdentityService().getCurrentAuthentication().getUserId()
See IdentityService.
The username can be then saved to a process variable:
execution.setVariable('username', user)
And accessed:
username = execution.getVariable('username')

Related

how to send mail automatically everyday when workflow completed successfully and also send an email when it run long

I want to receive an email when
1)The workflow will be successfully completed in time and a completion Notification will be sent to the respective person(e-mail id) or support group(distribution list) via the email task or the post-session e-mail notification in the session task.
The workflow will be continuing to run in excess of the expected time due to some issues (like network, data, etc).In such cases the “STILL RUNNING” notification will be sent to the respective person (e-mail id) or support group(distribution list) via the email task or the post-session e-mail notification in the session task.
For the scenario 1 just follow the guide linked by #rownum-highart.
For the scenario 2 use a parallel flow with a timer. Combine the two flows with Decision Task. Remember to use Treat the input links as set to OR. followed by Email Task with a condition of your session status.

Assigning JobObject to process with non-zero session id launched from a SYSTEM service

I have a Windows SYSTEM service which needs to launch a new process in the context of the logged-in user. Along with this, I need to create a Job for the new process with certain limits.
I am extracting the process token of explorer.exe and duplicating it to create a primary token. I use this token in CreateProcessAsUser to create the new process running in the context of the user with a session id which is non-zero. When I assign the Job to this process, AssignProcessToJobObject function fails with Access denied error. Specifically, I am not able to set JOBOBJECT_BASIC_UI_RESTRICTIONS limits (JOBOBJECT_EXTENDED_LIMIT_INFORMATION works though).
The process is created as suspended and after assigning the job, I am resuming the thread.
When I use the token of the current process (i.e. SYSTEM service with session id 0) instead of explorer.exe, everything works fine.
I am testing this on Windows 10

C++. Get logged in user name from Linux daemon

is there a way to get logged in user name from linux daemon?
I tried
seteuid(1000);
std::string userName = getlogin();
But seems that this call fails and my application is terminated after.
The general situation is following: some script that runs my daemon process. Inside of this daemon I start another UI process (lets call it A). Then in process A I'm trying to get logged in user name with way, described earlier. And my process A is terminated after getlogin call. Is there any reliable way to get logged in user name from process A?
getlogin() reads the login information from the utmp file, so it won't work if the process isn't associated with a terminal.
I assume from the seteuid call in your example that process A is running with the effective user ID of the original user.
If you don't have a terminal, but have the uid you need to use the "passwd" database (often, but not always, backed by the /etc/passwd file):
struct passwd *pw = getpwuid(geteuid());
string userName = pw->name;

How get the currently running activity instance of the process definition of camunda

I am a new for camunda.
I want cancel the currently running activity instance and start a new activity instance for move the token state.
But I got a hard time of how get the currently running activity instance id by the java api of camunda.
Any thougs ? Thank you all.
Actully the question is "How get the running activity instances". And I already got the answer from somewhere.
Here is the aswer.
Just use the java api like below
ActivityInstance activityInstance = runtimeService.getActivityInstance(instance.getProcessInstanceId());
ActivityInstance[] activityInstances = activityInstance.getChildActivityInstances();
The activityInstances array is the running activity instances. you can use the ids of the activity instances to cancel running activity instance.
Had the same trouble. This line returns a list of ids (whatever they are - user task, service task, and etc). If you don't have parallel active tasks - the list will contain a single activity id.
processEngine.getRuntimeService().getActiveActivityIds(
processInstance.getProcessInstanceId()
);

Stopping/Purging Periodic Tasks in Django-Celery

I have managed to get periodic tasks working in django-celery by subclassing PeriodicTask. I tried to create a test task and set it running doing something useless. It works.
Now I can't stop it. I've read the documentation and I cannot find out how to remove the task from the execution queue. I have tried using celeryctl and using the shell, but registry.tasks() is empty, so I can't see how to remove it.
I have seen suggestions that I should "revoke" it, but for this I appear to need a task id, and I can't see how I would find the task id.
Thanks.
A task is a message, and a "periodic task" sends task messages at periodic intervals. Each of the tasks sent will have an unique id assigned to it.
revoke will only cancel a single task message. To get the id for a task you have to keep
track of the id sent, but you can also specify a custom id when you send a task.
I'm not sure if you want to cancel a single task message, or if you want to stop the periodic task from sending more messages, so I'll list answers for both.
There is no built-in way to keep the id of a task sent with periodic tasks,
but you could set the id for each task to the name of the periodic task, that way
the id will refer to any task sent with the periodic task (usually the last one).
You can specify a custom id this way,
either with the #periodic_task decorator:
#periodic_task(options={"task_id": "my_periodic_task"})
def my_periodic_task():
pass
or with the CELERYBEAT_SCHEDULE setting:
CELERYBEAT_SCHEDULE = {name: {"task": task_name,
"options": {"task_id": name}}}
If you want to remove a periodic task you simply remove the #periodic_task from the codebase, or remove the entry from CELERYBEAT_SCHEDULE.
If you are using the Django database scheduler you have to remove the periodic task
from the Django Admin interface.
PS1: revoke doesn't stop a task that has already been started. It only cancels
tasks that haven't been started yet. You can terminate a running task using
revoke(task_id, terminate=True). By default this will send the TERM signal to
the process, if you want to send another signal (e.g. KILL) use
revoke(task_id, terminate=True, signal="KILL").
PS2: revoke is a remote control command so it is only supported by the RabbitMQ
and Redis broker transports.
If you want your task to support cancellation you should do so by storing a cancelled
flag in a database and have the task check that flag when it starts:
from celery.task import Task
class RevokeableTask(Task):
"""Task that can be revoked.
Example usage:
#task(base=RevokeableTask)
def mytask():
pass
"""
def __call__(self, *args, **kwargs):
if revoke_flag_set_in_db_for(self.request.id):
return
super(RevokeableTask, self).__call__(*args, **kwargs)
Just in case this may help someone ... We had the same problem at work, and despites some efforts to find some kind of management command to remove the periodic task, we could not. So here are some pointers.
You should probably first double-check which scheduler class you're using.
The default scheduler is celery.beat.PersistentScheduler, which is simply keeping track of the last run times in a local database file (a shelve).
In our case, we were using the djcelery.schedulers.DatabaseScheduler class.
django-celery also ships with a scheduler that stores the schedule in the Django database
Although the documentation does mention a way to remove the periodic tasks:
Using django-celery‘s scheduler you can add, modify and remove periodic tasks from the Django Admin.
We wanted to perform the removal programmatically, or via a (celery/management) command in a shell.
Since we could not find a command line, we used the django/python shell:
$ python manage.py shell
>>> from djcelery.models import PeriodicTask
>>> pt = PeriodicTask.objects.get(name='the_task_name')
>>> pt.delete()
I hope this helps!