public function testDelete() { $repository = new JobRepository(); $job = new Job(); $job->task = 'test.test'; $job->state = JobState::RUNNING; $job->attempts = 7; $job->save(); $this->assertNotNull(Job::find($job->id)); $repository->delete($job->id); $this->assertNull(Job::find($job->id)); }
/** * Rollback changes performed by this migration. * * @return mixed */ public function down() { $this->builder->table(Job::resolveTable()->getName(), function (Blueprint $table) { if (!$this->isSqlite()) { $table->dropColumn(['queue_connection', 'queue_name']); } }); }
public function testFire() { $task = new GarbageCollectTask(); $job = new Job(); $job->task = 'test.test'; $job->state = JobState::SCHEDULED; $job->created_at = Carbon::now()->subDays(32); $job->save(); $job2 = new Job(); $job2->task = 'test.test2'; $job2->state = JobState::FAILED; $job2->created_at = Carbon::now()->subDays(32); $job2->save(); $job3 = new Job(); $job3->task = 'test.test3'; $job3->state = JobState::COMPLETE; $job3->created_at = Carbon::now()->subDays(32); $job3->save(); $job4 = new Job(); $job4->task = 'test.test4'; $job4->state = JobState::CANCELLED; $job4->created_at = Carbon::now()->subDays(32); $job4->save(); $job5 = new Job(); $job5->task = 'test.test5'; $job5->created_at = Carbon::now()->subDays(28); $job5->save(); $gcJob = new Job(); $gcJob->task = 'jobs.gc'; $gcJob->created_at = Carbon::now()->subDays(1); $gcJob->save(); $task->fire($gcJob, m::mock(JobSchedulerInterface::class)); $this->assertNotNull(Job::find($job->id)); $this->assertNull(Job::find($job2->id)); $this->assertNull(Job::find($job3->id)); $this->assertNull(Job::find($job4->id)); $this->assertNotNull(Job::find($job5->id)); }
/** * Process a job. * * Note: A handler implementation does not need to worry about exception * handling and retries. All this is automatically managed by the task * runner. * * @param Job $job * @param JobSchedulerInterface $scheduler */ public function fire(Job $job, JobSchedulerInterface $scheduler) { $jobs = Job::query()->whereIn('state', [JobState::FAILED, JobState::COMPLETE, JobState::CANCELLED]); $repeatIn = $job->get('repeatIn', -1); // How far back to look $days = $job->get('days', 30); $jobs->where('created_at', '<', Carbon::now()->subDays($days)); $job->append('Removing jobs from ' . $days . ' days ago'); $total = $jobs->count(); $processed = 0; $jobs->chunk(25, function ($jobs) use(&$processed, $total, $job) { $processed += count($jobs); foreach ($jobs as $staleJob) { $staleJob->delete(); } $job->append('Progress: ' . $processed . '/' . $total); }); if ($repeatIn > -1) { $scheduler->pushCopy($job, Carbon::now()->addMinutes(max($repeatIn, 1)), Carbon::now()->addMinutes($job->get('expiresAfter', 1440))); } }
public function testFireWithCustomQueue() { $job = new Job(); $job->state = JobState::SCHEDULED; $job->run_at = Carbon::now()->subMinute(); $job->queue_connection = 'dogemq'; $job->queue_name = 'food'; $job->task = 'sometask'; $job->save(); $impersonator = new Impersonator(); $impersonator->mock(JobSchedulerInterface::class, function (MockInterface $mock) use($job) { $mock->shouldReceive('findReady')->atLeast()->once()->andReturn(new Collection([$job])); }); $impersonator->mock(Repository::class, function (MockInterface $mock) { $mock->shouldReceive('get')->with('jobs.queue.connection')->andReturn('nopemq')->once(); $mock->shouldReceive('get')->with('jobs.queue.id')->andReturn('fakejobs')->once(); }); $impersonator->mock(QueuePusherInterface::class, function (MockInterface $mock) { $mock->shouldReceive('push')->atLeast()->once()->with(m::type('string'), m::type('array'), 'dogemq', 'food'); }); /** @var EnqueueScheduledCommand $command */ $command = $impersonator->make(EnqueueScheduledCommand::class); $input = new StringInput('--take=25'); $output = new NullOutput(); $command->setLaravel($this->app); $command->run($input, $output); }
public function testFindByTag() { $impersonator = new Impersonator(); /** @var JobScheduler $scheduler */ $scheduler = $impersonator->make(JobScheduler::class); $job = new Job(); $job->task = 'test.test'; $job->state = JobState::SCHEDULED; $job->save(); $this->assertEquals(0, $scheduler->findByTag('wow')->count()); $scheduler->tag($job, 'wow'); $scheduler->tag($job, 'omg'); $scheduler->tag($job, 'wow'); $this->assertEquals(1, $scheduler->findByTag('wow')->count()); $this->assertEquals(1, $scheduler->findByTag('omg')->count()); $job->state = JobState::CANCELLED; $job->save(); $this->assertEquals(0, $scheduler->findByTag('wow')->count()); $this->assertEquals(0, $scheduler->findByTag('omg')->count()); $this->assertEquals(1, $scheduler->findByTag('wow', false)->count()); $this->assertEquals(1, $scheduler->findByTag('omg', false)->count()); }
/** * Cancel a job. * * Note: If the job was pushed on a queue, it won't be removed. However, * workers should query the DB to check if it has been cancelled before * running them. * * @param Job $job */ public function cancel(Job $job) { $job->state = JobState::CANCELLED; $job->save(); }
/** * Make sure this job does not run again even if it has retries left. * * This should be used when a problem is detected with the way the job or * task are coded which prevents them from running correctly. * * @param Job $job * @param null $message */ public function giveUp(Job $job, $message = null) { $job->state = JobState::FAILED; $job->attempts = $job->retries + 1; if (!is_null($message)) { $job->message = $message; } $job->save(); }
public function testFireWithExceptionAndRetries() { $laravelJob = m::mock(LaravelJob::class); $laravelJob->shouldReceive('release')->atLeast()->once(); $job = new Job(); $job->task = 'test.test'; $job->state = JobState::QUEUED; $job->attempts = 0; $job->retries = 5; $job->save(); $handler = m::mock(BaseTask::class); $handler->shouldReceive('fire')->with($job, m::type(JobSchedulerInterface::class))->atLeast()->once()->andThrow('Exception'); $impersonator = new Impersonator(); $impersonator->mock(JobRepositoryInterface::class, function (MockInterface $mock) use($job) { $mock->shouldReceive('find')->with(1337)->atLeast()->once()->andReturn($job); $mock->shouldReceive('started')->with($job, m::type('string'))->atLeast()->once(); $mock->shouldReceive('release')->with($job)->atLeast()->once(); }); $impersonator->mock(HandlerResolverInterface::class, function (MockInterface $mock) use($job, $handler) { $mock->shouldReceive('resolve')->with($job)->atLeast()->once()->andReturn($handler); }); /** @var RunTaskCommand $command */ $command = $impersonator->make(RunTaskCommand::class); $command->fire($laravelJob, ['job_id' => 1337]); }
public function testAppend() { $job = new Job(); $job->task = 'test.test'; $this->assertNull($job->message); $job->append('lol'); $this->assertEquals("lol\n", $job->message); $job->append('omg'); $this->assertEquals("lol\nomg\n", $job->message); $job->append('doge'); $this->assertEquals("lol\nomg\ndoge\n", $job->message); $huge = Str::quickRandom(200000); $job->append($huge); $job->append($huge); $job->append('doge'); $this->assertFalse(starts_with($job->message, "lol\nomg\ndoge\n")); $this->assertTrue(ends_with($job->message, "doge\n")); }