public static function store(AppNotice $notice) { $binary_notification = $notice->serialize(); $config = Config::instance(); $tube = Config::instance()->queuePrefix() . self::SEP . $notice->getApp() . self::SEP . date('Ymd', Time::now()); $try = $config->retries() + 1; $conns = $config->connections(); $keys = array_keys($conns); shuffle($keys); $ttr = 30; // $config->ttr(); $priority = 0; //floor( $notice->getExpires() - time::now() / 3600 ); $delay = 0; foreach ($keys as $key) { $conn = $conns[$key]; if (!$try--) { break; } $res = $conn->putInTube($tube, $binary_notification, $priority, $delay, $ttr); if (!$res) { continue; } return $conn->hostInfo() . '-' . $res; } throw new Exception('storage error', $conns); }
public function replace($k, $v, $expires = 0) { if ($expires > Wrap::TTL_30_DAYS) { $expires -= Time::now(); } if ($this->core) { return $this->core->replace($k, $v, $expires); } if (!$this->get($k)) { return FALSE; } return $this->set($k, $v, $expires); }
/** * returns the current time. */ public static function now() { return \Gaia\Time::now(); }
protected function ttl($ttl) { if ($ttl < 1) { return '4294967295'; } elseif ($ttl < Time::now() + Wrap::TTL_30_DAYS) { return Time::now() + $ttl; } }
public function flushOld($pattern = '*') { return $this->flush($pattern, NULL, Time::now() - Job::config()->ttl()); }
Tap::is($listing->bid, 3, 'a fresh read also confirms it'); $e = NULL; try { $listing = $souk->bid($listing->id, 3); } catch (Exception $e) { $e = $e->getMessage(); } Tap::is($e, 'too low', 'when under-bidding, fails with message: too low'); DB\Transaction::reset(); DB\Connection::reset(); $listing = $souk->close($listing->id); Tap::is($listing->bid, 3, 'after closing, bid is 3'); Tap::is($listing->price, 10, 'after closing, price is 10 ... wasnt met'); Tap::is($listing->closed, 1, 'auction is closed'); Tap::is($listing->buyer, $listing->bidder, 'bidder is now the buyer'); Tap::cmp_ok(abs($listing->touch - Time::now()), '<', 2, 'after closing, touch matches current time'); DB\Transaction::reset(); DB\Connection::reset(); $e = NULL; try { $listing = $souk->close($listing->id); } catch (Exception $e) { $e = $e->getMessage(); } Tap::is($e, 'already closed', 'when trying to close an already closed listing, fails with message: already closed'); DB\Transaction::reset(); DB\Connection::reset(); $e = NULL; try { $listing = $souk->bid($listing->id, 4); } catch (Exception $e) {
Tap::is(print_r($res, TRUE), print_r($expected, TRUE), 'search sort by low_price returns results sorted by price from low to high'); $res = array(); foreach (souk($app)->fetch(souk($app)->search(array('sort' => 'high_price', 'item_id' => $item_id, 'seller' => $seller_id))) as $id => $listing) { $res[] = $listing->price; } $expected = $res; rsort($expected, SORT_NUMERIC); Tap::is(print_r($res, TRUE), print_r($expected, TRUE), 'search sort by high_price returns results sorted by price from high to low'); $res = array(); foreach (souk($app)->fetch(souk($app)->search(array('sort' => 'expires_soon_delay', 'item_id' => $item_id, 'seller' => $seller_id))) as $id => $listing) { $res[] = $listing->expires; } $expected = $res; sort($expected, SORT_NUMERIC); Tap::is($res, $expected, 'search sort by expired_soon_delay sorted by expires'); Tap::cmp_ok(min($res), '>', Time::now() + 500, 'all of the results expire after now by more than 500 secs'); $res = array(); foreach (souk($app)->fetch(souk($app)->search(array('item_id' => $item_id, 'seller' => $seller_id, 'floor' => 4, 'ceiling' => 6))) as $id => $listing) { $res[$listing->price] = TRUE; } $res = array_keys($res); sort($res); Tap::is($res, array(4, 5, 6), 'setting floor and ceiling limits the result set to prices in the correct range'); $search = souk($app)->search(array('item_id' => $item_id, 'seller' => $seller_id, 'closed' => 0)); $id = array_shift($search); souk($app)->close($id); $ids = souk($app)->search(array('item_id' => $item_id, 'seller' => $seller_id, 'closed' => 0)); Tap::ok(!in_array($id, $ids, TRUE), 'after closing an item, it no longer appears in the list of unclosed items'); $res = souk($app)->fetch(souk($app)->search(array('item_id' => $item_id, 'seller' => $seller_id, 'closed' => 0, 'only' => 'bid'))); $bidonly = TRUE; foreach ($res as $listing) {
public static function currentShard() { return date('Ym', Time::now()); }
public function flushOld($pattern = '*') { $days = (int) Config::instance()->get('expired_days'); if ($days < 1) { $days = 1; } $ttl = 86400 * $days; return $this->flush($pattern, NULL, Time::now() - $ttl); }
/** * mark the job as failed */ public function fail() { if ($this->expires < Time::now()) { return $this->remove(); } if (!$this->id) { return FALSE; } list($server, $id) = explode('-', $this->id, 2); if (!$server) { return FALSE; } $conns = self::config()->connections(); if (!isset($conns[$server])) { return false; } $conn = $conns[$server]; $res = $conn->release(new \Pheanstalk_Job($id, ''), $this->priority, $this->ttr + 300); if (!$res) { throw new Exception('conn error', $conn); } return $res; }
protected function calculateShort() { list($value, $time) = $this->storage; $elapsed_time = Time::now() - $time; if ($elapsed_time < $this->options->short) { return $this->options->short - $elapsed_time; } return 0; }
protected static function time() { return Time::now(); }
protected function _isValid($k, $v) { if ($v === NULL) { return FALSE; } if (!is_array($v)) { $this->core->delete($k); return FALSE; } $ttl = $v[1]; if ($ttl == 0) { return TRUE; } if ($ttl < Time::now()) { $this->core->delete($k); return FALSE; } return TRUE; }
function replace($k, $v, $expire = 0) { if (!$this->core->replace($k . '/__lock/', 1, $expire)) { return FALSE; } if (!$expire) { $expire = self::DEFAULT_TTL; } $this->core->set($k . '/__exp/', Time::now() + $expire); return $this->core->set($k, $v); }