CGRU / cgru

CGRU - AFANASY
http://cgru.info/
GNU Lesser General Public License v3.0
278 stars 111 forks source link

execute any command as JOB_DONE #564

Closed sebastianelsner closed 1 year ago

sebastianelsner commented 1 year ago

As far as I can see, currently in a JOB_DONE event you can only run "email" and "notify-send". It would be really nice if one could execute "any" command. A custom_data would then for example look like this:

{
            "emails": ["bbb@aaa.com"],
            "events": {
                "JOB_ERROR": {"methods": ["email"]},
                "JOB_DONE": {"methods": ["run_job_done"]},
            },
        }

Additionally the command should be able to "know something about the job it has come from".

Solutions: a) the command could receive at least the job id as a command line arg b) the command has access to new environment variables like, e.g. "AF_JOB_ID"

Taking option a), I would implement it like this:

diff --git a/afanasy/python/services/events.py b/afanasy/python/services/events.py
index 05253eec..8e287cc5 100644
--- a/afanasy/python/services/events.py
+++ b/afanasy/python/services/events.py
@@ -102,6 +102,11 @@ class events(service.service):
             if 'notify-send' in methods:
                 self.taskInfo['command'] = "notify-send Afanasy 'Job " + task_info['job_name'].replace("'", "'\\''") + ": " + event + "'"

+            # execute any command and give job ID
+            for m in methods:
+                if m not in ["email", "notify-send"]:
+                    self.taskInfo["command"] = "%s -jobid %s" % (m, objects['job']['id'])
+
         if len(email_events):
             cmd = cgruconfig.VARS['email_send_cmd']
             cmd += ' -V'  # Verbose mode

This variant errors if you just want to execute a command which does not accept "jobid" as a option, e.g. "rm -rf /path/to/some/file" (I know deletions are done in post_command, this is just an example)

What do you think, is this acceptable none the less?

eberrippe commented 1 year ago

@sebastianelsner , I like that Idea.

timurhai commented 1 year ago

Yes, it can be so. events.py class that provided with cgru considered just to be en example. With this extension it can became a workable version.