예제 #1
0
 protected function doWork()
 {
     $data = $this->getTaskData();
     $lease = id(new DrydockLease())->loadOneWhere('id = %d', $data['lease']);
     if (!$lease) {
         return;
     }
     $type = $data['type'];
     $candidates = id(new DrydockResource())->loadAllWhere('type = %s AND status = %s', $type, DrydockResourceStatus::STATUS_OPEN);
     if ($candidates) {
         shuffle($candidates);
         $resource = head($candidates);
     } else {
         $blueprints = DrydockBlueprint::getAllBlueprintsForResource($type);
         foreach ($blueprints as $key => $blueprint) {
             if (!$blueprint->canAllocateResources()) {
                 unset($blueprints[$key]);
                 continue;
             }
         }
         if (!$blueprints) {
             $lease->setStatus(DrydockLeaseStatus::STATUS_BROKEN);
             $lease->save();
             DrydockBlueprint::writeLog(null, $lease, "There are no resources of type '{$type}' available, and no " . "blueprints which can allocate new ones.");
             return;
         }
         // TODO: Rank intelligently.
         shuffle($blueprints);
         $blueprint = head($blueprints);
         if (isset($data['synchronous'])) {
             $blueprint->makeSynchronous();
         }
         $resource = $blueprint->allocateResource();
     }
     $blueprint = $resource->getBlueprint();
     $blueprint->acquireLease($resource, $lease);
 }