Salome HOME
ebc9b83bb53ff86834cd651d89db8b0c2b06c3d5
[modules/kernel.git] / src / Launcher / Test / launcher_use_case.py
1 #! /usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2018-2023  CEA, EDF
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21
22 # classic use case of a job
23 import os
24 import salome
25 import tempfile
26 import time
27 import sys
28
29 if __name__ == '__main__':
30   salome.standalone()
31   salome.salome_init()
32   launcher = salome.naming_service.Resolve('/SalomeLauncher')
33   job_params = salome.JobParameters()
34   job_params.resource_required = salome.ResourceParameters()
35   job_params.resource_required.name = "localhost"
36   job_params.resource_required.nb_proc = 1 # slurm: --ntasks
37
38   job_params.job_type = "command"
39   #cwd = os.getcwd()
40   cwd = sys.argv[1]
41   case_dir = tempfile.mkdtemp(prefix="test")
42   job_params.local_directory = cwd
43   job_params.job_file = "command.sh"
44   job_params.work_directory = os.path.join(case_dir, "run")
45   job_params.result_directory = os.path.join(case_dir, "result")
46   job_params.out_files = ["result.txt"]
47   job_params.wckey="P11U5:CARBONES"
48   job_params.job_name = "MyJob"
49   job_id = launcher.createJob(job_params)
50   launcher.launchJob(job_id)
51   job_string = launcher.dumpJob(job_id)
52   jobState = launcher.getJobState(job_id)
53   while jobState != 'FINISHED' and jobState != 'FAILED':
54       time.sleep(1)
55       jobState = launcher.getJobState(job_id)
56   launcher.getJobResults(job_id, '')
57   launcher.getJobWorkFile(job_id, "result.txt", '')
58   launcher.clearJobWorkingDir(job_id)
59   launcher.removeJob(job_id)
60   new_id = launcher.restoreJob(job_string)
61   job_params_bis = launcher.getJobParameters(new_id)
62   jobState = launcher.getJobState(new_id)
63   launcher.removeJob(new_id)
64   launcher.saveJobs(os.path.join(case_dir,"savejobs.xml"))