diff --git a/lapis/drone.py b/lapis/drone.py
index 9826c78..7126242 100644
--- a/lapis/drone.py
+++ b/lapis/drone.py
@@ -154,9 +154,8 @@ class Drone(interfaces.Pool):
                 await instant
                 job_execution.cancel()
             self.jobs -= 1
-            if not job.successful:
-                job.drone = None
-                await self.scheduler.retry_job(job)
+            job.drone = None
+            await self.scheduler.job_finished(job)
             self._utilisation = self._allocation = None
             self.scheduler.update_drone(self)
             await sampling_required.put(self)
diff --git a/lapis/scheduler.py b/lapis/scheduler.py
index 7e9a42d..dc564b9 100644
--- a/lapis/scheduler.py
+++ b/lapis/scheduler.py
@@ -1,5 +1,5 @@
 from typing import Dict
-from usim import Scope, interval
+from usim import Scope, interval, Resources
 
 from lapis.drone import Drone
 from lapis.monitor import sampling_required
@@ -32,6 +32,7 @@ class CondorJobScheduler(object):
         self.interval = 60
         self.job_queue = JobQueue()
         self._collecting = True
+        self._processing = Resources(jobs=0)
 
     @property
     def drone_list(self):
@@ -100,19 +101,27 @@ class CondorJobScheduler(object):
                             for key, value in left_resources.items()
                         }
                         self._add_drone(best_match, left_resources)
-                if not self._collecting and not self.job_queue:
+                if (
+                    not self._collecting
+                    and not self.job_queue
+                    and self._processing.levels.jobs == 0
+                ):
                     break
                 await sampling_required.put(self)
 
     async def _collect_jobs(self):
         async for job in self._stream_queue:
             self.job_queue.append(job)
+            await self._processing.increase(jobs=1)
             # TODO: logging happens with each job
             await sampling_required.put(self.job_queue)
         self._collecting = False
 
-    async def retry_job(self, job):
-        await self._stream_queue.put(job)
+    async def job_finished(self, job):
+        if job.successful:
+            await self._processing.decrease(jobs=1)
+        else:
+            await self._stream_queue.put(job)
 
     def _schedule_job(self, job) -> Drone:
         priorities = {}
diff --git a/lapis/simulator.py b/lapis/simulator.py
index 9689ae2..2920202 100644
--- a/lapis/simulator.py
+++ b/lapis/simulator.py
@@ -32,6 +32,7 @@ class Simulator(object):
         self.cost = 0
         self._job_generators = []
         self.monitoring = None
+        self.duration = None
         self.enable_monitoring()
 
     def enable_monitoring(self):
@@ -77,7 +78,8 @@ class Simulator(object):
             for controller in self.controllers:
                 while_running.do(controller.run(), volatile=True)
             while_running.do(self.monitoring.run(), volatile=True)
-        print(f"Finished simulation at {time.now}")
+        self.duration = time.now
+        print(f"Finished simulation at {self.duration}")
 
     async def _queue_jobs(self, job_input, job_reader):
         await job_to_queue_scheduler(

Simulation doesn't await job execution after last scheduling cycle
I am under the impression that after the last update the simulation finishes after when the job queue is empty and all jobs are scheduled to drones but does not await the execution of all jobs in the drones job queue.

You can reproduce this by running lapis with the attached input files.

The simulation stops with the last scheduling cycle at 180 instead of processing the second job with a walltime of 1000.

I would expect the simulation to finish running all jobs.

Cluster input

    TotalSlotCPUs TotalSlotDisk TotalSlotMemory Count sitename
    1 44624348.0 8000 1 site1

Job Input

    [
    {
    "QDate": 1567155456,
    "RequestCpus": 1,
    "RequestWalltime": 60,
    "RequestMemory": 2000,
    "RequestDisk": 6000000,
    "RemoteWallClockTime": 100.0,
    "'Number of Allocated Processors'": 1,
    "MemoryUsage": 2867,
    "DiskUsage_RAW": 41898,
    "RemoteSysCpu": 10.0,
    "RemoteUserCpu": 40.0,
    "Inputfiles": {
    "a.root": {
    "filesize": 25,
    "usedsize": 20
    },
    "b.root": {
    "filesize": 25,
    "usedsize": 20
    },
    "c.root": {
    "filesize": 25,
    "usedsize": 20
    }
    }
    },
    {
    "QDate": 1567155456,
    "RequestCpus": 1,
    "RequestWalltime": 60,
    "RequestMemory": 2000,
    "RequestDisk": 6000000,
    "RemoteWallClockTime": 77.0,
    "'Number of Allocated Processors'": 1,
    "MemoryUsage": 1207,
    "DiskUsage_RAW": 45562,
    "RemoteSysCpu": 7.0,
    "RemoteUserCpu": 50.0,
    "Inputfiles": {
    "a.root": {
    "filesize": 25,
    "usedsize": 20
    },
    "b.root": {
    "filesize": 25,
    "usedsize": 20
    },
    "c.root": {
    "filesize": 25,
    "usedsize": 20
    }
    }
    },
    {
    "QDate": 1567155456,
    "RequestCpus": 1,
    "RequestWalltime": 60,
    "RequestMemory": 2000,
    "RequestDisk": 6000000,
    "RemoteWallClockTime": 63.0,
    "'Number of Allocated Processors'": 1,
    "MemoryUsage": 1207,
    "DiskUsage_RAW": 45562,
    "RemoteSysCpu": 3.0,
    "RemoteUserCpu": 40.0,
    "Inputfiles": {
    "a.root": {
    "filesize": 25,
    "usedsize": 20
    },
    "b.root": {
    "filesize": 25,
    "usedsize": 20
    },
    "c.root": {
    "filesize": 25,
    "usedsize": 20
    }
    }
    },
    {
    "QDate": 1567155456,
    "RequestCpus": 1,
    "RequestWalltime": 60,
    "RequestMemory": 2000,
    "RequestDisk": 6000000,
    "RemoteWallClockTime": 1000.0,
    "'Number of Allocated Processors'": 1,
    "MemoryUsage": 1207,
    "DiskUsage_RAW": 45562,
    "RemoteSysCpu": 7.0,
    "RemoteUserCpu": 40.3,
    "Inputfiles": {
    "a.root": {
    "filesize": 25,
    "usedsize": 20
    },
    "b.root": {
    "filesize": 25,
    "usedsize": 20
    },
    "c.root": {
    "filesize": 25,
    "usedsize": 20
    }
    }
    }
    ]

diff --git a/lapis_tests/test_simulator.py b/lapis_tests/test_simulator.py
new file mode 100644
index 0000000..4875d6b
--- /dev/null
+++ b/lapis_tests/test_simulator.py
@@ -0,0 +1,40 @@
+from tempfile import NamedTemporaryFile
+
+from lapis.job_io.htcondor import htcondor_job_reader
+from lapis.pool import StaticPool
+from lapis.pool_io.htcondor import htcondor_pool_reader
+from lapis.scheduler import CondorJobScheduler
+from lapis.simulator import Simulator
+
+
+class TestSimulator(object):
+    def test_simulation_exit(self):
+        simulator = Simulator()
+        with NamedTemporaryFile(suffix=".csv") as machine_config, NamedTemporaryFile(
+            suffix=".csv"
+        ) as job_config:
+            with open(machine_config.name, "w") as write_stream:
+                write_stream.write(
+                    "TotalSlotCPUs TotalSlotDisk TotalSlotMemory Count\n"
+                    "1 44624348.0 8000 1"
+                )
+            with open(job_config.name, "w") as write_stream:
+                write_stream.write(
+                    "QDate RequestCpus RequestWalltime RequestMemory RequestDisk "
+                    "RemoteWallClockTime MemoryUsage DiskUsage_RAW RemoteSysCpu "
+                    "RemoteUserCpu\n"
+                    "1567155456 1 60 2000 6000000 100.0 2867 41898 10.0 40.0"
+                )
+            job_input = open(job_config.name, "r+")
+            machine_input = open(machine_config.name, "r+")
+            simulator.create_job_generator(
+                job_input=job_input, job_reader=htcondor_job_reader
+            )
+            simulator.create_scheduler(scheduler_type=CondorJobScheduler)
+            simulator.create_pools(
+                pool_input=machine_input,
+                pool_reader=htcondor_pool_reader,
+                pool_type=StaticPool,
+            )
+            simulator.run()
+            assert 180 == simulator.duration

pip install -e .[dev]
parse_log_pytest
pytest
pytest
apt-get update apt-get install -y gcc
3.7
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
attrs @ file:///croot/attrs_1668696182826/work
certifi @ file:///croot/certifi_1671487769961/work/certifi
cfgv==3.3.1
click==8.1.8
cobald==0.13.0
distlib==0.3.9
entrypoints==0.4
exceptiongroup==1.2.2
filelock==3.12.2
flit_core @ file:///opt/conda/conda-bld/flit-core_1644941570762/work/source/flit_core
identify==2.5.24
idna==3.10
importlib-metadata==6.7.0
iniconfig @ file:///home/linux1/recipes/ci/iniconfig_1610983019677/work
-e git+https://github.com/MatterMiners/lapis.git@213a69da5c554bfadec160ed5246ba726d880760#egg=lapis_sim
nodeenv==1.9.1
outcome==1.3.0.post0
packaging @ file:///croot/packaging_1671697413597/work
platformdirs==4.0.0
pluggy @ file:///tmp/build/80754af9/pluggy_1648042572264/work
pre-commit==2.21.0
py @ file:///opt/conda/conda-bld/py_1644396412707/work
pytest==7.1.2
PyYAML==6.0.1
sniffio==1.3.1
sortedcontainers==2.4.0
tomli @ file:///opt/conda/conda-bld/tomli_1657175507142/work
toposort==1.10
trio==0.22.2
typing_extensions==4.7.1
usim==0.4.0
virtualenv==20.26.6
zipp @ file:///croot/zipp_1672387121353/work

name: lapis
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - attrs=22.1.0=py37h06a4308_0
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2022.12.7=py37h06a4308_0
  - flit-core=3.6.0=pyhd3eb1b0_0
  - importlib_metadata=4.11.3=hd3eb1b0_0
  - iniconfig=1.1.1=pyhd3eb1b0_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - packaging=22.0=py37h06a4308_0
  - pip=22.3.1=py37h06a4308_0
  - pluggy=1.0.0=py37h06a4308_1
  - py=1.11.0=pyhd3eb1b0_0
  - pytest=7.1.2=py37h06a4308_0
  - python=3.7.16=h7a1cb2a_0
  - readline=8.2=h5eee18b_0
  - setuptools=65.6.3=py37h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tomli=2.0.1=py37h06a4308_0
  - wheel=0.38.4=py37h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zipp=3.11.0=py37h06a4308_0
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - cfgv==3.3.1
      - click==8.1.8
      - cobald==0.13.0
      - distlib==0.3.9
      - entrypoints==0.4
      - exceptiongroup==1.2.2
      - filelock==3.12.2
      - identify==2.5.24
      - idna==3.10
      - importlib-metadata==6.7.0
      - lapis-sim==0.3.0
      - nodeenv==1.9.1
      - outcome==1.3.0.post0
      - platformdirs==4.0.0
      - pre-commit==2.21.0
      - pyyaml==6.0.1
      - sniffio==1.3.1
      - sortedcontainers==2.4.0
      - toposort==1.10
      - trio==0.22.2
      - typing-extensions==4.7.1
      - usim==0.4.0
      - virtualenv==20.26.6
prefix: /opt/conda/envs/lapis

lapis_tests/test_simulator.py::TestSimulator::test_simulation_exit
lapis/scheduler.py lapis/drone.py lapis/simulator.py
lapis/scheduler.py lapis/drone.py lapis/simulator.py