How to launch gvim reliably within a mojo app?

26 views
Skip to first unread message

Matthew Pressly

unread,
Mar 9, 2020, 8:46:03 PM3/9/20
to Mojolicious
From a mojolicious web app, I need to be able to launch gvim by clicking a link or button to edit a particular file.

Currently, I have this in the app:

get '/edit' => sub {
        my $c = shift;
        my $result = `/usr/bin/gvim $filename 2>&1`;
        $c->redirect_to('/');
};

Sometimes it works, but other times, gvim runs twice when the link is clicked and more frequently, gvim runs but hangs up in the background, after which I end up killing the gvim process and killing and restarting the web app to get it back to working.

Is there a way to launch gvim (or other interactive programs) more reliably? 

I'm using the following for a start script:

#!/usr/bin/perl
use warnings;
use strict;
use Daemon::Control;
 
exit Daemon::Control->new(
    name        => "My Application",
    lsb_start   => '$syslog $remote_fs',
    lsb_stop    => '$syslog',
    lsb_sdesc   => 'My App',
    lsb_desc    => 'Controls the time tracker service.',
    #path        => '/home/symkat/etc/init.d/program',
 
    program     => '/usr/bin/morbo',
    program_args => [ '/path/to/mojolicious/app.pl' ],
 
    pid_file    => '/tmp/myapp.pid',
    stderr_file => '/var/log/myapp/myapp.log',
    stdout_file => '/var/log/myapp/myapp.log',

    user        => 'myuser',
    group        => 'myuser',
 
    fork        => 2,
 
)->run;

Thank you,

--
Matthew 

Mike Lieman

unread,
Mar 9, 2020, 9:24:25 PM3/9/20
to mojol...@googlegroups.com
I don't think you want to use Daemon::Control.   How about a nice, simple fork()?

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mojolicious/8930b24b-4e37-4b56-82ed-fad82031fe44%40googlegroups.com.

Stefan Adams

unread,
Mar 9, 2020, 10:03:52 PM3/9/20
to mojolicious
Would Mojo::IOLoop->subprocess work for you?  Where the example shows sleep 5, put your system call to gvim?  Not sure if that's a good solution if gvim is a long-running process, tho.  Also, try it with `perl script daemon` as opposed to using `morbo`, maybe?  Or try the systemd setup for hypnotoad.  Is there any chance that Minion can help you here?  Minion is designed to handle long-running processes.

Matthew Pressly

unread,
Mar 9, 2020, 11:45:53 PM3/9/20
to mojol...@googlegroups.com

Using Mojo::IOLoop->subprocess is working for me. Here's what I have now (adapted from the example):

get '/edit' => sub {
        my $c = shift;

        Mojo::IOLoop->subprocess(
                sub {
                        my $subprocess = shift;
                        my $result = `/usr/bin/gvim $tc_file 2>&1`;
                        return;
                },
                sub {
                        my ($subprocess, $err, @results) = @_;
                        say "Subprocess error: $err" and return if $err;
                }
        );

        $c->redirect_to('/');
};

Thank you!

Reply all
Reply to author
Forward
0 new messages