|
| 1 | +/*___INFO__MARK_BEGIN__*/ |
| 2 | +/************************************************************************* |
| 3 | +* Copyright 2025 HPC-Gridware GmbH |
| 4 | +* |
| 5 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +* you may not use this file except in compliance with the License. |
| 7 | +* You may obtain a copy of the License at |
| 8 | +* |
| 9 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +* |
| 11 | +* Unless required by applicable law or agreed to in writing, software |
| 12 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +* See the License for the specific language governing permissions and |
| 15 | +* limitations under the License. |
| 16 | +* |
| 17 | +************************************************************************/ |
| 18 | +/*___INFO__MARK_END__*/ |
| 19 | + |
| 20 | +package accounting |
| 21 | + |
| 22 | +import ( |
| 23 | + "fmt" |
| 24 | + "os" |
| 25 | + "path/filepath" |
| 26 | +) |
| 27 | + |
| 28 | +// Record is a key-value pair representing an accounting record. |
| 29 | +type Record struct { |
| 30 | + AccountingKey string |
| 31 | + AccountingValue int |
| 32 | +} |
| 33 | + |
| 34 | +// AppendToAccounting appends accounting records to the usage file so |
| 35 | +// that it gets send to the execution daemon. Typically sgeadmin user |
| 36 | +// (Cluster Scheduler install user). |
| 37 | +// Hence you need to prefix your epilog script with sgeadmin@/path/to/epilog. |
| 38 | +func AppendToAccounting(usageFilePath string, records []Record) error { |
| 39 | + f, err := os.OpenFile(usageFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + defer f.Close() |
| 44 | + |
| 45 | + var nl string |
| 46 | + |
| 47 | + for _, record := range records { |
| 48 | + nl += fmt.Sprintf("%s=%d\n", |
| 49 | + record.AccountingKey, |
| 50 | + record.AccountingValue) |
| 51 | + } |
| 52 | + |
| 53 | + _, err = f.WriteString(nl) |
| 54 | + return err |
| 55 | +} |
| 56 | + |
| 57 | +// GetUsageFilePath returns the path to the job usage file in the |
| 58 | +// job spool directory. |
| 59 | +func GetUsageFilePath() (string, error) { |
| 60 | + jobSpoolDir := os.Getenv("SGE_JOB_SPOOL_DIR") |
| 61 | + if jobSpoolDir == "" { |
| 62 | + return "", fmt.Errorf("SGE_JOB_SPOOL_DIR is not set") |
| 63 | + } |
| 64 | + return filepath.Join(jobSpoolDir, "usage"), nil |
| 65 | +} |
0 commit comments