Skip to content

Commit

Permalink
All Inside
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramses II committed Jun 14, 2024
1 parent 87bb0b7 commit 3ceca35
Show file tree
Hide file tree
Showing 23 changed files with 7,456 additions and 4 deletions.
Binary file modified .DS_Store
Binary file not shown.
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
*.pyc
__pycache__/
job_manager.log
*.pyc
__pycache__/
job_manager.log
.DS_Store
.DS_Store
node_modules
/build
/.svelte-kit
/package
.env
.env.*
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
SECRET_CONFIG
.idea
!.env.ci
!.env
gcp-*.json
.venv
dist/
poetry.lock
__pycache__
/target
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use the official Python image from the Docker Hub
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME JobManager

# Run job_manager.py when the container launches
CMD ["python3", "job_manager.py", "run"]
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,58 @@
# Job Manager

A simple job management system to handle job queues with priorities, implemented in Python.

## Features

- Add jobs to a queue with priority
- Check the status of jobs
- Execute jobs in the queue based on priority
- Persist job states across restarts
- Run jobs in the background using `nohup` and `screen`

## Usage

### Adding a Job

```sh
python3 job_manager.py add "your_command_here" [priority]
```

### Checking Job Status

```sh
python3 job_manager.py status job_id
```

### Running Jobs

```sh
./run_jobs.sh
```

### Example

```sh
# Add a job with priority 10
python3 job_manager.py add "echo 'Hello, High Priority World!'" 10

# Check the status of the job
python3 job_manager.py status 1

# Run the job manager to process the queue
./run_jobs.sh
```

### Requirements

- Python 3.x
- `screen` and `nohup` installed

### Error Handling

- Errors are logged in the `context.txt` file.
- Jobs that fail are marked as `failed` and their status is persisted.

### License

This project is licensed under the MIT License.
3,541 changes: 3,541 additions & 0 deletions context.txt

Large diffs are not rendered by default.

44 changes: 40 additions & 4 deletions job_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import csv
import subprocess
from time import sleep
import json

QUEUE_FILE = '/Volumes/macOS-extravol/majinbo/job_queue.txt'
CONTEXT_FILE = '/Volumes/macOS-extravol/majinbo/context.txt'
STATE_FILE = '/Volumes/macOS-extravol/majinbo/job_state.json'

# Function to add a job to the queue with priority
def add_job(command, priority=1):
Expand Down Expand Up @@ -40,6 +42,7 @@ def check_job_status(job_id):

# Function to execute jobs in the queue based on priority
def execute_jobs():
restore_state()
while True:
stop_existing_jobs()
with open(QUEUE_FILE, 'r') as f:
Expand All @@ -59,10 +62,17 @@ def execute_jobs():
if is_job_running(command):
continue
update_job_status(job_id, 'running')
subprocess.Popen(f'screen -dmS job_{job_id} bash -c "{command}" && echo {job_id} completed', shell=True)
sleep(2)
update_job_status(job_id, 'completed')
update_context(f'Job {job_id} with command "{command}" has been completed')
save_state(job_id, 'running')
try:
subprocess.Popen(f'screen -dmS job_{job_id} bash -c "{command}" && echo {job_id} completed', shell=True)
sleep(2)
update_job_status(job_id, 'completed')
save_state(job_id, 'completed')
update_context(f'Job {job_id} with command "{command}" has been completed')
except Exception as e:
update_context(f'Error executing job {job_id} with command "{command}": {e}')
update_job_status(job_id, 'failed')
save_state(job_id, 'failed')
sleep(2)

# Function to check if a job with the same command is already running
Expand Down Expand Up @@ -92,6 +102,32 @@ def update_context(message):
with open(CONTEXT_FILE, 'a') as f:
f.write(f'CONTEXT: {message}\n')

# Function to save the state of jobs
def save_state(job_id, status):
state = {}
if os.path.exists(STATE_FILE):
with open(STATE_FILE, 'r') as f:
state = json.load(f)
state[job_id] = status
with open(STATE_FILE, 'w') as f:
json.dump(state, f)

# Function to restore the state of jobs
def restore_state():
if not os.path.exists(STATE_FILE):
return
with open(STATE_FILE, 'r') as f:
state = json.load(f)
with open(QUEUE_FILE, 'r') as f:
reader = csv.reader(f)
rows = list(reader)
with open(QUEUE_FILE, 'w') as f:
writer = csv.writer(f)
for row in rows:
if len(row) == 4 and row[0] in state: # Ensure row has the expected length
row[2] = state[row[0]]
writer.writerow(row)

if __name__ == '__main__':
import sys
if len(sys.argv) < 2:
Expand Down
1 change: 1 addition & 0 deletions serverwitch-api
Submodule serverwitch-api added at 5bb040
1 change: 1 addition & 0 deletions serverwitch-client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
Loading

0 comments on commit 3ceca35

Please sign in to comment.