Hello Everyone.
I have a long-running task that I have queued using the enqueue function.
Now, I want to send updates to the UI while the job is running in the background so as to notify the user what task is being executed.
I have tried using frappe.msgprint(message,alert=True)
but it only reflects before the job starts and never during execution.
Since the job is running in the background I can’t use frappe.set_alert in js.
Does anyone have any idea what approach can be used?
@Ebuka_Joseph_Akeru , you can send websocket messages!
The bench manager does exactly this, while running message commands!
In the backend:
The command is enqueued here
branch_name=branch_name, remote=remote
)
],
"commit": [
"git add .",
'git commit -m "{commit_msg}"'.format(commit_msg=commit_msg),
],
"stash": ["git add .", "git stash"],
"apply-stash": ["git stash apply"],
}
frappe.enqueue(
"bench_manager.bench_manager.utils.run_command",
commands=commands[caller],
cwd=os.path.join("..", "apps", self.name),
doctype=self.doctype,
key=key,
docname=self.name,
)
@frappe.whitelist()
The websocket message from the command is generated here
"key": key,
"source": doctype + ": " + docname,
"command": logged_command,
"console": console_dump,
"status": "Ongoing",
}
)
doc.insert()
frappe.db.commit()
frappe.publish_realtime(
key,
"Executing Command:\n{logged_command}\n\n".format(logged_command=logged_command),
user=frappe.session.user,
)
try:
for command in commands:
terminal = Popen(
shlex.split(command), stdin=PIPE, stdout=PIPE, stderr=STDOUT, cwd=cwd
)
for c in iter(lambda: safe_decode(terminal.stdout.read(1)), ""):
frappe.publish_realtime(key, c, user=frappe.session.user)
On the UI it’s listen here:
frappe._output_target = $('<pre class="console"><code></code></pre>')
.appendTo(dialog.get_field('console').wrapper)
.find('code')
.get(0);
frappe._output = '';
frappe._in_progress = false;
frappe._output_target.innerHTML = '';
dialog.show();
dialog.$wrapper.find('.modal-dialog').css('width', '800px');
frappe.realtime.on(key, function(output) {
if (output==='\r') {
// clear current line, means we are showing some kind of progress indicator
frappe._in_progress = true;
if(frappe._output_target.innerHTML != frappe._output) {
// progress updated... redraw
frappe._output_target.innerHTML = frappe._output;
}
frappe._output = frappe._output.split('\n').slice(0, -1).join('\n') + '\n';
return;
} else {
But the console on the UI is initiated here
if (frm.doc.version == undefined) $('div.form-inner-toolbar').hide();
else $('div.form-inner-toolbar').show();
let app_fields = ["app_title", "version", "app_description", "app_publisher", "app_email",
"app_icon", "app_color", "app_license"];
app_fields.forEach(function(app_field) {
frm.set_df_property(app_field, "read_only", frm.doc.__islocal ? 0 : 1);
});
if (frm.doc.is_git_repo != true) {
frm.add_custom_button(__("Git Init"), function(){
let key = frappe.datetime.get_datetime_as_string();
console_dialog(key);
frm.doc.is_git_repo = true;
frm.call("console_command", {
key: key,
caller: "git_init"
}, () => {
setTimeout(() => { frm.save(); }, 5000);
});
});
} else {
frm.add_custom_button(__('Commit'), function(){
Keep in mind, the key
it’s used as an topic, for you be able to watch these messages on the frontend!
2 Likes
@max_morais_dmm Thanks a lot for your help, it seems I will have to set up a listener in js for this to work, I will give it. a try and keep you updated, Many thanks again.