/**
  * Get all the currencies we want to create block counts for.
  */
 function getCurrencies()
 {
     return \DiscoveredComponents\Currencies::getDifficultyCurrencies();
 }
 function run(Connection $db, Logger $logger)
 {
     $job = $this->job;
     switch ($job['job_type']) {
         // average jobs
         case "average":
             require __DIR__ . "/../jobs/average.php";
             break;
             // account jobs
         // account jobs
         case "generic":
             require __DIR__ . "/../jobs/generic.php";
             break;
         case "cryptostocks":
             require __DIR__ . "/../jobs/cryptostocks.php";
             break;
         case "securities_cryptostocks":
             require __DIR__ . "/../jobs/securities_cryptostocks.php";
             break;
         case "havelock":
             require __DIR__ . "/../jobs/havelock.php";
             break;
         case "securities_havelock":
             require __DIR__ . "/../jobs/securities_havelock.php";
             break;
         case "securities_crypto-trade":
             require __DIR__ . "/../jobs/securities_cryptotrade.php";
             break;
         case "796":
             require __DIR__ . "/../jobs/796.php";
             break;
         case "securities_796":
             require __DIR__ . "/../jobs/securities_796.php";
             break;
         case "litecoininvest":
             require __DIR__ . "/../jobs/litecoininvest.php";
             break;
             // individual securities jobs
         // individual securities jobs
         case "individual_cryptostocks":
             require __DIR__ . "/../jobs/individual_cryptostocks.php";
             break;
         case "individual_havelock":
             require __DIR__ . "/../jobs/individual_havelock.php";
             break;
         case "individual_crypto-trade":
             require __DIR__ . "/../jobs/individual_crypto-trade.php";
             break;
         case "individual_796":
             require __DIR__ . "/../jobs/individual_796.php";
             break;
         case "individual_litecoininvest":
             require __DIR__ . "/../jobs/individual_litecoininvest.php";
             break;
             // summary jobs
         // summary jobs
         case "sum":
             require __DIR__ . "/../jobs/sum.php";
             break;
         case "securities_count":
             require __DIR__ . "/../jobs/securities_count.php";
             break;
             // notification jobs
         // notification jobs
         case "notification":
             require __DIR__ . "/../jobs/notification.php";
             break;
             // system jobs
         // system jobs
         case "securities_update":
             require __DIR__ . "/../jobs/securities_update.php";
             break;
         case "version_check":
             require __DIR__ . "/../jobs/version_check.php";
             break;
         case "vote_coins":
             require __DIR__ . "/../jobs/vote_coins.php";
             break;
         case "missing_average_find":
             require __DIR__ . "/../jobs/missing_average_find.php";
             break;
         case "missing_average":
             require __DIR__ . "/../jobs/missing_average.php";
             break;
             // transaction jobs
         // transaction jobs
         case "transaction_creator":
             require __DIR__ . "/../jobs/transaction_creator.php";
             break;
         case "transactions":
             require __DIR__ . "/../jobs/transactions.php";
             break;
             // cleanup jobs, admin jobs etc
         // cleanup jobs, admin jobs etc
         case "outstanding":
             require __DIR__ . "/../jobs/outstanding.php";
             break;
         case "expiring":
             require __DIR__ . "/../jobs/expiring.php";
             break;
         case "expire":
             require __DIR__ . "/../jobs/expire.php";
             break;
         case "cleanup":
             require __DIR__ . "/../jobs/cleanup.php";
             break;
         case "disable_warning":
             require __DIR__ . "/../jobs/disable_warning.php";
             break;
         case "disable":
             require __DIR__ . "/../jobs/disable.php";
             break;
         case "delete_user":
             require __DIR__ . "/../jobs/delete_user.php";
             break;
         default:
             if (substr($job['job_type'], 0, strlen("address_")) === "address_") {
                 // address job
                 $currency = substr($job['job_type'], strlen("address_"));
                 if (!in_array($currency, get_address_currencies())) {
                     throw new JobException("Currency {$currency} is not a valid address currency");
                 }
                 if (in_array($currency, \DiscoveredComponents\Currencies::getBalanceCurrencies())) {
                     require __DIR__ . "/../jobs/addresses/discovered.php";
                 } else {
                     // TODO eventually remove this block once we have no currencies that are also in getBalanceCurrencies()
                     if (!file_exists(__DIR__ . "/../jobs/addresses/" . safe_include_arg($currency) . ".php")) {
                         throw new JobException("Could not find any addresses/{$currency}.php include");
                     }
                     require __DIR__ . "/../jobs/addresses/" . safe_include_arg($currency) . ".php";
                 }
                 break;
             }
             if (substr($job['job_type'], 0, strlen("blockcount_")) === "blockcount_") {
                 // address job
                 $currency = substr($job['job_type'], strlen("blockcount_"));
                 if (!in_array($currency, \DiscoveredComponents\Currencies::getBlockCurrencies())) {
                     throw new JobException("Currency {$currency} is not a valid block currency");
                 }
                 require __DIR__ . "/../jobs/blockcount/discovered.php";
                 break;
             }
             if (substr($job['job_type'], 0, strlen("difficulty_")) === "difficulty_") {
                 // address job
                 $currency = substr($job['job_type'], strlen("difficulty_"));
                 if (!in_array($currency, \DiscoveredComponents\Currencies::getDifficultyCurrencies())) {
                     throw new JobException("Currency {$currency} is not a valid difficulty currency");
                 }
                 require __DIR__ . "/../jobs/difficulty/discovered.php";
                 break;
             }
             if (substr($job['job_type'], 0, strlen("markets_")) === "markets_") {
                 // address job
                 $exchange = substr($job['job_type'], strlen("markets_"));
                 if (!in_array($exchange, \DiscoveredComponents\Exchanges::getKeys())) {
                     throw new JobException("Exchange {$exchange} is not a valid exchange");
                 }
                 require __DIR__ . "/../jobs/markets/discovered.php";
                 break;
             }
             if (substr($job['job_type'], 0, strlen("ticker_")) === "ticker_") {
                 // address job
                 $exchange = substr($job['job_type'], strlen("ticker_"));
                 if (!in_array($exchange, \DiscoveredComponents\Exchanges::getKeys())) {
                     throw new JobException("Exchange {$exchange} is not a valid exchange");
                 }
                 require __DIR__ . "/../jobs/ticker/discovered.php";
                 break;
             }
             if (substr($job['job_type'], 0, strlen("currencies_")) === "currencies_") {
                 // address job
                 $exchange = substr($job['job_type'], strlen("currencies_"));
                 if (!in_array($exchange, \DiscoveredComponents\Accounts::getKeys())) {
                     throw new JobException("Account {$exchange} is not a valid account");
                 }
                 require __DIR__ . "/../jobs/currencies/discovered.php";
                 break;
             }
             if (substr($job['job_type'], 0, strlen("hashrates_")) === "hashrates_") {
                 // address job
                 $exchange = substr($job['job_type'], strlen("hashrates_"));
                 if (!in_array($exchange, \DiscoveredComponents\Accounts::getMiners())) {
                     throw new JobException("Account {$exchange} is not a valid miner");
                 }
                 require __DIR__ . "/../jobs/hashrates/discovered.php";
                 break;
             }
             if (substr($job['job_type'], 0, strlen("account_")) === "account_") {
                 // address job
                 $exchange = substr($job['job_type'], strlen("account_"));
                 if (!in_array($exchange, \DiscoveredComponents\Accounts::getKeys())) {
                     throw new JobException("Account {$exchange} is not a valid account");
                 }
                 require __DIR__ . "/../jobs/account/discovered.php";
                 break;
             }
             throw new JobException("Unknown job type '" . htmlspecialchars($job['job_type']) . "'");
     }
 }
 /**
  * Get a list of all jobs that need to be queued, as an array of associative
  * arrays with (job_type, arg_id, [user_id]).
  *
  * This could use e.g. {@link JobTypeFinder}
  */
 function findJobs(Connection $db, Logger $logger)
 {
     $standard_jobs = self::getStandardJobs();
     $logger->info("Current time: " . date('r'));
     // get all disabled users
     $disabled = array();
     $q = $db->prepare("SELECT * FROM user_properties WHERE is_disabled=1");
     $q->execute();
     while ($d = $q->fetch()) {
         $disabled[$d['id']] = $d;
     }
     foreach (array(true, false) as $is_premium_only) {
         $job_count = 0;
         foreach ($standard_jobs as $standard) {
             $always = isset($standard['always']) && $standard['always'];
             $field = isset($standard['user_id_field']) ? $standard['user_id_field'] : 'user_id';
             $query_extra = isset($standard['query']) ? $standard['query'] : "";
             $args_extra = isset($standard['args']) ? $standard['args'] : array();
             if (isset($standard['failure']) && $standard['failure']) {
                 $query_extra .= " AND is_disabled=0";
             }
             $args = array();
             if (!$always) {
                 // we want to run system jobs at least every 0.1 hours = 6 minutes
                 $args[] = isset($standard['hours']) ? $standard['hours'] : (isset($standard['user_id']) && $standard['user_id'] == get_site_config('system_user_id') ? get_site_config('refresh_queue_hours_system') : ($is_premium_only ? get_site_config('refresh_queue_hours_premium') : get_site_config('refresh_queue_hours')));
             }
             $queue_field = isset($standard['queue_field']) ? $standard['queue_field'] : 'last_queue';
             if ($is_premium_only && (!isset($standard['user_id']) || $standard['user_id'] != get_site_config('system_user_id'))) {
                 $query_extra .= " AND {$field} IN (SELECT id FROM user_properties WHERE is_premium=1)";
             }
             // multiply queue_hours by 0.8 to ensure that user jobs are always executed within the specified timeframe
             try {
                 $q = $db->prepare("SELECT * FROM " . $standard['table'] . " WHERE " . ($always ? "1" : "({$queue_field} <= DATE_SUB(NOW(), INTERVAL (? * 0.8) HOUR) OR ISNULL({$queue_field}))") . " {$query_extra}");
                 $q->execute(array_join($args, $args_extra));
             } catch (\PdoException $e) {
                 throw new \Exception("Could not find jobs for table '" . $standard['table'] . "': " . $e->getMessage(), $e->getCode(), $e);
             }
             $disabled_count = 0;
             while ($address = $q->fetch()) {
                 $job = array("job_type" => $standard['type'], "user_id" => isset($standard['user_id']) ? $standard['user_id'] : $address[$field], "arg_id" => $address['id'], "queue_field" => $queue_field, "object" => $address, "table" => $standard['table']);
                 // check that this user is not disabled
                 if (isset($disabled[$job['user_id']])) {
                     if ($disabled_count == 0) {
                         $logger->info("Skipping job '" . $standard['type'] . "' for user " . $job['user_id'] . ": user is disabled");
                     }
                     $disabled_count++;
                     continue;
                 }
                 $result[] = $job;
                 $job_count++;
             }
             if ($disabled_count > 1) {
                 $logger->info("Also skipped another " . number_format($disabled_count) . " " . $standard['type'] . " jobs due to disabled users");
             }
         }
         $logger->info($is_premium_only ? "Found {$job_count} premium jobs" : "Found {$job_count} general user jobs");
     }
     $block_jobs = array('version_check', 'vote_coins', 'average', 'missing_average_find');
     foreach ($block_jobs as $name) {
         // as often as we can (or on request), run litecoin_block jobs
         $result[] = array('job_type' => $name, 'user_id' => get_site_config('system_user_id'), 'arg_id' => -1);
     }
     // block count jobs (using the new Currencies framework)
     foreach (\DiscoveredComponents\Currencies::getBlockCurrencies() as $cur) {
         $name = "blockcount_" . $cur;
         $result[] = array('job_type' => $name, 'user_id' => get_site_config('system_user_id'), 'arg_id' => -1);
     }
     // difficulty jobs (using the new Currencies framework)
     foreach (\DiscoveredComponents\Currencies::getDifficultyCurrencies() as $cur) {
         $name = "difficulty_" . $cur;
         $result[] = array('job_type' => $name, 'user_id' => get_site_config('system_user_id'), 'arg_id' => -1);
     }
     // markets jobs (using the new Exchanges framework: #400)
     foreach (\DiscoveredComponents\Exchanges::getKeys() as $exchange) {
         if (!in_array($exchange, \DiscoveredComponents\Exchanges::getDisabled())) {
             $name = "markets_" . $exchange;
             $result[] = array('job_type' => $name, 'user_id' => get_site_config('system_user_id'), 'arg_id' => -1);
             $name = "ticker_" . $exchange;
             $result[] = array('job_type' => $name, 'user_id' => get_site_config('system_user_id'), 'arg_id' => -1);
         }
     }
     // supported currencies jobs (using the new Accounts framework)
     foreach (\DiscoveredComponents\Accounts::getKeys() as $key) {
         if (!in_array($key, \DiscoveredComponents\Accounts::getDisabled())) {
             $name = "currencies_" . $key;
             $result[] = array('job_type' => $name, 'user_id' => get_site_config('system_user_id'), 'arg_id' => -1);
         }
     }
     // supported hashrates jobs (using the new Accounts framework)
     foreach (\DiscoveredComponents\Accounts::getMiners() as $key) {
         if (!in_array($key, \DiscoveredComponents\Accounts::getDisabled())) {
             $name = "hashrates_" . $key;
             $result[] = array('job_type' => $name, 'user_id' => get_site_config('system_user_id'), 'arg_id' => -1);
         }
     }
     return $result;
 }