def submit_job(self, my_study: StudyDTO):
        """Submits the Antares job to slurm

        Args:
            my_study: The study data transfer object

        Returns:
            The slurm job id if the study has been submitted

        Raises:
            SubmitJobErrorException if the job has not been successfully submitted
        """
        time_limit = self.convert_time_limit_from_seconds_to_minutes(
            my_study.time_limit)
        script_params = ScriptParametersDTO(
            study_dir_name=Path(my_study.path).name,
            input_zipfile_name=Path(my_study.zipfile_path).name,
            time_limit=time_limit,
            n_cpu=my_study.n_cpu,
            antares_version=my_study.antares_version,
            run_mode=my_study.run_mode,
            post_processing=my_study.post_processing,
        )
        command = self.compose_launch_command(script_params)
        output, error = self.connection.execute_command(command)
        if error:
            raise SubmitJobErrorException
        job_id = None
        # squeue SLURM command returns f'Submitted {job_id}' if successful
        stdout_list = str(output).split()
        if stdout_list and stdout_list[0] == "Submitted":
            job_id = int(stdout_list[-1])
        return job_id
 def test_compose_launch_command(self, my_remote_env_with_slurm_mock,
                                 job_type, mode, post_processing, study):
     # given
     filename_launch_script = (my_remote_env_with_slurm_mock.
                               slurm_script_features.solver_script_path)
     # when
     study.run_mode = mode
     study.post_processing = post_processing
     script_params = ScriptParametersDTO(
         study_dir_name=Path(study.path).name,
         input_zipfile_name=Path(study.zipfile_path).name,
         time_limit=1,
         n_cpu=study.n_cpu,
         antares_version=study.antares_version,
         run_mode=study.run_mode,
         post_processing=study.post_processing,
     )
     command = my_remote_env_with_slurm_mock.compose_launch_command(
         script_params)
     # then
     change_dir = f"cd {my_remote_env_with_slurm_mock.remote_base_path}"
     reference_submit_command = f'sbatch --job-name="{Path(study.path).name}" --time={study.time_limit//60} --cpus-per-task={study.n_cpu} {filename_launch_script} "{Path(study.zipfile_path).name}" {study.antares_version} {job_type} {post_processing}'
     reference_command = change_dir + " && " + reference_submit_command
     assert command.split() == reference_command.split()
     assert command == reference_command
 def test_given_study_with_xpansion_mode_when_compose_launch_command_is_called_then_use_xpansion_script(
         self, my_remote_env_with_slurm_mock, study):
     # given
     study.run_mode = Modes.xpansion
     # when
     script_params = ScriptParametersDTO(
         study_dir_name=Path(study.path).name,
         input_zipfile_name=Path(study.zipfile_path).name,
         time_limit=10,
         n_cpu=study.n_cpu,
         antares_version=study.antares_version,
         run_mode=study.run_mode,
         post_processing=study.post_processing,
     )
     command = my_remote_env_with_slurm_mock.compose_launch_command(
         script_params)
     # then
     assert ("ANTARES_XPANSION" in command) is True
 def test_when_submit_job_is_called_then_execute_command_is_called_with_specific_slurm_command(
         self, my_remote_env_with_slurm_mock, study):
     # when
     output = "output"
     error = None
     my_remote_env_with_slurm_mock.connection.execute_command = mock.Mock(
         return_value=(output, error))
     my_remote_env_with_slurm_mock.submit_job(study)
     # then
     script_params = ScriptParametersDTO(
         study_dir_name=Path(study.path).name,
         input_zipfile_name=Path(study.zipfile_path).name,
         time_limit=60 // 60,
         n_cpu=study.n_cpu,
         antares_version=study.antares_version,
         run_mode=study.run_mode,
         post_processing=study.post_processing,
     )
     command = (my_remote_env_with_slurm_mock.slurm_script_features.
                compose_launch_command(
                    my_remote_env_with_slurm_mock.remote_base_path,
                    script_params))
     my_remote_env_with_slurm_mock.connection.execute_command.assert_called_with(
         command)