public function run() { Rails::systemExit()->register(function () { if (!$this->finished) { $this->active = false; $this->data->success = false; $this->data->error = "Couldn't finish successfuly"; $this->save(); } }); # Ugly: set the current user ID to the one set in the batch, so history entries # will be created as that user. // $old_thread_user = Thread::current["danbooru-user"]; // $old_thread_user_id = Thread::current["danbooru-user_id"]; // $old_ip_addr = Thread::current["danbooru-ip_addr"]; // Thread::current["danbooru-user"] = User::find_by_id(self.user_id) // Thread::current["danbooru-user_id"] = $this->user_id // Thread::current["danbooru-ip_addr"] = $this->ip $this->active = true; $this->save(); $post = Post::create(['source' => $this->url, 'tags' => $this->tags, 'updater_user_id' => $this->user_id, 'updater_ip_addr' => $this->ip, 'user_id' => $this->user_id, 'ip_addr' => $this->ip, 'status' => "active"]); if ($post->errors()->blank()) { if (CONFIG()->dupe_check_on_upload && $post->image() && !$post->parent_id) { $options = ['services' => SimilarImages::get_services("local"), 'type' => 'post', 'source' => $post]; $res = SimilarImages::similar_images($options); if (!empty($res['posts'])) { $post->tags = $post->tags() . " possible_duplicate"; $post->save(); } } $this->data->success = true; $this->data->post_id = $post->id; } elseif ($post->errors()->on('md5')) { // $p = $post->errors(); $p = Post::where(['md5' => $post->md5])->first(); $this->data->success = false; $this->data->error = "Post already exists"; $this->data->post_id = $p->id; } else { // p $post.errors $this->data->success = false; $this->data->error = $post->errors()->fullMessages(", "); } if ($this->data->success) { $this->status = 'finished'; } else { $this->status = 'error'; } $this->active = false; $this->save(); $this->finished = true; // Thread::current["danbooru-user"] = old_thread_user // Thread::current["danbooru-user_id"] = old_thread_user_id // Thread::current["danbooru-ip_addr"] = old_ip_addr }
public function execute() { if ($this->repeat_count > 0) { $count = $this->repeat_count - 1; } else { $count = $this->repeat_count; } Rails::systemExit()->register(function () { if ($this->status == 'processing') { $this->updateAttribute('status', 'pending'); } }, 'job_task'); try { $this->updateAttribute('status', "processing"); $task_method = 'execute_' . $this->task_type; $this->{$task_method}(); if ($count == 0) { $this->updateAttribute('status', "finished"); } else { $this->updateAttributes(array('status' => "pending", 'repeat_count' => $count)); } } catch (Exception $x) { $text = ""; $text .= "Error executing job: " . $this->task_type . "\n"; $text .= " \n"; $text .= $x->getTraceAsString(); Rails::log()->warning($text); $this->updateAttributes(['status' => "error", 'status_message' => get_class($x) . ': ' . $x->getMessage()]); throw $x; } }
public static function systemExit() { if (!self::$systemExit) { self::$systemExit = new Rails\SystemExit\SystemExit(); } return self::$systemExit; }